Forums

Why application = default_app() to launch Bottle and how to use debug and reload?

Hi,

I created a Bottle Web app through the automated process, and I noticed the default main.py file it generates uses the following line to launch the application:

application = default_app()

When I toy around with Bottle on my local system, I just use the run method to start the app, but that doesn't work here:

run(host='0.0.0.0', port=80, debug=True, reloader=True)

In fact, I can't even change the name of the application variable or call default_app() directly to start the app, it has to be application = default_app() in order to work.

The Bottle docs aren't very helpful in explaining what default_app is, but after digging around in the Deployment chapter of the docs, I found a section explaining how to deploy Bottle using Apache:

APACHE MOD_WSGI

Instead of running your own HTTP server from within Bottle, you can attach Bottle applications to an Apache server using mod_wsgi.

All you need is an app.wsgi file that provides an application object. This object is used by mod_wsgi to start your application and should be a WSGI-compatible Python callable.

File /var/www/yourapp/app.wsgi:

import os
# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))

import bottle
# ... build or import your bottle application here ...
# Do NOT use bottle.run() with mod_wsgi
application = bottle.default_app()

The Apache configuration may look like this:

<VirtualHost *>
    ServerName example.com

    WSGIDaemonProcess yourapp user=www-data group=www-data processes=1 threads=5
    WSGIScriptAlias / /var/www/yourapp/app.wsgi

    <Directory /var/www/yourapp>
        WSGIProcessGroup yourapp
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>
</VirtualHost>

Notice the comment "Do NOT use bottle.run() with mod_wsgi".

If you can't use run with Apache, is there any other way to get the automatic reloader and debugger to work?

If not, maybe it would be helpful to include a similar comment in main.py, notifying users that Apache deployment is used on Python Anywhere and that run and any of its parameters people may be used to are unavailable?

bottle.run runs the development web server, which is not what you want when you're running the app in an application container like we do. default_app is the main WSGI application that bottle created based on your code. The automatic reloader would not work, because that relies too much on the development web server that is not being used. Debug should work as it normally does.

We do not use Apache. We use uwsgi, which provides a standard WSGI environment for your web app.