Learning Python3 Flask Part 3

Pyecharts & Echarts

Posted by Riino on July 11, 2020 Python Flask Server Web html visualization


Not Recommend?

Pyecharts is a python library helps you to generate echart-standard html clip or echart-standard options(which means a json package to configure the echarts tag in your html page), depends on which render method is used.

First, I have to declare that I DO NOT recommend Pyecharts, at lease in 1.8.0 version , which is the one I used, and the pyecharts part of this article may be out-dated because of several reasons below:

  1. Pyecharts is not a stable python library, the method of Pyecharts is still updating, and in most cases the old methods, even the style of using corresponding APIs are recommend by Pyecharts differently.

  2. Pyecharts still does not perform very well in ‘freedom’ of using API and data format, especially when you were in Echarts area.

    For example, by looking around the source code, we can find that the function inside do not accept users to set more than 2 parameters in creating visualMap attribution, because the function in pyecharts itself is like “def func(name=a,value=b)”.And yes you can make b a matrix, but it will make things tough when you trying to use dimension parameters in echarts.

However, pyecharts do have some advantages:

  1. Allows you to create echarts diagrams in few steps.
  2. It’s in python so you don’t need javascript , unless you want more features in dynamic diagram or edit the event in charts.
  3. You even don’t need to write html , or any other code about the front-end page.

Overall, in my opinion: if you are not majoring in programming, or it’s not your major job or career, and you just learnt some basic python and want to get the feedback as soon as possible, and you don’t need to spend much time debugging and coding, your boss didn’t require too much in these charts, then just use Pyecharts , AND CONFIRM THE VERSION OF THE TUTORIALS YOU ARE READING. But, if you are a programmer, and you know a lot about javascript, python, and you have enough time to read echarts’ doc, and you want a strong webpage charts with ajax, user event, dynamic freshing.. , then pyecharts is not for you.

Basic Syntax(Ver 1.8.0)

Warning: The approach here is just the style of 1.8.0 version, so I just recommend you to take a look, for the latest details please visit official website

Basically any type of charts can be created like this style, please be careful that different types of from

A simple bar chart example

    c = (
            .add_yaxis("Name", your_data)
            .add_yaxis("Name", your_data)

A little complex Map with COVID-19 visualization example with custom style, tooltip format, and visualMap settings

    c = (
        MapGlobe(init_opts=opts.InitOpts(width="100%", height="100%", theme=ThemeType.DARK))
            .add("geo", map_data)

                range_size=[5, 1000],
                    var tool_tip =
                    "<br/>Comfirmed:" + parameter.value[2][0] +
                    return tool_tip;
            # title_opts=opts.TitleOpts(title=country_name)

Render Methods

Noticed that we created a variable named c above, and we can using the render method of c to get the chart itself, or its options.

There’re 3 types of rendering your charts when using pyecharts, but keep it in mind that the final result are all just CSS+js+HTML with echarts, pyecharts just help you generate those front-end codes:

Embedded Render

Remember the ‘variable’ in jinja template? (Visit last blog of flask)

Like assign any variable in Jinja, you just put your c in a function with @app.route in your views.py:

    return render_template(

And in your html, just use :


This will actually generate a html clip which draws an echart diagram, with all js dependencies inside, but the source ur of all those dependencies comes from pyecharts’server, so you may meet speed issue.

Render Echarts Options(For ajax)(Weak Support from Pyechart)

Another way is to just use pyecharts to create a json package, which is the “Options” config in echarts, any json with echarts’ standard can be recognized by echarts in html. To do this, we need to render our c as an option:

return c.dump_options_with_quotes()

TIP: You can’t use dump_options_with_quotes() in pyecharts layout classes like Grid, Page, etc.

To draw multiple charts, you need implement this procedure individually in every single charts when using ajax to make front-end and back-end works independently.

<div class="PyechartMap" id="myechart" >

And you can use CSS to config the style of this div, including width and height.

Now, we have to write a js script to let the js know these things:

  1. Target div tag should be regarded as a echart.
  2. Send ajax, and the response body contains a json, which can be used to generate target echart.

For the step 1, we can import echart.js and assign target div, notice that in this case, we should use Pyecharts’ Echart js dependency, and I DO NOT recommend you to set the url as the pyecharts’ url, actually you can download these js in: https://github.com/pyecharts/pyecharts-assets and put them into your static folder.

    src="{{ url_for('static', filename='js/world.js') }}"
  src="{{ url_for('static', filename='js/echarts.min.js') }}"

Now you may ask: What if I don’t know the name of required js file? (Caution: This only in 1.8.0 level )

For example, if you are drawing a map, and you don’t know specific js file to support this map, then you can use c’s member : js.dependencies

And yes, the dev team of Pyecharts said that there’s a get_js_dependencies() method, but it do not works well in my repo. So I recommend use :


This is not elegant but it works. You will get a list contains the names of every js file you need.

Before assign an echart variable, we need assign an global variable representing our root url, because this will change between dev and product server:

$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};

Then, assign our echart div:

 var chart = echarts.init(document.getElementById('myechart'), {renderer: 'canvas'});

For the step 2, we can send a ajax ,which will let python call our dump_options_with_quotes() and send back the result, then we just config the option:

		var data = {
                  data: JSON.stringify({
                    'name': YourAttribution,
                    type: "GET",
                    url: $SCRIPT_ROOT+"/getChart",
                    dataType: 'json',
                    success: function (result) {
                        eval('result.tooltip.formatter='+result.tooltip.formatter);//Fix JsCode's format

The back-end python will be like :

@app.route("/getChart", methods=['GET'])
def get_chart():
	attr = json.loads(request.args.get('data', type=str))['name']
	return c.dump_options_with_quotes()

Render Notebook


When we use render_embed(), or dump_options_with_quotes(), the final page is actually rendered by standard echarts.

Echarts is a js library , which is the real role drawing those charts, which means, we can add javascript in our page, via echarts’ settings, to control our charts at the final step.

Notice that we have a success function above:

success: function (result) {