views:

514

answers:

3

Hello, 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()
+2  A: 

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

hhafez
+8  A: 

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.

Blair Conrad
A: 

thx for the input, really appreciate them.

as for grouping imports together, i tried to keep all app-engine related imports into a .py file called ext.py

Then I called it anywhere I wanted to use it by this line:

from ext import *

ext.py contains the following:

import os

# import from appengine's libs
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

# import models
from models import *
fooyee
I noticed that when I had seperated the Page2 class from main.py, I still had to import the appengine libs at the top of the page2.py file.It's kind of redundant. Any suggestions?
fooyee
This should probably be a separate question, as we're really packing a lot of information in this one now, but...One answer would be to create variables in ext that mirror your imports. The formatting will be bad here, but something like:`import google.appengine.ext.webapp; webapp = google.appengine.ext.webapp`Then when your other modules import ext, they can refer to ext.webapp (or use the `from ext import` to just talk about webapp).
Blair Conrad