Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine - How do I split code into multiple files? (webapp)

I have a question about splitting up a main.py file.

right now, I have everything in my main.py. I have no other .py files. And I always have to scroll long lines of code before reaching the section I wish to edit.

How do I split it up? (i'm going to have more than 20 pages, so that means the main.py will be HUGE if I don't split it up.

PS: also, I noticed that I had to setup the template values, template path, and call template.render each time. Any way of shortening them all?

Code:

    # everything here in main.py
class MainPage(webapp.RequestHandler):
    def get(self):
        # Models are queried here, results transferred to template_values

        template_values = {
            'value1': value1,
            'value2': value2,
            'value3': value3,
            }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class Page2(webapp.RequestHandler):
    def get(self):
        # Models are queried here, results transferred to template_values

        template_values = {
            'value1': value1,
            'value2': value2,
            'value3': value3,
            }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class Page3(webapp.RequestHandler):
    def get(self):
        # Models are queried here, results transferred to template_values

        template_values = {
            'value1': value1,
            'value2': value2,
            'value3': value3,
            }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))


application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/page2', Page2)
                                      ('/page3', Page3)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
like image 961
fooyee Avatar asked Dec 27 '09 20:12

fooyee


People also ask

How do you execute a web app in a shared deployment?

When a web app in a shared is deployed, choosing to "execute as you" causes the web app to execute under the authority of the user that deployed it (since there is no script owner). Embedding your web app in Google Sites You can also embed web apps in both the classicand new versions of Google Sites.

How can I simulate a multi-page application using Apps Script?

It can be desirable to have an Apps Script web app simulate a multi-page application, or one with a dynamic UI controlled via URL parameters. In order to do this well, you can define a state object to represent the app's UI or page, and push the state into the browser history as the user navigates your app.

What is Google App Engine?

With zero-config deployments and zero server management, App Engine allows you to focus on writing code. Plus, App Engine automatically scales to support sudden traffic spikes without provisioning, patching, or monitoring. Below is a sample reference architecture for building a simple web app using App Engine and Google Cloud Platform.

Is it possible to break a web application into smaller components?

Nothing stops you from taking advantage of this to break your web application into smaller web applications, for example one that responds to yoursite.com/music, the other to yoursite.com/movies. They can be totally different apps, developed by different teams, different code bases.


2 Answers

Splitting the code is no different than splitting code for any Python app - find a bunch of related code that you want to move to another file, move it to that file, and import it into the main handler file.

For example, you could move the Page2 code to page2.py, put

import page2

at the top of your file, and change your mapping to load /page2 from page2.Page2 (you might want to rename these classes in this case...

Alternatively, you could have separate .py files handle different (groups of) pages by editing the app.yaml file as described in Script Handlers.

You can wrap your template-handling code in a convenience function and call it, to reduce repeated code a little bit. You may be able to streamline the loading of the template values, but once you want to render, you could call a method something like

def render(template_file, template_values):
    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, template_values))

It's not much of a savings, but it's a little more readable. Probably you'd want to move render to a different file and import it where you want it.

like image 168
Blair Conrad Avatar answered Oct 17 '22 18:10

Blair Conrad


Define your classes in other .py files and use "import" to use them in your main.py. It is quite simple actually.

like image 24
hhafez Avatar answered Oct 17 '22 17:10

hhafez