Python: Pygal Bar Chart

Along with Flask

nick3499
3 min readNov 20, 2020
screen capture of bar chart

Virtual Environment

This app was originally developed in a Python virtual environment. After Flask and Pygal are installed, this repo can basically be dropped into such a virtual environment.

flask and pygal can be installed, typically, by using pip or yarn. Another option is to install Anaconda, which becomes a separate Python distribution.

Run the Application

Navigate to the application’s root directory, then enter the following in a Unix-like terminal emulator:

$ python index.py

Then open web browser and navigate to localhost:3000 or 127.0.0.1:3000.

Shebang/Interpreter Directive

The shebang is a two-character sequence which represents the magic byte string 0x23 0x21 encoded from ASCII #!. In this case, that shebang string is followed by the path of the Python program loader /bin/python which enables the bar_graph.py text file to be used as an executable Python script in a Unix-like system. The /bin/python segment is known as an interpreter directive in a shebang line.

For users running Windows, the shebang line can be changed to #!/usr/bin env python.

Documentation

In the Python interactive shell, the documentation can be accessed:

>>> __doc__
'Renders bar graph to web page based on Fibonacci sequence.'
>>> render_bar_chart.__doc__
'Renders bar graph to web page based on Fibonacci sequence.'

The module and method documentation are identical because render_bar_chart() is the only method.

Imports

from flask import Flask
from flask import render_template
import pygal
  • Flask() implements a WSGI application (pronounced whiskey) which is a simple calling convention for web servers to forward requests to web applications…written in Python.
  • render_template Renders a template from the template folder with the given context.
  • Pygal is a charting module.

Flask Instance

Instantiate a Flask instance, which acts as the central object:

APP = Flask(__name__)

Function Decorator

@APP.route('/')

This decorator modifies the behavior of render_bar_chart() by assigning a path to that function. '/' indicates that the root path was assigned. A user can visit the APP’s domain by loading 127.0.0.1:3000 or localhost:3000 in their web browser. Basically, this becomes a testing server in development mode.

Function

def render_bar_chart():
bar_chart = pygal.Bar(height=300)
bar_chart.title = 'Fibonacci sequence'
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
chart = bar_chart.render_data_uri()
return render_template('index.htm', chart=chart)
  • def render_bar_chart(): defines the view function which executes the following instructions.
  • bar_chart = pygal.Bar(height=300) instantiates Pygal’s Bar class. The height attribute shrinks down the graph so that the entire SVG image can be viewed.
  • bar_chart.title = 'Fibonacci sequence' sets chart’s title.
  • bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) adds Fibonacci values to chart. The chart’s data.
  • chart = bar_chart.render_data_uri() renders chart.
  • return render_template('index.htm', chart=chart) renders Jinja2 template to the web browser. The chart object is passed to render_template().

If Statement

if __name__ == '__main__':
APP.run(host='localhost', port=3000)

If this module runs as a standalone application, then its name in the namespace will be '__main__':

>>> __name__
'__main__'

Otherwise, if this module was to be imported into another application, its name would not be __main__.

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet