My Introduction to Bottle.py
My first introduction to bottle.py occurred about a year ago, when I realized I wanted to move my web development process from PHP to Python. I had started using Python regularly in my day job (enterprise analytics) and was growing weary of switching languages. While PHP is the lingua franca of the web (and WordPress a pidgin version of it), Python is better suited for deep algorithm and analytics work. It’s just easier to build large number-crunching application in Python – due to factors such as decent support for functional programming, the depth of the standard library, Numpy / Scipy, and the highly readable syntax.
I started by looking at some of the python web frameworks. Django is the market leader and is the best bet for those seeking fast employment. Recruiters and hiring managers want it. There is a shortage of good developers. Seriously, go forth and prosper…
For the rest of you, Bottle.py is a great option if you already have basic knowledge of both Python and web development and want to blend them. It is simple to learn, easy to deploy, and powerful enough to run a good sized site. I think of it as very much a maker’s web framework – learn it because you want to build cool stuff. Although it has handled itself very nicely in production and does catch people’s eye in job interviews…
Bottle.py makes it easy to expose your Python functions as a web page or web service. And it accomplishes this with a mimimum amount of overhead – you wrap a bottle.py decorator around your Python function and grab relevant parameters off the url. If you need to go deeper (or do forms, uploads, etc.), you can interrogate the request object. The whole apparatus fits in a single file, so deployment is dead simple. The bottle.py community has been growing – they’ve added a number of plugins and the volume of blog articles and stack overflow items is growing rapidly.
Bottle.py Resource Collection
This site has resources to help build your first Python website using bottle.py. Since I’m assuming many of our visitors have read the project’s public content (including their excellent Official Manual & Tutorial), this collection focuses primarily on other sources.
Getting Started – Hello World and other basic activities
Here’s a quick collection of materials to help you get started using bottle.py. Most of these are relatively “light” examples (basic routes, hello world, basic AJAX services, simple template usage) but they cover the essence of the framework.
- Good intro tutorial at Giant Flying Saucer.
- Throwing this one in for the sake of diversity. A collection of Hello World Examples written in bottle and several other python frameworks (Flask, Cherrypy, Web.py)
- Another python to ajax example at ericrossi.org. Includes an example of using the built in SimpleTemplateEngine.
Hosting, Deployment, Managing The Server:
Python is very different from PHP in that you need deeper access to the server to run your application. You also have the ability, on some platforms, to move away from a script approach to keeping objects in memory – this can simplify your development process. However, you now have some new challenges….
- Let’s start with the development and demo server:
- First and most awesome thing about bottle; your localhost test server is part of your single file install. Which means, you can put it on a jump drive and and run it (with no additional configuration) from any computer with Python.
- This includes your android phone! Install Scripting Layer for Android and drop a copy of bottle into the scripting directory. Here’s a tutorial on this.
- Next, you need to start and manage your application. I wrote an overview of how we developed a bash script to handle python server monitoring and restarts…
- You will need a hosting enviroment. My web host originally wanted me to spring for a full VPS ($50/month) to run Python web applications. This prompted me to look into some lower-cost python shared hosting options for my first project.
- For students and new developers on a tight budget, I recommend you check out some free python hosting options. I’ve personally built sites using Google App Engine and have friends on Heroku. Useful tutorials include:
- Hosting bottle.py on Google App Engine (update: I’ve now got my own site on google app engine and will be doing a presentation on this next month).
- An example of hosting a bottle application on heroku
- Slide show on deploying Flask (very similar) on Heroku
Need a Place to Host Your Site?
Check out PythonAnywhere (free plan) or Webfaction (Low Cost Plan, with Free Trial).
Both are run by active members of the Python community; we did an article on them here.
Stuff We Built Using Bottle…
- Our word game solver runs on bottle behind nginx. I originally built this site as a “real world” production load test – it gets a decent amount of traffic and features an AJAX based UI with a lot of back-and-forth data exchange.
- I’ve also been playing with a web-based calculator framework based on bottle. One specific application is on this site, where we took the analytics behind our website revenue study and repackaged them into a website revenue estimator.
Other Blog Posts and Cool Projects
Here’s a collection of non-intro materials bottle materials that I found interesting…
- Web Framework Performance Shootout and microframework shootout!
- Developing a RestFul API using bottle. Includes a nice treatment of using JSON to return data to the browser (convenient default option for bottle and python dicts).
- Bottle and MongoDB play nicely with each other… It even has a mongoDB plugin.
- Another bottle web service example – wrapping a python function in a web framework.
I’ve been playing with bottle.py and trying to get my head around the method of communicating with routes etc. I’m getting there.
You mention how good it is for websites that involve analytics. I know your using wordpress as your blog site. Wouldn’t it be possible to have a bottle.py website with a subdomain called blog? Not meant to be a negative criticism.
It would be interesting to see if there are any big websites on the internet such as facebook etc who use python bottle, cherrypy, flask or django.
Great post. Thanky you very much. It really helps getting my head around python bottle instead of PHP
Absolutely – although lets take a moment to reflect on the required architecture.
First, your bottle application (or Flask, etc) should be chained behind a production server. I’ve had good luck with nginx and cherrypy; many other options exist. This creates opportunities, since you can generally route and path traffic at the production server level down into multiple apps. This is a very good thing you should happily exploit:
– Render unto Python, things which are Python’s… dynamic, custom coded, stuff that you weave together using multiple libraries
– Static content? [Not in my app!] Move this over into a “static” path on the production server; serve it directly (no Python) or move it to a CDN (for larger projects). Your production server can sling an incredible volume of static assets like art, scripts, and fixed html docs without breaking a sweat… it’s faster and more efficient (and unlikely to break). I’m a fan of serving the core of a page using static html / assets and using call-backs to serve up dynamic elements with lots of database calls; allows you to retain a visitor’s attention with a partial page w/o waiting for database calls.
– Basic Blog (WordPress works great) on a third path to support marketing & communications; not sexy from a technical perspective, but makes publishing content really simple and has a ton of supporting plugins. [this could easily be handled via a Python blogging framework if you wish]
Bottle is like an undiscovered gem. We actually prefer Bottle over Flask for simplicity, even on some pretty complicated apps. Instead of an ORM, we use direct psycopg2 sql calls in a model.py file – works great And we have been able to deploy a Bottle app behind our Nginx proxy server using uwsgi (we are unsuccessful deploying Flask). We also prefer the Bottle default template over jinja2 – again less coding.
Do you have any example using session with Bottle ?
Any example of using Bottle with session deployed with Nginx and uwsgi ?
Pretty much the same reason I latched onto it. Fast, simple deploy with minimal moving parts – even better, it works great when you need to jump across platforms in your develop/deploy process. I routinely develop on an Windows box and push it to a Linux server when I’m done.
Sessions – yes… beaker works great. Core implementation is two lines of code and session options object.
You’ve got a few of app-specific code for interrogating the session object per usage. Data can be store in files, in memory, or on a database.
As an added bonus, the beaker package also support caching if you happen to be running a high volume site.
Currently using this setup (Bottle / Beaker) to support a site that gets about 200K visits/month; front end server is nginx.