Example of Chartkick with Flask drawing report

Source: Internet
Author: User
Tags min

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))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.