This guide takes a small flask project as an example of how to use pyecharts in flask. Please make sure you have installed flask and have not been installed please perform PIP install flask or otherwise install.
Step 0: Create a new flask project first
Linux/macos System
$ mkdir flask-echarts
$ cd flask-echarts
$ mkdir Templates
Windows System
Create a new Flask-echarts folder under which to create a new templates subfolder.
Step 1: Provide your own template for the project
Save the following HTML template code as a pyecharts.html file and move to the new Templates folder in the previous step.
<! DOCTYPE html>
Step 2: Invoke the Chart_instance.render_embed () method to render the chart
Please save the following code as a server.py file and move to the root of the project.
Import random from Pyecharts import Scatter3d to pyecharts.constants import default_host from flask import flask, Render _template app = Flask (__name__) @app. Route ("/") def hello (): S3d = Scatter3d () return render_template (' Pyechar
Ts.html ', myechart=s3d.render_embed (), Host=default_host, Script_list=s3d.get_js_dependencies ()) def scatter3d (): data = [Generate_3d_random_point () for _ in Range] Range_color = [' #313695 ', ' #4575b4 ', ' #74add1 ', ' #abd9e9 ', ' #e0f3f8 ', ' #ffffbf ', ' #fee090 ', ' ', ' #fdae61 ', ' #f46d43 ', ' #d73027 ', ' #a50026 '] Scatter3d = Scatter3d ("3D scattering plot demo", Width=1200, height=600 ) Scatter3d.add ("", Data, Is_visualmap=true, Visual_range_color=range_color) return Scatter3d def Generate_3d_ra
Ndom_point (): return [Random.randint (0, MB), Random.randint (0), Random.randint (0, 100)] if __name__ = = "__main__ ": #运行项目 app.run (debug = True)
Script_list is the Echarts JS Library on which page () class renders web pages, and the number of dependent libraries depends on the kind of graphics you want to render.
Host is Echarts JS Library address, the default address for Http://chfw.github.io/jupyter-echarts/echarts of course, if you want you can also change this address, first clone https://github.com /chfw/jupyter-echarts then mount the Echarts folder on your own server.
At this point, the Flask-echarts directory is
├──server.py
└──templates
└──pyecharts.html
Step 3: Run the project
Linux/macos System
$ export flask_app=server.py
$ flask Run
* Serving flask APP "Server"
* Running on Http://127.0.0.1:5000/(Pre SS Ctrl + C to quit)
Windows System
$ set flask_app=server.py
$ flask Run
* Serving flask APP "Server"
* Running on http://127.0.0.1:5000/(press CTRL + C to quit)
Effect Display: