I just started with Google App Engine using python and I was following a tutorial and writing my own little app to get familiar the webapp framework. Now I just noticed the tutorial does the following self.redirect('/'). So that got me wondering: is there a way to redirect to a handler instead of a hardcoded path? Thought that might be better so that you can change your urls without breaking your app.
One alternative would be to have a map of symbolic names to URLs, that way you could redirect to the mapped URL - you could then update your URLs with impunity.
Or if you'd rather just execute the code from another handler, I don't know why you couldn't just make a method call - worst case, you could extract a common method from the two handlers and call that.
This isn't a limitation of App Engine so much as the webapp framework. webapp is intended to be a lightweight framework, providing just the essentials.
If you want fancier redirect behavior, try Django.
The accepted answer made me built my own little router before I realized that in webapp2 you can name your routes and then redirect using that name as described in webapp2 Uri routing
app = webapp2.WSGIApplication(
routes=[webapp2.Route('/', handler='RootController', name='root')])
and then redirect to them in the RequestHandler
self.redirect_to('root')
if your path contains placeholders you can supply the values for the placeholders and the webapp2.Router will build the correct uri for you. Again take a look at webapp2 Uri routing for more detailed information.
Related
I have an application which uses flask and flask-security-too in the rest layer. Since flask-security contains some nice, out-of-the-box solutions for user signup, registration etc. including some override-able views. I would really like to use it if possible.
However, I would instead like to plug in a react frontend to get the SPA things going. I read in the documentation for flask-security that the views are indeed possible to override but not sure if/how I would manage to get this working for a built react app?
To summarize my question: Can I use React.js still benefit from flask-security features?
Please read:
https://flask-security-too.readthedocs.io/en/stable/spa.html
basically - you will be using the JSON api rather than forms.
I've been tasked with converting a service from Flask to aiohttp. Originally this service used Flask admin, but we are moving away from Flask. I saw that there is a aiohttp-admin package, but frankly, the documentation is very poor to almost non-existent and I can't wrap my head around the examples. Are there any other options for aiohttp admin pages out there? I've been using Jinja2 templates, but I think my solution is lacking.
Which exactly functionality do you lack in aiohttp admin? Please note the demos https://github.com/aio-libs/aiohttp_admin/tree/master/demos, they might contain fully or partially what you're looking for.
If you prefer to use jinja2 templates then aiohttp_admin2 is better for your case. The setup of aiohttp_admin2 is very simple and good cover into the documentation.
from aiohttp import web
from aiohttp_admin2 import setup_admin
app = web.Application()
# setup admin interface
setup_admin(app)
web.run_app(app)
It doesn't cover all flask-admin features, but you can add your own views or automatically generate CRUD views based on sqlAlchemy's models (or mongodb models) without JavaScript code.
This link to the official repository you can find here. Here you can find documentation with demo and examples of code.
I am pretty new to google app engine and python. After successfully uploading an application, I am stuck with a basic question.
app = webapp2.WSGIApplication([
('/.*',IndexHandler)
], debug=True)
This is the code block which is generally used to map the request with the class that handles it. However there is a section in app.yaml which allows specifying handler for individual url.
My question is what is the correct architecture of a python application on google app engine. What if my application has several hundreds of classes for handling different request ?, do I have to specify all of them here in this code ?
I have googled but could not find a satisfactory answer. Link to a good tutorial or documentation would be a great help.
Basically, you define the app to be used in app.yaml. For example, if you've got multiple apps, you can specify here which to use.
Yes, you have to specify all the allowed URLs here (in main.py). Otherwise the request will get 404. However, you can use regular expressions to make certain type of addresses to match the given handler.
Check out the tutorial: https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp
Documentation for URI routing: http://webapp-improved.appspot.com/guide/routing.html?highlight=url
You can do like this.
In app.yaml
- url: /service/.*
script: service.app
- url: .*
script: main.app
In service.py
url_map = [
('.*/user.*', 'service.UserHandler'),
('.*/data/show/', 'appname.service.DataShowHandler'),
('.*/data.*', 'appname.service.DataHandler'),
]
app = webapp2.WSGIApplication(url_map)
When you tried to access http://your-appid.com/service/user, appengine will excecute GET function of UserHandler Class in service.py which is located in the Root Folder.
When you tried to access http://your-appid.com/service/data/show, appengine will excecute GET function of DataShowHandler Class in service.py which is located in the Root/appname Folder.
I have two anwers :
1) You can use webapp2 routing to handle the requests and uri routing. This is very powerfull. You can use url templates and / or write your own custom dispatcher.
2) For a lot of requests you can use a single URL and use a session / state to find out how to continue after a post. This means : you do not have to use a request handler for every request.
app.yaml can be used for setting such as secure and login options, though I personally don't use them.
I use main.py to map all urls to the right handlers.
I'm developing an app (an API) in python and I would like to offer some of its functionality through a web interface (like web services do).
I've been looking at django, but I don't know if really fits well in my idea. I only want to create a web page that invokes to my API methods in order to acomplish the functionality that offers that web page. But, after followed the tutorial, I feel a little confused about the point of django. It seems to me to be more related with an ORM than a classic web application.
Is django a solution so heavy for such a simple development (as I mentioned, make calls to my API through the web)? Do I always have to use a database?
Thanks.
I love django but there is an lot of it to get your head around! If you don't want the database bit, focus on urls.py and views.py that will process your urls and return the info you want as an http response.
eg. urls.py
urlpatterns += patterns('myapp.views',
url(r'^getstuff/$', 'getstuff' ),
)
in views.py
def getstuff(request):
do whatever in python
return HttpResponse(stuff to return)
You don't need to use database in Django projects.
Basically django comes with some standardized architecture that follows MVC pattern (or MVT as sometimes described). This includes models, views, url dispatching, templates, etc.
Probably you need to do following things to accomplish your task:
create url definition in urls.py
to some django view
write
django view that call somehow your
api and displays result as a web
page
you don't need models and database at all but you need to get familliar with views, urls, templates. It might look like a big machinery for your simple case but if you have some time I encourage you to these django basics.
If you are looking for somethin much simpler I heard about webpy project. This might be better option if you need something really simple.
An important question is: Do you want the web services to be provided by a full-featured server like Apache, or are you just looking at the "web server" to be a thread (or equivalent) in your program?
If you want to run Apache, then I'd recommend something like Werkzeug, which will handle most of the WSGI stuff for you. For templating, I've heard good things about Jinja2.
If that is too much, and all you want is a lightweight, simple server (something that, say, just spits out some HTML or XML when asked, and doesn't need any fancy URL handling), you can use the SimpleHTTPServer or CGIHTTPServer modules that ship with Python.
Django is a full-featured, integrated package that provides almost everything you need to write database-backed web applications. While its various components can be used in isolation, if you're only using one thing (the template and view engines, in your case), it is probably overkill.
No need for a framework at all. Raw wsgi isn't hard but a little verbose. So I like to use WebOb
Here's Raw wsgi
def application(environ, start_response):
start_response("200 OK", [])
return ["<html><body><h1>Hello World</h1></body></html>"]
Here's the webob version
from webob.dec import wsgify
from webob import Request
#wsgify
def application(request):
return Response("<html><body><h1>Hello World</h1></body></html>")
That's enough to run under apache mod_wsgi, and there are plenty of libraries that you can use that expect/produce webob Request and Responses. Anything that Turbogears 2 or repoze.bfg uses is fair game at that point.
You definitely don't have to use a database with Django. Whether it fits your needs, only you can tell. There are other Python web frameworks that you can use.
I'm developing software on google app engine. I'm using the webapp framework. is there any way to avoid hard-coding urls in the html templates? django provides a solution, but I couldn't find one for webapp. thanks in advance..
WebApp is really very basic.
Django url templatetag is closely tied to its idea of URL resolving by matching regex patterns. As WebApp does not have any serious URL resolver, I imagine this would be really hard task to implement something in this line.
But you are not totally out of luck. Werkzeug has very easy to understand yet powerful URL routing mechanism that allows for reversing urls. You can try to base your implementation on this one (or throw away WebApp and use Werkzeug ;), it's really worth a try).
Webapp uses Django templates so:
check out Django templates here
The Django solution is the web app solution.