Using Google App Engine how do I open an html page? - google-app-engine

Using Google App Engine I hope to learn some very basic knowledge with this question. I want to be able to open an index.html page that is placed in a folder when I open the application.
I generated a new application using 'Google App Engine Launcher'
I slightly modified the app.yaml and it now looks like the following...
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /templates
static_dir: templates
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
I also have a added a directory called 'templates'.
In the directory I placed a file called 'index.html'.
<html>
<header><title>This is title</title></header>
<body>
Hello world cls
</body>
</html>
My main.py hasn't been modified so it looks like
#!/usr/bin/env python
#
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
Any ideas on what I need to do or change to find success?
Regards,
Chris
>
My code has changed in a big way because of a comment from Gwyn
I now have the standard Django template code from link (https://console.developers.google.com/start/appengine)
Gwyn eventually the index.html file will be something a little more than a static page so a direct URL that you described won't work. You did teach me though and that will come in handy as I progress. I want to bring in some Polymer code once I get the basics figured out here...
So if anyone can help me feed up a hello world from within a 'templates' folder using an index.html page from a standard django codeset generated from the Google Developers Console then your answer would be very much appreciated here.
Regards,
Chris

First, to use HTML, do you need a template engine, in app engine you can use this EZT, Cheetah, ClearSilver, Quixote, Django, and Jinja2 but for simplicity, you can modify your code to send the html directly
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('<html><header><title>This is title</title></header><body>Hello world cls</body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
But as Gwyn Howell says, for more complicated stuff you must use a template engine

I wanted to update this so if anyone else is struggling they may find success also...
I want to thank both of Gwyn Howell and Kristian Damian. I used both of your comments to come up with an answer for anyone else that has the same question.
I went to clarify that I am using the downloaded Python SDK for Google App Engine
I made a sample python project using File|Create New Application - Python 2.7
I ran the project to make sure it was working without any of my changes
I then decided I would use 'Jinja2' for my template engine
I made a 'templates' folder and placed my 'about_v2.html' file in it
I modified the 'app.yaml' code to read
application: helloworld
version: 2
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
I modified the 'main.py' code to read
import os
import webapp2
import jinja2
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),
'templates')))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world! About.<br />')
class AboutPage_v2(webapp2.RequestHandler):
def get(self):
template_values = {
}
template = env.get_template('about_v2.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainHandler),
('/about_v2.html', AboutPage_v2)],
debug=True)
I then ran the project on my local machine and was able to get to the url
I hope this answer helps someone else.

Related

Dynamic HTML Page not found in App Engine. Returns Cannot Get /index

I am trying to host my HTML page in App Engine. When I am trying to access the webpage with url/index it returns
Cannot GET /index
I have followed the steps mentioned in
https://cloud.google.com/appengine/docs/standard/python/getting-started/hosting-a-static-website
having same file structure as it expects according to the link above.
- my_project
|- app.js
|- app.yaml
|- www
|- index.html
The below is my app.yaml file
# [START app_yaml]
runtime: nodejs
env: flex
service: service_name
# Adding CORS Support
handlers:
- url: /index
static_files: www/index.html
upload: www/index.html
- url: /index
static_dir: www/
- url: /getEndPoint
script: app.js
# [END CORS Support]
# [END app_yaml]
Not sure what am I missing here.
You're following the python 1st generation standard environment documentation, but your app.yaml selects the nodejs flexible environment (and uses statements not supported/ignored in this environment's app.yaml)
Maybe of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment
So you either:
follow the nodejs flexible environment Serving Static Files documentation
switch to the nodejs standard environment (by dropping the env:flex statement and selecting the nodejs10 or nodejs8 runtime) and follow the corresponding Serving Static Files documentation.
If you're unsure which environment you want, go through the Choosing an App Engine Environment guide.

Hosting pure angularjs app on google app engine

I have an angular js app with following structure
app structure
my app folder look like
app folder
the app does use any back end interaction for now but in future its gonna interact with a separate app engine java project . I want to host this angularjs app to google app engine but I am not able to understand the right configuration . I am more confused how do I set up the app.yaml for google app engine ... and is it necessary to have a main.py file as at present I do not have any handler
If I understand correctly, you would like to host a static web page on app engine, specifically an AngularJS app.
You don't need any server side code and can configure your app.yaml as follows:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: www/index.html
upload: www/index.html
- url: /(.*)
static_files: www/\1
upload: www/(.*)
This is assuming you will host your client side code in a www folder and you use a index.html as your index file.
Also take a look at the following guide for hosting a static website [0].
[0] https://cloud.google.com/appengine/docs/standard/python/getting-started/hosting-a-static-website

Google App Engine - Flask Application Bypassing Standard Google Auth on Devserver

