Wednesday, July 23, 2014

Simple server

I've been working on a new demonstration project that I'd like to tell you about.

When I want to run a bioinformatics script on a sequence file, I just go to the command line and do something like:

python infilename arg1 arg2 > results.txt

But suppose instead the scenario is that I have co-workers who just will not do this, and they pester me until I run an analysis for them.  I'd like to explore an alternative approach:  set up a simple web server that will guide the command-line-phobic user through the process of choosing a script and a sequence file and also entering the appropriate options for that script.  We can use html forms to do the work.

I've written a skeleton web server (using the Flask framework and the development server that comes with it) as a proof of concept.  It is not a web server in the usual sense and it's not actually exposed to the web.  It is designed to be run in the background on a single machine, with the permissions of the user, and can read and write files with those permissions.  The "server" could be configured to startup on Launch, if desired, so the user would never even be aware of that.

I've put what I have so far on github:

git clone git://

As currently configured, the python scripts don't actually do anything except read the options data and return a nice image.  However, the web pages are wired up together. The command line output shows that all the data is flowing through as desired.  Here is a screenshot of the index page:

What you would need to do to run the demo is to use pip to install the Flask framework.  Many people use virtualenv to set up something like this, and I did that for my first run through with this flask tutorial.

However, my setup is a separate python from the System python, installed with Homebrew, and I can easily replace it at any time.  I don't worry about the risks of experimentation, and I don't have so much stuff there that I worry about conflicts.  The new Python comes first in my $PATH:

> which python
> which pip
> pip install flask

I'm not actually using the WTForms in the current version of the project, but if you did want them you would need flask-wtf.  I did not install any of the other extensions for this project.  With flask available from python, and the project downloaded, just cd into the project directory and do

> ./
 * Running on
 * Restarting with reloader

Point your browser at localhost:5000 and you should see the index page.

The python code to render the form is simple:

The form itself is built from two templates, a base template that provides the header, and this:

The form data is routed back to the correct url/route/function to start the next steps (by "action=/prog_request").

The main issues are:  enforcing that the user provide a filename and make a choice of script, showing an options page with appropriate options for that script, and then dispatching correctly.  I'll have something to say about those in another post.

I am also just getting started with git, so for reference here is what I did to get this up on github.  I got a free account (and Homebrew installed git), and configured my editor.  That's important because the git commit step requires a commit message, and the default editor that comes up is vim, which I don't know how to use.  (Yes, I know "everyone" loves it).

git config --global core.editor TextMate

then from the scripter directory

git init
git add .
git commit
git remote add origin
git push -u origin master

github prompts for my credentials and then says the magic words which mean success.  One more task for the future:  setup ssh to do the push.