When testing a Google App Engine app with the Launcher, the Launcher will start a local server, and open a tab listening to http://localhost:8080/ If the App.yaml file is configured to point to a html file, then the webpage will open. Like if your home page is index.html
app.yaml file
application: myProjectName
version: 2
runtime: python27
threadsafe: false
api_version: 1
handlers:
- url: .*
static_dir: index.html
If the app.yaml is configured to point to a python script in the root url, I don't know how to get the script to load a web page:
app.yaml file
- url: /.*
script: main.py
If my main.py Python script is:
import webbrowser
webbrowser.open_new("README.html")
That will open the README.html file in my browser when I run the code from the Python Shell, but if I launch the app from Google App Engine Launcher, it won't load the html file. How do I get the .py file to open an HTML file after the Launcher has started the app on the localhost:8000 ?
I'm looking at a Google example, and it uses a WSGIApplication webapp I guess. First the Python code goes through an authorization process, then at the end of the script, there is this code:
# Create an WSGI application suitable for running on App Engine
application = webapp.WSGIApplication(
[('/', MainPage), ('/svc', ServiceHandler), ('/about', AboutHandler),
('/user', UserHandler)],
# XXX Set to False in production.
debug=True
)
def main():
"""Main entry point for executing a request with this handler."""
run_wsgi_app(application)
if __name__ == "__main__":
main()
I'd appreciate any feedback from anyone who has any experience with this.
The simplest example that I know is the hello world in the documentation:
# helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
The corresponding app.yaml is:
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.application
You create a class MainPage -- whenever you make a request to localhost:8080/ (note the slash is unncessary), you will be directed to the main page. the application is responsible for routing the request to the proper class and creating a new instance of that class for each request. It also calls the get or post or whatever HTTP method. Whatever you write into the response is what gets returned to the browser as the web page.
Now a single page isn't all that interesting. Maybe you want localhost:8080/goodbye as well. Then you just add another class and "register" it with the application:
# helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
class GoodbyePage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Goodbye, World. Time to sleep.')
application = webapp2.WSGIApplication([
('/', MainPage),
('/goodbye', GoodbyePage),
], debug=True)
No changes to app.yaml are necessary at this point.
Life might be kind of frustrating if you needed to keep all your pages in a single file though. We can break this into 2 (or more) files by modifying app.yaml.
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /goodbye
script: goodbye.app
- url: /.*
script: helloworld.application
helloworld.py is the same as the first example. goodbye.py looks like this:
# goodbye.py
import webapp2
class GoodbyePage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.response.write('<html><head></head><body>Goodbye!</body></html>')
app = webapp2.WSGIApplication([
('/goodbye', GoodbyePage),
], debug=True)
The urls in app.yaml are regular expressions -- you want to order them from most specific to least specific (otherwise, you might handle the request with the wrong handler). Also note that the convention in app.yaml is script: <module_name>.<variable_name> when we set it up this way.
Here is another example without Jinja2. To serve HTML with Python you simply need these 3 simple things.
1) Use the webapp2 Python module.
import webapp2
2) Read your HTML file into a variable:
INDEX_HTML = open('index.html').read()
3) Then write it out when a certain URL is sent:
self.response.out.write(INDEX_HTML)
This is the full Python script:
import cgi
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
INDEX_HTML = open('index.html').read()
self.response.out.write(INDEX_HTML)
class PageTwo(webapp2.RequestHandler):
def post(self):
self.response.write('<html><body>You wrote:<pre>')
self.response.write(cgi.escape(self.request.get('content')))
self.response.write('</pre></body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', PageTwo),
], debug=True)
Note that your app.yaml configuration needs to be correct, and you need an HTML file. The webapp2 module provides a RequestHandler that can route your URL requests. (Display different webpage content) Here is a GitHub repository with the code, manifest app.yaml file and the index.html file.
Serve HTML with Python
This gets the HTML from a file, then writes it to the browser. This is actually more of what I was looking for:
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render())
This is the entire module:
import cgi
import os
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class MainPage(webapp2.RequestHandler):
def get(self):
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render())
class PageTwo(webapp2.RequestHandler):
def post(self):
self.response.write('<html><body>You wrote:<pre>')
self.response.write(cgi.escape(self.request.get('content')))
self.response.write('</pre></body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', PageTwo),
], debug=True)
Here is a link to the Git Hub repository:
Get Template HTML, and write to browser
I saw an interesting variation of the app.yaml configuration file in a code example:
- url: /
static_files: index.html
upload: index.html
Even though the webbapp2 Request Handler is serving a particular HTML file based on the URL, the app.yaml file also controls what happens when different URL's are sent. The app.yaml file can be configured to run the python script AND upload a particular HTML file. If your application has lots of sub-directories, and the main.py script doesn't load until the end of your app.yaml file, configuring the app.yaml file to load static HTML right up front may cause your main page to load faster:
application: YourProjectNameHere
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
#Even though the main.py script also loads the index,
#this is faster if lots of processing
- url: /
static_files: index.html
upload: index.html
- url: /Client_Style
static_dir: Client_Style
- url: /Client_Pages
static_dir: Client_Pages
- url: /Client_Script
static_dir: Client_Script
- url: /Library
static_dir: Library
- url: /apiclient
static_dir: apiclient
- url: /Client_Data
static_dir: Client_Data
- url: /oauth2client
static_dir: oauth2client
- url: /uritemplate
static_dir: uritemplate
- url: /.*
script: main.application
libraries:
- name: webapp2
version: latest
Related
I have a web site up and running with about 100 urls specified in my app.yaml file. Because app.yaml is limited to 100 urls, I am trying to figure out how to transfer the app.yaml specified routing instructions into main.py. I want modify (shorten) the app.yaml file to send all requests to main.py and then I want main.py to route the requests as app.yaml currently does. I have been experimenting with the three files below but can't get a response of "Hello from MainHandler in test1.py" if I request any url other than /test1. Why am I not getting a browser response of "Hello from MainHandler in test1.py" request a url of /xyz?
Test 1:
Request: /test1
Response: Hello from MainHandler in test1.py
Test 2:
Request: /example
Response: Hello from ExampleHandler in main.py
Test3:
Request: /xyz
Response: Hello MainHandler in main.py
Why doesn't Test3 above also result in a response of "Hello from MainHandler in test1.py" and how can I make this happen?
main.py:
#!/usr/bin/env python
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello MainHandler in main.py<br>')
import test1
test1.app
class ExampleHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello from ExampleHandler in main.py<br>')
app = webapp2.WSGIApplication([
('/example', ExampleHandler),
(r'.*', MainHandler)],
debug=True)
test1.py:
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello from MainHandler in test1.py')
app = webapp2.WSGIApplication([(r'.*', MainHandler)], debug=True)
if __name__ == "__main__":
app
app.yaml:
application: test-for-rr
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /test1
script: test1.app
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
The precedence hierarchy goes from app.yaml -> handlers specified in the program.
When you hit /test1, app.yaml routes your request to test1.app (because it matches the /test1 route) which routes your request to test1.MainHandler.
When you hit /test3, app.yaml routes your request to main.app (because it matches the .* route) which routes your request to main.MainHandler.
I am new to developing apps on google appengine and to python and jinja. I have been trying this since two days. My first app without jinja worked fine. However this particular app is not displaying anything on the browser. This is the main.py file
import webapp2
from webapp2_extra import jinja2
import logging
# this one is to help us parse an RSS feed
import feedparser
import urllib
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
class MainHandler(BaseHandler):
def get(self):
context = {}
self.render_response('index.html', **context)
# self.response.write('Byte1')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
And this is the app.yaml file
application: ykelkar-byte1
version: 1
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: "2.5.2"
- name: jinja2
version: latest
I tried to run the sample code that I had and it is getting displayed. So there shouldn't be any installation/configuration issue.
First thing I noticed is that your import is wrong:
from webapp2_extra import jinja2
should be:
# You have a missing s!
from webapp2_extras import jinja2
In general, during development you should enable debugging to catch simple errors like this by setting up your WSGIApplication as follows:
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
EDIT: Just noticed you already had debugging enabled. An error like this should have been showing you in your development console.
Are you using any particular IDE for development? I have found PyCharm to be a really good IDE for python development and google app engine specifically (The community edition is free). Maybe you should give it a try to help you out.
I have the following app.yaml file
application: gtryapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /images/(.*\.(gif|png|jpg))
static_files: static/img/\1
upload: static/img/(.*\.(gif|png|jpg))
- url: /css/(.*\.css)
mime_type: text/css
static_files: static/css/\1
upload: static/css/(.*\.css)
- url: /js/(.*\.js)
mime_type: text/javascript
static_files: static/js/\1
upload: static/js/(.*\.js)
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
And the file app.py:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
if self.request.url.endswith('/'):
path = '%sindex.html'%self.request.url
else:
path = '%s/index.html'%self.request.url
self.redirect(path)
application = webapp2.WSGIApplication([('/.*', MainPage)],
debug=True)
The files that I should deploy are just html files or js or images, I get the following error after compiling the app:
raise ImportError('%s has no attribute %s' % (handler, name))
ImportError: has no attribute app
Solved: I had to call "app" not "application" !
app = webapp2.WSGIApplication([('/.*', MainPage)],
debug=True)
You've called the file index.py, not main.py. Either rename it, or use index.app in the yaml.
The issue you are having is that your app.yaml file doesn't properly describe your code. Here is the offending bit:
- url: .*
script: main.app
This says that all URLs that were not matched by some previous entry should be handled by the app object of the main module, which should be a WSGI application object (see the WSGI standard).
This doesn't work because your code is set up differently. Your primary module is in index.py (the index module) and its interface with the server is via the CGI standard (though WSGI is used internally).
So, you need to change something. It could either be the app.yaml description of the app, or it could be the organization of your code.
Making your code work as a CGI-style program is easy. Just change app.yaml to point to index.py as the script. The .py part in this case is the file extension, and the file will be run as a script.
If instead you want to go with the newer, WSGI-compatible style (which is probably the best option), the documentation suggests the following format:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
app = webapp2.WSGIApplication([('/', MainPage)])
Your code is almost like this already. To make it work, get rid of your main function and if __name__ == "__main__" boilerplate. Replace it with:
app = webapp.WSGIApplication([('/.*', IndexHandler)],
debug=False)
This creates an app object at the top level of your module. Now, either rename your index.py file to main.py, or change app.yaml to point to index.app. The .app part of this is different this time. Rather than a file extension, it represents Python member access (in this case, accessing a global variable in a module).
I am trying to study using Python in Google App Engine and can't get the tutorial to work. But ultimately, I would want to write a Python script that would return list of files in a folder in server to JavaScript.
Here's what I currently have:
+MainProj
+ static
+scripts
. __init__.py
. helloworld.py
. app.yaml
In helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([('/.*', MainPage)], debug=True)
In app.yaml
application: applicationname
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: static.scripts.helloworld.app
I am getting a server error
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
Anyone can help what's wrong with my setup?
Every folder in your package path ('static.scripts.helloworld.app') needs to have __init__.py in it to import properly, so either add one to 'static' or (more sensibly, in my opinion) move helloworld.py up to the top, and use 'helloworld.app' in your app.yaml.
All you need in your app.yaml handler is:
- url: /.*
script: static.scripts.helloworld.py
And make sure you also have in your helloworld.py at the bottom code to actually start the application and listener:
from google.appengine.ext.webapp import util
# granted, you might want to replace "webapp" with "webapp2" here
def main():
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
Can someone give an example of how the warmup inbound service works in the python runtime of Google App Engine?
I've read this: http://code.google.com/appengine/docs/python/config/appconfig.html#Inbound_Services, but it doesn't give me much of an example after the GET request is sent (I can't seem to ever pick it up)
My app.yaml looks like this:
application: whatevs
version: 1
runtime: python
api_version: 1
builtins:
- datastore_admin: on
inbound_services:
- warmup
handlers:
- url: /static
static_dir: static
- url: /_ah/warmup
script: main.py
login: admin
- url: /.*
script: main.py
my main.py looks like this:
def main():
application = webapp.WSGIApplication(
[("/", views.LandingPage),
("/_ah/warmup", views.WarmupHandler)
],
debug=True)
run_wsgi_app(application)
WarmupHandler looks like this:
class WarmupHandler(webapp.RequestHandler):
"""
Called on app init
"""
def get(self):
current_user = users.get_current_user()
return
However, WarmupHandler never seems to get called (I have breakpoints and lots of debug code). What am I doing wrong?
App Engine sends warm up requests only if there is some constant traffic on your app. It won't get always called if instances stand mostly idle.