I am new to Flask and I am trying to organize my code as a large application for deployment on Google App Engine. I am using Flask-Classy to help organize my application into logical packages.
The application runs on the dev server just fine with one exception. When I set login: required or login: admin in my app.yaml (see app.yaml below), the application appears to skip over the mock Google Login page and automatically signs in as 'test#example.com'.
Normally, I would expect to see the standard Google mock authentication form (below) when login:required or login: admin is set in the app.yaml. However, when I point the browser to the '/' or any other uri, I am directed right to the respective page without authenticating via this form.
I am not sure what I am missing/doing wrong. I am not getting any type of errors.
Below, I have included my application's structure and most of the pertinent code to see if anyone might be able to point out anything I am doing wrong here.
/flask_app
/flask_app
/mod_one
/__init__.py
/forms.py
/views.py
/models.py
/mod_two
/__init__.py
/forms.py
/views.py
/models.py
/static
/css
...
/templates
/index.html
...
/__init__.py
/settings.py
/urls.py
/lib
/app.yaml
/appengine_config.py
/main.py
app.yaml
application: flask-app
version: 1
runtime: python27
api_version: 1
threadsafe: yes
default_expiration: "30d"
builtins:
- deferred: on
- admin_redirect: on
handlers:
- url: /css
static_dir: flask_app/static/css
- url: /admin/.*
script: main.app
login: admin
- url: /.*
script: main.app
login: required
appengine_config.py
import sys
import os.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
main.py
from flask_app import app
app = app
flask_app/init.py
import os
from flask import Flask
from flask_bootstrap import Bootstrap
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
if os.getenv('FLASK_CONF') == 'TEST':
app.config.from_object('flask_app.settings.Testing')
elif 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
# Development settings
app.config.from_object('flask_app.settings.Development')
app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True)
else:
app.config.from_object('flask_app.settings.Production')
# Instantiate Flask-Bootstrap.
Bootstrap(app)
import urls
flask_app/urls.py
from flask import render_template
from flask_app import app
#app.route('/')
def index():
params = {
'page_title': 'Home'
}
return render_template('index.html', **params)
#app.route('/contact')
def contact():
params = {
'page_title': 'Contact'
}
return render_template('contact.html', **params)
#app.route('/about')
def about():
params = {
'page_title': 'About'
}
return render_template('about.html', **params)
# Error handlers
# Handle 404 errors
#app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
# Handle 500 errors
#app.errorhandler(500)
def server_error(e):
return render_template('500.html'), 500
# Register Flask Classy Classes
import mod_one.views as mod_one
import mod_two.views as mod_two
mod_one.OneView.register(app)
mod_two.TwoView.register(app)
As #Greg pointed out in a comment to the question, I was already 'logged in' to the dev_appserver (which threw me off since I never saw the mock login page) so authentication was happening using the default 'test#example.com' user.
Once I went to /_ah/login I was able to access the mock login page and proceed as usual from there.

unable to redirect to a particular url

Hi I am building a web site.
here requirement is to build a url like
http://localhost:10080/edit/?d=2014-02-22%2015:36:38.688000
and it have to be processed bu application
application = webapp2.WSGIApplication([
('/edit',EditPost),
('/',HomePage),
('/post',PostPosting),
], debug=True)
I am using above but its not recognizing EditPost class
my app.yaml is
application: your-app-id-indoor
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: IndoorBlog.application
- url: /stylesheets
static_dir: stylesheets
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
---could some one help me please...
thanks
According to your webapp2.WSGIApplication Class, you are handling edit url as http://localhost:8080/edit not /edit/.
If you need to handle this url http://localhost:10080/edit/?d=2014-02-22%2015:36:38.688000,
then webapp2.WSGIApplication class should be like this:
application = webapp2.WSGIApplication([
('/edit/',EditPost),
('/',HomePage),
('/post',PostPosting),
], debug=True)
Webapp2 routing has named routes for redirect_to:
Docs: https://webapp-improved.appspot.com/api/webapp2.html#webapp2.redirect_to

multiple file handlers in google app engine

I want to have multiple pages in google-app engine (python 2.7) and the followoing is my directory structure:
root contains: pujaweb.py (the main script), index.html, stylesheets(folder), port(folder)...
now my second script for the page is in the port folder and i want to configure my index.html such that it links to that page. I have tried a lot of stuff but somehow it does not work and always shows 404 page not found error and the command line says that PujaPort module does not exist (pujaport being the app handler for the second page).
the following is my app.yaml file code:
application: thepujabhalerao
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /images
static_dir: images
- url: /stylesheets
static_dir: stylesheets
- url: /port
script: pujaport.app
- url: /.*
script: pujaweb.app
libraries:
- name: jinja2
version: latest
this is my main handler (pujaweb.py)
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
and the href in the index filer looks like this:
P
and finally this is the second page handler pujaport.py:
import cgi
import webapp2
import jinja2
import os
from google.appengine.api import xmpp
from google.appengine.ext import db
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class PujaPort(webapp2.RequestHandler):
def get(self):
self.response.out.write("In handler")
app = webapp2.WSGIApplication([('/port', PujaPort)],
debug=True)
I know its a minor glitch somewhere but please help me out as after being on it for too long i maybe missing the obvious.
If you insist on using that folder structure, you'll need to make the port directory into a package by adding an __init__.py file to it (which can be empty), and reference the app as port.pujaport.app, its fully qualified name.

Resources