I'm trying to get a web.py application running on GAE. I hoped that sth like the following might work
import web
from google.appengine.ext.webapp.util import run_wsgi_app
[...]
def main():
app = web.application(urls, globals())
run_wsgi_app(app)
But obviously the app object doesn't conform with the run_wsgi_app function's expectations. The error msg says sth like app has no __call__ function, so I tried passing app.run instead, but that didn't work either.
How can I make the call to run_wsgi_app work?
Here is a snippet of StackPrinter, a webpy application that runs on top of Google App Engine.
from google.appengine.ext.webapp.util import run_wsgi_app
import web
...
app = web.application(urls, globals())
def main():
application = app.wsgifunc()
run_wsgi_app(application)
if __name__ == '__main__':
main()
You don't need to import or use run_wsgi_app, web.py has a runcgi method that works perfectly!
if __name__ == '__main__':
app.cgirun()
Related
I am using Flask to build a tool to view data locally in a browser. I want to pass the directory containing the data as a command line argument, and then pass it to the appropriate routing function to do the rendering.
This does what I want, but with global variables:
dataDir = None
def initializeData(pathname):
global dataDir
dataDir = pathname
#app.route('/')
def home():
# Use dataDir as desired
if __name__ == '__main__':
initializeData(sys.argv[1])
app = Flask(__name__)
app.run()
Is there a better way to communicate between the command line and my routes?
Your flask app has a config property. Also, this code will fail with a NameError. You want something like this:
import sys
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'You wanted {!r} directory'.format(app.config.get('some_setting'))
if __name__ == '__main__':
app.config['some_setting'] = sys.argv[1]
app.run()
Consider using app.config.from_json('config.json') so that you can configure your env parameters in a json file.
I'm using Flask with virtualenv, and my demo Flask app is structured as follows:
app/
hello.py
config/
settings.py
venv/
virtualenv files
Contents of hello.py
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.from_object("config.settings")
#app.route('/')
def index():
return app.config["HELLO"]
return app
if __name__ == "__main__":
app = create_app()
app.run()
settings.py contains just 2 values
DEBUG = True
HELLO = "Hello there from /config !"
I can run this successfully with gunicorn using gunicorn -b 0.0.0.0:9000 --access-logfile - "app.hello:create_app()", it works without any errors.
However, running python app/hello.py from root results in the error ImportError: No module named 'config'. It seems that flask is unable to find the config directory when executed in this manner.
I could move the config directory inside app, but doing so would cause errors with gunicorn instead. Is it not possible to have both ways "just work" ? More importantly, why and what is happening ?
Not the most elegant yet still perfectly working solution:
from os.path import abspath, join
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
config_file_path = abspath(
join(app.instance_path, '../config/settings.py')
)
app.config.from_pyfile(config_file_path)
#app.route('/')
def index():
return app.config["HELLO"]
return app
if __name__ == "__main__":
app = create_app()
app.run()
Addition after considering the comment. In order for Flask to properly import config.settings, the path to app root has to be inside sys.path. It can easily be achieved by adding a single line in the original script:
sys.path.insert(0, os.getcwd())
So the final hello.py looks like:
import os
import sys
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
sys.path.insert(0, os.getcwd())
app.config.from_object("config.settings")
#app.route('/')
def index():
return app.config["HELLO"]
return app
if __name__ == "__main__":
app = create_app()
app.run()
Even more bullet-proof solution would be
app_root_path = os.path.abspath(
os.path.join(app.instance_path, '..')
)
sys.path.insert(0, app_root_path)
This way we do not depend on what os.getcwd() returns: it does not always have to return the app root path.
The Tumblelog app on the MongoDB site does not work .
I've followed the example absolutely and I get a 404 error when I run it in my local host. I'm using Eclipse Indigo (3.7.2) with pyDev on Ubuntu 12.0.4.
I'm not sure if it's because of the register_blueprints, which I included in the __init__.py
I did it like this as in the tutorial:
from flask import Flask
from flask.ext.mongoengine import MongoEngine
app = Flask(__name__)
app.config["MONGODB_DB"] = "my_tumble_log"
app.config["SECRET_KEY"] = "KeepThisS3cr3t"
db = MongoEngine(app)
if __name__ == '__main__':
app.run()
def register_blueprints(app):
# Prevents circular imports
from tumblelog.views import posts
app.register_blueprint(posts)
register_blueprints(app)
Otherwise I have followed the tutorial exactly.
register_blueprints is never called - app.run blocks until you kill the script (at which point there is no point to adding routes).
Change the order and everything will run:
def register_blueprints(app):
# Prevents circular imports
from tumblelog.views import posts
app.register_blueprint(posts)
register_blueprints(app)
if __name__ == '__main__':
app.run()
regist_blueprints is not actually preventing circular imports - the pattern to avoid circular imports is to create the app in a different file and import both app and blueprint into a third file to run everything:
# application.py
from flask import Flask # etc.
app = Flask("your_package_name")
# tumblelog/views.py
from flask import Blueprint, current_app # etc.
posts = Blueprint("tumblelog")
#posts.route("/")
def index():
# use current_app rather than app here
# run_server.py (use the same pattern for .wsgi files)
from application import app
from tumblelog.views import posts
app.register_blueprint(posts)
if __name__ == "__main__":
app.run()
I am trying to break my app into separate scripts. Part of this effort meant breaking the api calls into it's own file. However the calls to the api (like http://example.com/api/game/new no longer work).
My app.yaml contains this:
- url: /api.*
script: api.py
which seems to be redirecting properly because this configuration works:
def main():
application = webapp.WSGIApplication([('/.*', TestPage)], debug=True)
util.run_wsgi_app(application)
however this one doesn't:
def main():
application = webapp.WSGIApplication([('/game/new$', CreateGame),
('/game/(d+)$', GameHandler)],
debug=True)
util.run_wsgi_app(application)
The URL patterns you use in the WSGI application have to be the full path - eg, /api/game/.... The App Engine infrastructure uses the regular expressions in app.yaml to route requests, but it does not modify the request path based on them.
My guess is you are trying to pass some arguments to your handler.
Try this. It will give you a hint.
#!/usr/bin/env python
import wsgiref.handlers
from google.appengine.ext import webapp
class MyHandler(webapp.RequestHandler):
def get(self, string=None):
if string:
self.response.out.write("Hello World!! %s" % string)
else:
self.response.out.write("Hello World!! (and no word)")
def main():
app = webapp.WSGIApplication([
(r'/word/(\w+)/?$', MyHandler),
(r'.*', MyHandler),
], debug=True)
wsgiref.handlers.CGIHandler().run(app)
if __name__ == "__main__":
main()
Hope it helps. Cheers.
I'm just starting learning google app engine.
When I enter the following code below, all I got is "Hello world!"
I think the desired result is "Hello, webapp World!"
What am I missing? I even try copying the google framework folder to live in the same folder as my app.
Thanks.
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Running exactly this code DOES give me the desired output. I suspect you must be erroneously running some other code, since there's absolutely no way this code as given could possibly emit what you observe.