bottle framework with multiple files bottle framework with multiple files python python

bottle framework with multiple files


I've wanted to use a single bottle server to serve a suite of micro-applications and for a decent separation of concerns, have wanted to do what you've been looking for.

Here's how I resolved my task:

rootApp.py (Your main file)

from bottle import Bottlefrom clientApp import clientApprootApp = Bottle()@rootApp.route('/')def rootIndex():    return 'Application Suite Home Page'if __name__ == '__main__':    rootApp.merge(clientApp)    rootApp.run(debug=True)



clientApp.py (The new app needing to be merged into the suite)

from bottle import BottleclientApp = Bottle()@clientApp.route('/clientApp')def clientAppIndex():    return 'Client App HomePage'


I am not sure if this is the best way to do it, but it seems to work without complaints and saves the hassle of having to share ports between applications that could otherwise have mutual knowledge. The approach really stems out of a design preference but I'd be grateful if someone could demonstrate how/if the AppStack could be used to get the same result.


If you split your code into 10 Python modules, you’re going to do 10 imports. You can iterate with __import__:

for i in range(1, 11):    __import__('hello%d' % i)

but this doesn’t strike me as a good idea. Why would you need 10 modules with a micro-framework?


Not sure about the BEST way, but indeed mount() might be used and looks good for me (tested with python 3.6 + Bottle 0.12.13).

First of all, building one of your "sub apps" in a app1.py file:

from bottle import Bottleserver1 = Bottle()@server1.route('/')def root():    return 'Hello from sub app 1'

Then you use it in your main app:

from bottle import Bottlefrom app1 import server1 mainApp = Bottle()if __name__ == "__main__":    mainApp.mount('/appli1', server1)    mainApp.run()

Lets go:

Hit your server address: http://myServer/appli1

Let me know if you need a fully functional example.