Chartkick is a charting tool that features a beautiful, Easy-to-use UI, and supports most browsers, including IE6. Chartkick can draw JavaScript reports, the interface is more beautiful, its support to load the Google charts and Highcharts graphics library, and support the integrated Django, FLASK/JINJA2 framework. Flask is a lightweight web application framework that is written using Python. Based on the Werkzeugwsgi Toolbox and the Jinja2 template engine. Use BSD authorization.
Flask + Chartkick need to install the chartkick.py module first. Here is a demo implementation of the graphical interface:
The code structure is as follows:
[Root@361way chartkick]# Tree
.
├──run.py
├──static
│├──chartkick.js
│├──highcharts.js
│└──jquery.min.js
└──templates
└──index.html
First, run.py
The code is as follows:
[Root@361way chartkick]# Cat run.py
From flask import flask, jsonify, render_template, request
Import Chartkick
#app = Flask (__name__, static_folder=chartkick.js ())
App = Flask (__name__)
App.jinja_env.add_extension ("Chartkick.ext.charts")
@app. Route ('/')
@app. Route ('/index ')
def index ():
data = {' Chrome ': 52.9, ' Opera ': 1.6, ' Firefox ': 27.7}
Return render_template (' index.html ', data=data)
if __name__ = = "__main__":
App.run (host= "0.0.0.0", Debug=true)
Second, index.html
The template code is as follows:
[Root@361way chartkick]# Cat run.py
From flask import flask, jsonify, render_template, request
Import Chartkick
#app = Flask (__name__, static_folder=chartkick.js ())
App = Flask (__name__)
App.jinja_env.add_extension ("Chartkick.ext.charts")
@app. Route ('/')
@app. Route ('/index ')
def index ():
Value = {' Chrome ': 52.9, ' Opera ': 1.6, ' Firefox ': 27.7}
Return render_template (' index.html ', data=value)
if __name__ = = "__main__":
App.run (host= "0.0.0.0", Debug=true)
[Root@361way chartkick]# Cat templates/index.html
<!--<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" ></script>
<script src= "Http://code.highcharts.com/highcharts.js" ></script>-->
<script src= "{{url_for (' static ', filename= ' Jquery.min.js ')}}" ></script>
<script src= "{{url_for (' static ', filename= ' Highcharts.js ')}}" ></script>
<script src= "{{url_for (' static ', filename= ' Chartkick.js ')}}" ></script>
{% Bar_chart data with style= "width:200px; height:20px;"%}
{% Pie_chart data%}
Because the test host can not be on the network, so here will be the JS file downloaded to the local, and configured as a url_for path.
After the Python run.py runs, the http://127.0.0.1:5000 can be seen in the browser to see the effect of the screenshot above.
Third, garbled problem
The use of Chinese data will encounter garbled problem, the solution is to use the JSON dumps method to convert dict or list to the normal display of Chinese strings. As follows:
#先import JSON module
Return render_template (' index.html ', data=value)
Replaced by
Return render_template (' index.html ', data=json.dumps (value, encoding= ' Utf-8 ', indent=4))