Using Django for Scientific Web Applications

The MVC pattern

The MVC pattern stands for Model-View-Controller and is a way of separating the user's interaction with an application from the inner workings of the application. In a scientific application this usually means separating mathematical computations from the user interface and visualization of results. The Wikipedia definition of the MVC pattern gives a very high-level explanation of what the model, view, and controller do and mentions the fact that different web frameworks interpret the three components differently. Any web application works with a set of data and needs a user interface for the communication of data between the user and some data processing software. The classical MVC pattern introduces

For applications performing mathematical computations we find it convenient to explicitly introduce a fourth component that we call compute where the mathematical computations are encapsulated. With the MVC pattern and the compute component we have a clear separation between data (model), the way data is presented (view), the computations (compute), and the way these components communicate (controller). In a small program such a separation may look as overkill, but it pays off in more complicated applications. More importantly, the concepts of the MVC pattern are widely used in web frameworks and their documentation so one should really adapt to the MVC way of thinking.

Web frameworks often have their own way of interpreting the model, view, and controller parts of the MVC pattern. In particular, most frameworks often divide the view into two parts: one software component and one HTML template. The latter takes care of the look and feel of the web page while the former often takes the role of being the controller too. For our scientific applications we shall employ an interpretation of the MVC pattern which is compatible with what we need later on:

The model will be a Python class with static attributes holding the data. The view consists of Python code processing the model's data, calling the compute component, and specifying HTML templates for the design of the web pages.

Django generates files with names models.py and views.py so the model and view concepts are very explicit. The controller functionality in Django lies both in the views.py file and in configuration files (settings.py and urls.py). The view component consists of the views.py file and HTML templates.

Forthcoming examples will illustrate how a scientific application is split to meet the requirements of the MVC software design pattern.