Notes on Ubuntu (Linux) computing

Brief notes on installation, setup and other tasks in the Ubuntu world

Posts Tagged ‘CherryPy

Install CherryPy on Ubuntu 12.04

leave a comment »

CherryPy “is a pythonic, object-oriented web framework”. My goal is to use the package to make a simple desktop, browser-based application for looking at the output of statistical inference routines I’ve developed elsewhere — we’ll see if this works (I’m not sure yet).

In any case, the first step is to install with pip:

sudo pip install CherryPy

This should install without issue. There are tutorial scripts located at /usr/local/lib/python2.7/dist-packages/cherrypy/tutorial/ that you should be able to run. For example, the `hello world’ example can be started typing the following at you terminal:

python /usr/local/lib/python2.7/dist-packages/cherrypy/tutorial/tut01_helloworld.py

This will start up the web service and the resulting output can be seen in your browser by going to http://localhost:8080. To exit, close the web browser and use a Cntrl-C at the terminal to stop the server.

As I said above, I’d like to be able to read the contents of local files and display them in a browser. If a local file is called numbers.dat and contains the following:

col1,col2
1,5
2,6
3,2
4,7

We can use the following script to display the contents of the above file:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#

"""A super simple example of reading a file and displaying results with
CherryPy.

"""
import cherrypy

class ReadFile(object):
    def index(self):
        page = "<html><body>\n"
        page += "<p>Read file: <i>numbers.dat</i></p>\n"

        with open("numbers.dat", "r") as f:
            for line in f.readlines():
                try:
                    col1, col2 = line.strip().split(',')
                    page += "<p>{:s} -- {:s}</p>\n".format(col1, col2)
                except:
                    pass

        page += "</body></html>"

        return page

    index.exposed = True

# start CherryPy
cherrypy.quickstart(ReadFile())

For more detailed documentation and tutorials, see the CherryPy website.

Written by Chris S

January 23, 2014 at 9:49 pm