Using Web Frameworks for Scientific Applications

Hans Petter Langtangen [1, 2]
Anders E. Johansen [1]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Oct 11, 2015

Table of contents

Web frameworks
      The MVC pattern
      A very simple application
      Application of the MVC pattern
Making a Flask application
      Programming the Flask application
      Equipping the input page with output results
      Splitting the app into model, view, and controller files
Making a Django application
      Setting up a Django project
      Setting up a Django application
      Programming the Django application
      Equipping the input page with output results
Handling multiple input variables in Flask
      Programming the Flask application
      Implementing error checking in the template
      Using style sheets
      Using LaTeX mathematics
      Rearranging the elements in the HTML template
      Bootstrap HTML style
      Custom validation
      Avoiding plot files
      Plotting with the Bokeh library
      Autogenerating the code
      User login and storage of computed results
      Uploading of files
Handling multiple input variables in Django
      Programming the Django application
      Custom validation
      Customizing widgets
      Exercise 1: Add two numbers
      Exercise 2: Upload data file and visualize curves
      Exercise 3: Plot a user-specified formula
      Exercise 4: Visualize Taylor polynomial approximations
      Exercise 5: Extend the gen app
      Exercise 6: Make a web app with multiple apps
      Exercise 7: Equip the gen app with more data types
      Exercise 8: Auto-generate code from function signature
      Project 9: Interactive function exploration
      Flask resources
      Django resources

Web frameworks

Computational scientists may want to offer their applications through a web interface, thereby making a web application. Basically, this means that users can set input data to the application on a web page, then click on some Compute button, and back comes a new web page with the results of the computations. The web interface can either be used as a GUI locally on the scientist's computer, or the interface can be depolyed to a server and made available to the whole world.

Web applications of the mentioned type can be created from scratch using CGI scripts in (e.g.) Python, but the code quickly gets longer and more involved as the complexity of the web interface grows. Nowadays, most web applications are created with the aid of web frameworks, which are software packages that simplify the programming tasks of offering services through the Internet. The downside of web frameworks is that there is a significant amount of steps and details to learn before your first simple demo application works. The upside is that advanced applications are within reach, without an overwhelming amount of programming, as soon as you have understood the basic demos.

We shall explore two web frameworks: the very popular Django framework and the more high-level and easy-to-use framework Flask. The primary advantage of Django over other web frameworks is the rich set of documentation and examples. Googling for "Django tutorials" gives lots of hits including a list of web tutorials and a list of YouTube videos. There is also an electronic Django book. At the time of this writing, the Flask documentation is not comparable. The two most important resources are the official web site and the WTForms Documentation. There is, unfortunately, hardly any examples on how Django or Flask can be used to enable typical scientific applications for the web, and that is why we have developed some targeted examples on this topic.

A basic question is, of course, whether you should apply Flask or Django for your web project. Googling for flask vs django gives a lot of diverging opinions. The authors' viewpoint is that Flask is much easier to get started with than Django. You can grow your application to a really big one with both frameworks, but some advanced features is easier in one framework than in the other.

The problem for a computational scientist who wants to enable mathematical calculations through the web is that most of the introductory examples on utilizing a particular web framework address web applications of very different nature, e.g., blogs and polls. Therefore, we have made an alternative introduction which explains, in the simplest possible way, how web frameworks can be used to

  1. generate a web page with input data to your application,
  2. run the application to perform mathematical computations, and
  3. generate a web page with the results of the computations.
To work with Django, you need to know about Python packages and modules as well as Python classes. With Flask it is enough to be familiar with functions and modules, though knowledge of classes and a bit of decorators might be an advantage.

All the files associated with this document are available in a GitHub repository. The relevant files for the web applications are located in a subtree doc/src/web4sa/src-web4sa/apps of this repository.

Other frameworks

Our introductory examples were also implemented in the web2py framework, but according to our experience, Flask and Django are easier to explain to scientists. A framework quite similar to Flask is Bottle. An even simpler framework is CherryPy, which has an interesting extension Spyre for easy visualization of data. Once you know the basics of Flask, CherryPy is easy to pick up by reading its tutorial. (There are some comments on the Internet about increased stability of Flask apps if they are run on a CherryPy server.)

© 2015, Anders E. Johansen, Hans Petter Langtangen. Released under CC Attribution 4.0 license