Using Flask for Scientific Web Applications

Using Flask for Scientific Web Applications


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
      Troubleshooting
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
Exercises
      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

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 in the forthcoming text explore the very easy-to-use Flask framework. At the time of this writing, the primary Flask documentation is the official web site and the WTForms Documentation. That is why we have made specific examples for the application of Flask in computational science.

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.
Flask is very easy to work with and extensive Python knowledge is not necessary. It is in fact enough to know about functions and modules. Beyond that, some knowledge of classes 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.)