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.
- I did an introductory presentation (slides here) about building a site with bottle.py at PyAtl. The talk includes code for writing a simple AJAX application using bottle and JQuery.
- 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:
Editorial Comment: The free plan on the sites listed above is great for demo projects; however, if you’re looking to build a business or long term professional portfolio, I highly recommend you go ahead and buy a domain and hosting. The cost is small and there are several strategic advantages (SEO, subdomains) to having full control of the site. This is discussed in detail in the article I wrote aboutusing Python shared hosting.
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.