Chapter 8. Integrating Matplotlib with Web Applications
Web-based applications (web apps) offer multi-level advantages. First, users can enjoy a unified experience across platforms. Second, since an installation process is not required, users can enjoy a simpler workflow. Lastly, from the perspective of developers, the development cycle can be simplified as less platform-specific code has to be maintained. Given these advantages, more and more applications are being developed online.
Owing to the popularity and flexibility of Python, it makes sense for web developers to use Python-based web frameworks such as Django and Flask to develop web applications. In fact, Django and Flask ranked 6th and 13th out of 175 respectively in terms of popularity, according to http://hotframeworks.com/. These frameworks are batteries included. From user authentication, user administration, and content management to API design, these frameworks have got them all covered. The code base is closely reviewed by...
Docker allows developers to run applications in self-contained and lightweight containers. Since its introduction in 2013, Docker has quickly gained popularity among developers. At the center of its technology, Docker uses the resource isolation methods of the Linux kernel instead of a full-blown virtualization hypervisor to run applications.
This enables easier development, packaging, deployment, and management of code. Therefore, all code development work in this chapter will be conducted using a Docker-based environment.
There are two ways to install Docker on Windows: the aptly named Docker for Windows package, and Docker Toolbox. I recommend stable versions of the Docker Toolbox because Docker for Windows requires Hyper-V support in 64-bit Windows 10 Pro. Meanwhile, Docker for Windows is not supported by older versions of Windows. Detailed installation instructions can be found at https://docs.docker.com/toolbox/toolbox_install_windows/, but we...
Django is a popular web framework that is designed to simplify the development and deployment of web applications. It includes loads of boilerplate code for everyday tasks, such as database model management, frontend templating, session authentication, and security. Django was built around the Model-Template-View (MTV) design pattern.
The model is perhaps the most critical component of MTV. It refers to how you represent your data in terms of different tables and attributes. It also abstracts away the nitty-gritty details of different database engines such that the same model can be applied to SQLite, MySQL, and PostgreSQL. Meanwhile, the model layer of Django would expose engine-specific parameters, such as ArrayField
and JSONField
in PostgreSQL, for fine-tuning of your data representation.
The template is similar to the role of a view in the canonical MTV framework. It handles the presentation of data to the users. In other words, it doesn't handle the logic of how the...
Showing Bitcoin prices using Django and Matplotlib
We have now established a fully fledged website backbone using just a few commands. I hope you will appreciate the simplicity of using Django for web development. Now, I will demonstrate how we can integrate Matplotlib charts into a Django site, which is the key topic of this chapter.
An app in the Django ecosystem refers to an application that handles a specific function within a site. For instance, our default project comes with the profile and account apps already. With the terminology clarified, we are set to build an app to display the latest price of bitcoin.
We should keep our development server running in the background. When the server detects any changes to our code base, it will reload automatically to reflect the changes. Therefore, right now, we need to launch a new Terminal and attach to the running server container:
docker exec -it 377bfb2f3db4 bash
The strange-looking numbers before bash
refer to the ID of...
In this chapter, you learned about a popular framework made to simplify the development and deployment of web applications, called Django. You further learned about integrating Matplotlib charts into a Django site.
In the next chapter, we will cover some useful techniques to customize figure aesthetics for effective storytelling.