Running the Flask App

We can run the flask application (assuming the filename to be main.py) using the python main.py command in the terminal.

$ python main.py 
 * Serving Flask app 'main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

Output:

Then go to the URL http://127.0.0.1:5000 and you should be able to see the HTML template and the three Bokeh charts as shown below.

 



Responsive Chart with Bokeh, Flask and Python

In this post, we will use the Flask framework to create our web application in Python. The Bokeh library will be used to create interactive graphs and we will visualize this graph through a simple frontend HTML page. For this, we will first write the endpoints in Flask which will help us to create Bokeh charts, and then we will create HTML templates that will utilize these Bokeh charts to display them to the user.

Flask is a web framework that offers libraries for creating simple web applications in Python. It is built using the Jinja2 template engine and the WSGI tools. Flask is considered a micro-framework. Web server gateway interface, sometimes known as WSGI, is a standard for creating web applications in Python. It is regarded as the specification for the common interface between the web application and server. Jinja2 is a web template engine that renders dynamic web pages by fusing a template with a specific data source. You can install Flask using the below command:

pip install flask

For building interactive visualizations for current web browsers, the Python library Bokeh is a good choice. It enables you to create stunning visualizations, from straightforward plots to interactive dashboards with streaming datasets. Without writing any JavaScript yourself, you may build visualizations that are powered by JavaScript using Bokeh. To install Bokeh use the below command:

pip install bokeh

Steps to Follow:

  1. Create a python file ‘main.py‘ that will contain the Flask App.
  2. Create a directory named ‘templates‘ and add an HTML file ‘charts.html‘.
  3. Run the Flask App and view the output in a browser.
  4. This is the file structure we will follow:

 

Similar Reads

Flask main.py

Here, we have created three Bokeh charts (scatter plot, bar chart, and line chart). We have used a single endpoint which is the root endpoint for our Flask application. For each chart, we get the HTML components to embed the charts in our template using the components() function. It returns the script and an HTML div tag assuming that the required JS files from Bokeh are already loaded in our template through the local URL or Bokeh’s CDN. The charts will be responsive due to the attribute ‘sizing_mode=”stretch_width”‘ used in each of the figures....

HTML Template

...

Running the Flask App

To create an HTML page, we will use the most popular Bootstrap framework. It has pre-defined CSS and JS classes which will help us to create a good-looking form with minimal code. For our purpose, we are using Bootstrap and CSS along with Bokeh JS. Bokeh JS contains all the required JavaScript functions to help the graphs with the required functionalities like loading data, zooming in or out, saving the graphs, etc. Please note that anything written within double curly braces ({{…}}) is interpreted as python code within the HTML template....

Contact Us