Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Outputs in Pygal

Feb 6, 2020 • 10 Minute Read

Introduction

Pygal is a very versatile library. It provides dynamic animated graphs or plots in SVG (Scalar Vector Graphics). It also provides multiple output formats that can come handy in different situations. In this guide, we will learn through examples of how to get each type of output. We will also look at how to embed pygal in a Flask app.

If you are new to pygal, please check out my previous guide on Charts in Pygal.

Types of Output

I assume you have a basic knowledge of Python, you have installed Python 3 and the pygal packaged library on your system, and you have a web browser. In this guide, we will learn how to generate output in the following formats:

  • Output as strings
    • Output in bytes
    • Output in Unicode
  • Output in file
  • Output as png
  • Output as etree
  • Output as base 64 data URI
  • Output in browser
  • Output in PyQuery
  • Output In Flask app

Let's discuss each of these in detail.

Output as Strings

Output in Bytes

This output format will provide us with vectorial output in SVG format.

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render SVG as bytes
line_chart.render()
    

Output in Unicode

This output format will also provide us with vectorial output of SVG in Unicode.

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9,None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render ouput as unicode
line_chart.render(is_unicode=True)
    

Output in file

In this output format, your file will be saved in your current directory if you do not specify the location.

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9, 10.4,  10, 5.8, 6.7, 6.8, 7.5])
# this will render output file in current directory
line_chart.render_to_file('line_chart.svg')
    

Output as Png

Before starting, we have to install the dependencies if we want to output in png.

lxml

  • pip install: To install using pip, open the terminal and run the following command:

    pip install lxml
    
    • conda Install: To install using conda, open the terminal and run the following command:

      conda install -c anaconda lxml 
      

cairosvg

    • pip install: To install using pip, open the terminal and run the following command:

      pip install cairosvg
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c conda-forge/label/cf201901 cairosvg
        

cssselect

    • pip install: To install using pip, open the terminal and run the following command:

      pip install cssselect
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c anaconda cssselect 
        

tinycss

    • pip install: To install using pip, open the terminal and run the following command:

      pip install tinycss
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c conda-forge/label/cf201901 tinycss
        
      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B vs C'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9,  10.4,  10, 5.8, 6.7, 6.8, 7.5])
line_chart.add('C',  [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 5.7, 4.8, 6.2, 66, 36])
# this will render output file in current directory
line_chart.render_to_png('line_chart.png')
    

Note: There will be no animation in rendered png.

Output as Etree

This output format will return the SVG root etree node.

      # Importing pygal library
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3, 8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_tree()
    

Output as Base 64 Data URI

      import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3, 8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_data_uri()
    

Output in Browser

Pygal can also render SVG in the browser directly using the render_in_browser command. This becomes beneficial if you are using Jupyter notebook.

      # Importing pygal library
import pygal
# creating object
xy_chart = pygal.XY(stroke=False)
# adding title
xy_chart.title = 'Correlation'
# adding random data
xy_chart.add('A', [(0, 0), (.1, .25), (.3, .1), (.5, .1), (.8, .6), (1, 1.08), (1.3, 1.1), (2, 3.23), (2.43, 2)])
xy_chart.add('B', [(.1, .15), (.14, .3), (.5, .3), (.3, .4), (.21, .21), (.5, .3), (.6, .8), (.7, .8)])
xy_chart.add('C', [(.5, .01), (.13, .02), (1.5, 1.7), (1.02, 1.6), (1.8, 1.63), (1.5, 1.82), (1.7, 1.23), (2.1, 2.23), (2.3, 1.98)])
# render file in browser
xy_chart.render_in_browser()
    

Output in PyQuery

For PyQuery, you first have to install the pyquery library on your system.

  • pip install: To install using pip, open the terminal and run the following command:
      pip install pyquery
    
  • conda Install: To install using conda, open the terminal and run the following command:
      conda install -c anaconda pyquery
    

After installing pyquery, you can get the pyquery object to wrap the chart by calling render_pyquery.

      # Importing the pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9,None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_pyquery()
    

Output in Flask

For output in a Flask app, you should have installed the Flask and virtualenv libraries on your system .

  • Flask installation

    • pip install: To install using pip, open the terminal and run the following command:
    pip install Flask
    
    • conda Install: To install using conda, open the terminal and run the following command:
    conda install -c anaconda flask 
    
    • virtualenv library installation

      • pip install: To install using pip, open the terminal and run the following command:
      pip install virtualenv
      
      • conda Install: To install using conda, open the terminal and run the following command:
      conda install -c anaconda virtualenv 
      

In the given code, we are using pygal in flask app.

      # Importing flask module in the project is mandatory 
# An object of Flask class is our WSGI application.
# We also have to import pygal library
import pygal
from flask import Flask 
  
# current module (__name__) as argument. 
app = Flask(__name__) 
    
# the associated function. 
@app.route('/')
def line_route(): 
    line_chart = pygal.Line()
    line_chart = pygal.Line()
    line_chart.title = 'A vs B vs C '
    line_chart.x_labels = map(str, range(0, 20))
    line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
    line_chart.add('B',  [91.2, 19.4, 5.3, None, None, 4, 0.4, 15, 7.8, 7, 2.8])
    line_chart.add('C',  [9.2, 19.4, 15.3, 8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
    # this will render output in flask app
    return line_chart.render_response()
  
# main driver function 
if __name__ == '__main__': 
    # run() method of Flask class runs the application  
    # on the local development server. 
    app.run()
    

Open the URL circled above and you will get your rendered SVG in flask app.

Note: For Django, you can use the command render_django_response in your code.

Conclusion

This guide was focused on the different output formats provided by pygal. In this guide, we looked at every kind of output format with examples. We also saw how to embed pygal in a Flask app. I hope that after reading this guide, you understand the different output formats and you're able to use any output format depending on your situation. If you have any questions, contact me at CodeAlphabet.