Remaining

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
      Rearringing the elements in the HTML template
      User-provided validation
      Autogenerating the code
      Avoiding plot files
            PNG plots
            SVG plots
Further work
Exercises
      Exercise 1: Add two numbers
      Exercise 2: Extend the vib3_flask app
      Exercise 3: Equip the vib3_flask app with more data types
      Exercise 4: Auto-generate code from function signature
Flask resources
Remaining

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.