I'm just getting started in python with google app engine using the webapp2 framework and jinja2 templating. I can't seem to get my first and very simple script up and running. All I want is for the script to serve the index.html file(located in the same directory).
Here is the app.yaml file:
libraries
- name: webapp2
version: latest
- name: jinja2
version: latest
application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: practice.application
Here is practice.py:
import os
import webapp2
from jinja2 import Enviroment, FileSystemLoader
loader = jinja2.FileSystemLoader(os.path.dirname(__FILE__)
env = jinja2.Enviroment(loader)
class MainPage(webapp2.RequestHandler):
def get(self):
template = env.get_template('index.html')
self.response.write(template.render())
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
Update:
I am running this locally from the Google app engine launcher.
When I attempt to open the file I receive a server error with the description
The website encountered an error while retrieving http://localhost:9080/. It may be
down for maintenance or configured incorrectly."
Here's why your code won't run:
Your app.yaml is malformed
Enviroment is spelt wrong
Your missing a closing bracket on line 5
You haven't imported the jinja2 library
The variable __FILE__ is undeclared
Here's what I think your code should look like:
app.yaml
application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
handlers:
- url: /.*
script: practice.application
practice.py
import jinja2
import os
import webapp2
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
env = jinja2.Environment(loader=loader)
class MainPage(webapp2.RequestHandler):
def get(self):
template = env.get_template('index.html')
self.response.write(template.render())
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
I suggest you do the following to make your life a LOT easier:
download eclipse (I assume you haven't given the syntax errors) -> http://eclipse.org/
complete the tutorial -> https://developers.google.com/appengine/docs/python/gettingstartedpython27/introduction
read the docs -> https://developers.google.com/appengine/docs/python/
Hope this helps get you on your way.
Happy coding :)
In webapp2 you should use app instead of application, so last line should look like this:
app = webapp2.WSGIApplication([('/', MainPage),], debug=True)
Related
I'm having some difficulties setting up Django 1.5 to run via the dev_appserver.py, rather than the manage.py runserver.
The project directory is as such:
env
myproject
manage.py
myproject
init, settings, urls, wsgi (.py)
I've tried to two following ways to get it working with the App Engines appserver:
app.yaml:
application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.5"
builtins:
- django_wsgi: on
as suggested from this tutorial https://cloud.google.com/appengine/docs/python/cloud-sql/django
It does not produce any error when setting the up the local server, but when I access the given link, I get this error:
RuntimeError: django must be included in the "libraries:" clause of your app.yaml file when using the django_wsgi builtin.
Next I've tried it in a slightly different way.
With app.yaml
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.application
libraries:
- name: django
version: "1.5"
and a main.py as such:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
which produces the following error:
File "C:\Users\Mello\AppEngine\02\myproject\main.py", line 4, in module>
import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
Even though if I run
>>>import django.core.handlers.wsgi
>>>
in the same virtual environment, it runs fine.
Anybody has any piece of advice to point me in the right direction to fix this?
Thanks
I have tried all the existing resources but to no avail.
here is my app.yaml code:
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.app
Here is my webapp2 code:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
All in my hello_world directory. I proceed to add existing application and point to the hello_world directory where helloworld.py (webapp2) script and the app.yaml code are living. I hit run on Google App Engine and it just stays loading, it never completes. Therefore, my http://localhost:8080/ page never loads.
You're missing the application: line in your app.yaml.
You're probably also gonna need to list the 3rd party webapp2 in the libraries section, something like this:
libraries:
- name: webapp2
version: "2.5.2"
I am trying to run a web application using CherryPY framework in Google App engine . I am unable to run the basic helloworld code in development server locally (from downloaded the SDK )
I am getting ImportError: No module named cherrypy. Although I did install
cherrypy using pip install and the same code works using
python hello.py ( removing the google import )
this is my hello.py
import random
import string
import cherrypy
from google.appengine.ext.webapp.util import run_wsgi_app
class StringGenerator(object):
#cherrypy.expose
def index(self):
return "Hello world!"
#cherrypy.expose
def generate(self):
return ''.join(random.sample(string.hexdigits, 8))
if __name__ == '__main__':
cherrypy.quickstart(StringGenerator(), '/')
and my app.yaml file
version: 1
runtime: python27
api_version: 1
threadsafe: true
# [START handlers]
handlers:
- url: /.*
script: hello.app
# [END handlers]
# [START libraries]
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
# [END libraries]
Cherrypy is not bundled as part of App Engine but since it's a pure python framework, you can resort to vendoring to add it to your project so the development server can pick it up:
$ mkdir lib
$ pip install -t lib cherrypy
Create a new appengine_config.py file in your application's root, same location as your app.yaml etc... with the following contents:
from google.appengine.ext import vendor
vendor.add('lib')
More info can be found here and here.
If you're using Python 3+ and CherryPy, you can follow this tutorial in order to deploy your web app / API REST on Google App Engine. I hope this can help you.
I'm trying to get web.py app running on local Google App Engine.
My yaml:
application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: code.app
My code.py:
import web
urls = (
"/.*", "hello",
)
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
app = app.gaerun()
When I start the server all I get is a blank page. So what's wrong?
Edit:
python --version
Python 2.7.6
Edit 2:
Error from console:
ImportError: No module named web
What you're trying to do is import an external module, which is not supported by GAE.
What you can do though, is copy web.py into your app directory, and then use it. See "How to include third party Python libraries in Google App Engine".
You can get the source code from here
Hi I'm trying to get my Google App Engine application running on local host but I'm getting issues when I try to run it.
This is the code I'm tryng to run:
# helloworld.py
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
#"Test" text is not displayed
self.response.write("Test")
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
#This line prints the content
print "This works fine"
When I run the dev server on localhost I get Code 200 as response but the handler doesn't seems to be executed properly.
Any idea?
Your code looks fine, just make sure your in your app.yaml you have the handlers correct. It should look something like this:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: helloworld.app
libraries:
- name: webapp2
version: "2.5.2"
If you haven't done it yet, you should also complete the Getting Started tutorial.
I solved this issue. Using webapp2 library I have to add the folowing code to the *.yaml file:
libraries:
- name: webapp2
version: "2.5.2"