I am building a web app with google app engine with python as well as HTML and CSS and I have a bunch of pages (around 15) that I want to make that will all somewhat different though will have a similar purpose. Is there any way to only have a few handlers that can create a bunch of pages or do I need a different class in python for every page?
I know this question is somewhat vague without context for the pages but really any information about how to create multiple pages without coding entirely new handlers and HTML for them would be a huge help.
No you don't need a different class for each page.
You can redirect every request to a singe script with app.yaml
handlers:
- url: /.*
script: dispatcher.app
Then, from dispatcher.py you can redirect every request to a single RequestHandler and program all your logic there, like serving a different jinja2 template for different URLs dinamically.
The URL Mappings documentation explains how to redirect multiples URLs to one RequestHandler. For example:
class BrowseHandler(webapp.RequestHandler):
def get(self, category, product_id):
# Display product with given ID in the given category.
# Map URLs like /browse/(category)/(product_id) to BrowseHandler.
application = webapp.WSGIApplication([(r'/browse/(.*)/(.*)', BrowseHandler)
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
It will really depend on what framework you are using (if at all), but what you are looking for is templating systems. Here is a good list of frameworks and templating systems.
It really depends on what you're trying to do, but if you're using webapp or webapp2, you can use one handler. Check the Request parameter's url field to find out which page the user is requesting.
Related
I want to use Django REST framework for my new project but I am not sure if I can do it efficiently. I would like to be able to integrate easily classical Django app in my API. However I don't know how I can proceed to make them respect the REST framework philosophy. Will I have to rewrite all the views or is there a more suitable solution?
"Normal" Django views (usually) return HTML pages.
Django-Rest-Framework views (usually) return JSON.
I am assuming you are looking for something more like a Single page application.
In this case you will have a main view that will be the bulk of the HTML page. This will be served from "standard" Django view returning HTML (which will likely include a fair bit of JavaScript).
Once the page is loaded the JavaScript code will makes requests to the DRF views. So when you interact with the page, JavaScript will request Json, and update (not reload) the page based on the contents of the JSON.
Does that make sense?
I am not sure what would be the best route to go down, or I may be missing something obvious.
Example I can give is I have 'person' model and associated form, and view created to add a new 'person'. This all works great. What I would like to do though is use this 'view/form' in a master page with other similar 'views/forms'. With each part being able to add/edit or delete a record from each sub view/form.
So I have all functionality done, just don't know how I can create this master page with child objects, but these child objects can be their own page as well, type of thing.
The idea being that the master page structure is flexible and can accommodate various elements based on the context the user is in.
Should I be looking at blueprints or Jinja2 and its template structure. Or is it how I am handling routes within the main app.
Apologies if this is too vague.
I have done this using AngularJS ng-include directive. You can include whatever html you want, but be careful with Jinja2. If the html you are trying to include contains any script tag it will crash. See my question here. If you need to import a form that needs a script tag you will need to make sure it is not loaded when you are pushing it with Angular. Since it makes a xhr request, you can use flask.request.is_xhr to check if it is angular or the user that is requiring the form. You cannot forget to add this to your angular app
Otherwise is_xhr will always return false.
myAppModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
Be careful with base_ templates as well, since they usually load script tags. You can pass the base template through your flask route, and make it extend a blank base html when the request is made through angular. I wish I had my example here, but I am on the bus. Let me know how far you got.
I am working on a small task.
the task requires me to process the data from a html form. the html form consists of a phone dial pad. the data is the phone number the user enters. I need to process the data. The functionality for processing of phone number (data) has been completed.
But i have a problem. I am used to django framework. But for this task, the database is not needed and i do not need a admin page as well. In short, i do not want to use a framework for a single web page.
Is there any possible ways for me to create a simple html form, and when i submit the form the data is transferred to the python code for processing.
I have been looking over through the internet, and i am not finding any suitable ways of implementing it. can you please help me out or point me in the right direction
I don't have an exact idea about Python Flask but you'll have to do the something similar to the following
from flask import Flask
app = Flask(__name__)
#app.route("/")
def calculation():
#manipute request here as per your
#pass manipulated data to the processing functionality already present
if __name__ == "__main__":
app.run()
Suppose this code now runs at http://localhost/, in HTML code, you'll have to write a form processing JavaScript code that'll parse the form. You can use any method .serialize() or anything else depending on what you are comfortable with. Then make an Ajax call to the running Flask code and to it pass the request parameters.
You can then probably host this Flask code on Heroku or something similar. Here is method that's creating RESTful web API using Flask. You can read through it and understand the workings of it. http://blog.luisrei.com/articles/flaskrest.html
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 really new to Python and GAE. I'm setting up a basic CRUD app for some test data and am trying to get some routing for the admin pages going. I'd like to use the same page for creating and editing an object. So basically I want:
/admin/edit/<id>
where <id> is optional and /admin/edit will route to the same page. I tried adding <id:\w*> to the route which then allowed me to hit the page without supplying an id, but then when I supplied the id, I received a 404. Then I tried <id:\w+> and got a 404 with and without an id. I'm not having much luck.
Can anyone help me with what regex I need for this?
You can set up a regex to parse IDs out of the URL. Here's a really premitive example using webapp2:
app = webapp2.WSGIApplication([('/', MainPage),
('/property/(.*)', PropertyHandler)],
debug=True)
And you setup your request handler to accept the additional parameter:
class PropertyHandler(webapp2.RequestHandler):
def get(self, propertyId):
For a real-world implementation, you'd want to be a bit more specific on the regex and add validation to the handler incase you get garbage or no ID.