Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created November 20, 2020 22:43
Show Gist options
  • Save nick3499/f64237cb54000c097317b55481226cfe to your computer and use it in GitHub Desktop.
Save nick3499/f64237cb54000c097317b55481226cfe to your computer and use it in GitHub Desktop.
Pygal Bar Chart: Python, Flask, pygal, HTML, Jinja2, decorator
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bar Chart: Fibonacci</title>
</head>
<body>
<!-- bar chart -->
<div><embed src={{ chart|safe }} /></div>
</body>
</html>
#! /bin/python
'''Renders bar graph to web page based on Fibonacci sequence.'''
from flask import Flask
from flask import render_template
import pygal
APP = Flask(__name__) # Flask instance
@APP.route('/')
def render_bar_chart():
'''Renders bar graph to web page based on Fibonacci sequence.'''
bar_chart = pygal.Bar(height=300) # instance of Bar class
bar_chart.title = 'Fibonacci sequence' # title of bar chart
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) # add fibo data to chart
chart = bar_chart.render_data_uri() # render bar chart
return render_template('index.htm', chart=chart) # render Jinja2 template
if __name__ == '__main__':
APP.run(host='localhost', port=3000) # if standalone
@nick3499
Copy link
Author

nick3499 commented Nov 20, 2020

Original repo: https://github.com/nick3499/pygal_bar_chart

#Python #Flask #Pygal #ChartingLibrary #Chart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment