Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 521173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:13:20+00:00 2026-05-13T08:13:20+00:00

I have a question about splitting up a main.py file. right now, I have

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:13:20+00:00Added an answer on May 13, 2026 at 8:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.