5 ms·
Bottle doesn't have "blueprints" but I've used it for something like that in the past. IIRC you can "mount" another bottle app object into a URL path [1]. [1]
by rambojazz 7y ago
Bottle doesn't have "blueprints" but I've used it for something like that in the past. IIRC you can "mount" another bottle app object into a URL path [1].
[1] https://bottlepy.org/docs/dev/api.html#bottle.Bottle.mount https://bottlepy.org/docs/dev/api.html#bottle.Bottle.mount
- e12e 7y agoHm: > app – an instance of Bottle or a WSGI application. So, that means a require to load the code, and the some kind of app = my app.default_app()? Do you have an example of how you create an instance of a wsgi app? For bottle it looks like that's normally something bottle.run() does?
- rambojazz 7y agowsgi is an interface; if you implement it then you are compatible. The simplest way is to just use another Bottle app, or maybe subclass the Bottle class like this `class MyClass (Bottle):`, otherwise you can create a new class by implementing the interface methods (many tutorials online for this). Then you "mount" that wsgi object into a specific path using the `.mount()` function of your main Bottle app.
- e12e 7y agoRight, I get that part (sort of). But looking at the quickstart, you typically don't subclass Bottle, you require it, and call run()? So your typical bottle app won't be a subclass of bottle? At which point you can't make an instance of it, to pass to mount()? Looking at the documentation for mount() it looks like you should be able to mount the quick-start app - but it's not clear of you create an instance to pass to mount? That is if I have example.py: from bottle import route, run, template @route('/hello/<name>') def index(name): return template('<b>Hello {{name}}</b>!', name=name) run(host='localhost', port=8080) How do I write example_mount.py that mounts example.py under /hello1 and /hello2?
- rambojazz 7y agotest.py ======= import bottle @bottle.get('/') def index(): return 'INDEX' # a new wsgi app hello = bottle.Bottle() @hello.get('/<name>') def greet(name): return 'Hello, ' + name bottle.app().mount('/hello', hello) Test like this: python3 -m bottle --bind :8080 test You should have 2 working routes, "/" and "/hello/<name>".
- e12e 7y agoThanks, that makes sense. But I'm still not clear on how I require "example.py" and mount it (ie how do I make an wsgi instance from a python file that is a bottle app)?