Search code examples
pythonbottle

How to split Python views in bottle.py


I currently have a big file server.py containing all my bottle functions for routing. My application could be divided into 3 applications: news, forum, and blog.

I would know there is a good way to split my big file into 4 parts: news functions, forum functions, blog functions and common utilities (decorators, text formatting and others).

My current approach is to split my functions into 5 files : server.py, blog.py, admin.py, news.py and utils.py. server.py just contains the bottle.run() and import all others views.

Is it the good way? Is there any way to load/import functions automatically WITHOUT an import *.


Solution

  • from blog import blogRoute
    from admin import adminRoute
    from news import newsRoute
    from utils import utilsRoute
    botapp = bottle.app()
    for approute in (blogRoute, adminRoute, newsRoute, utilsRoute):
        botapp.merge(approute)
    

    And in each of your python files, you just need this at the top: blog.py for example

    from bottle import Bottle
    blogRoute = Bottle()