Creating Subprojects in Bottles

I am just starting Python web development and have chosen Bottle as my framework of choice.

I am trying to have a project structure that is modular, as I can have a β€œmain” application with modules built around it, where these modules can be turned on / off during installation (or β€œon the fly” if possible ... not sure , how would I install it).

My "main" class is as follows:

from bottle import Bottle, route, run from bottle import error from bottle import jinja2_view as view from core import core app = Bottle() app.mount('/demo', core) #@app.route('/') @route('/hello/<name>') @view('hello_template') def greet(name='Stranger'): return dict(name=name) @error(404) def error404(error): return 'Nothing here, sorry' run(app, host='localhost', port=5000) 

My "subproject" (i.e. module) is as follows:

 from bottle import Bottle, route, run from bottle import error from bottle import jinja2_view as view app = Bottle() @app.route('/demo') @view('demographic') def greet(name='None', yob='None'): return dict(name=name, yob=yob) @error(404) def error404(error): return 'Nothing here, sorry' 

When I go to http://localhost:5000/demo in my browser, it shows error 500. Exit from the bottle server:

 localhost - - [24/Jun/2012 15:51:27] "GET / HTTP/1.1" 404 720 localhost - - [24/Jun/2012 15:51:27] "GET /favicon.ico HTTP/1.1" 404 742 localhost - - [24/Jun/2012 15:51:27] "GET /favicon.ico HTTP/1.1" 404 742 Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.9-py2.7.egg/bottle.py", line 737, in _handle return route.call(**args) File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.9-py2.7.egg/bottle.py", line 582, in mountpoint rs.body = itertools.chain(rs.body, app(request.environ, start_response)) TypeError: 'module' object is not callable 

Folder structure:

 index.py views (folder) |-->hello_template.tpl core (folder) |-->core.py |-->__init__.py |-->views (folder) |--|-->demographic.tpl 

I have no idea what I'm doing (wrong) :)

Does anyone know how to do this / should be done?

Thanks!

+6
source share
1 answer

You pass the kernel module to mount (). Instead, you should pass the application object for the bottle to the mount () function, so the call will be like this.

 app.mount("/demo",core.app) 

Here are the formal docs for the mount () function.

 mount(prefix, app, **options)[source] 

Connect the application (check box or simple WSGI) to a specific URL prefix.
Example:

 root_app.mount('/admin/', admin_app) 

Parameters:
prefix - path prefix or mount point. If it ends with a slash, this slash is required.
application - instance of the Bottle or WSGI application

+8
source

Source: https://habr.com/ru/post/918893/


All Articles