Step 0: Using the new virtualenv environment
We recommend that you use the 1.11.4 version of Django
$ virtualenv--no-site-packages pyecharts-env$ source pyecharts-env/bin/activate$ pip install Django ==1.11.4$ pip Install pyecharts
Step 1: Create a new Django project
$ django-admin Startproject Myechartsite
Create an application
$ python manage.py startapp myfirstvis$ lsdb.sqlite3 manage.py myechartsite Myfirstvis
myechartsite/settings.py
registering the application in
#myechartsite/settings.pyInstalled_apps = [ 'Django.contrib.admin', 'Django.contrib.auth', 'Django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'Django.contrib.staticfiles', 'Myfirstvis' #<---]
Let's edit urls.py first. The function of this file in Django is to hook up the HTTP requirements of the previous paragraph with the backend service functions. In Step3, we introduce back-end service functions
1 #myfirstvis/urls.py2 fromDjango.conf.urlsImportURL3 4 from.Import views5 6Urlpatterns = [7URL (r'^$', Views.index, Name='Index'),8]
myechartsite/urls.py
new ' Myfirstvis.urls ' in
1myechartsite/urls.py2 fromDjango.conf.urlsImportinclude, url3 fromDjango.contribImportAdmin4 5Urlpatterns = [6URL (r'^admin/', Admin.site.urls),7URL (r'myfirstvis/', Include ('Myfirstvis.urls'))#<---8]
Step 2: Working with the View feature section
Save the following code myfirstvis/views.py
in.
1 from __future__ Importunicode_literals2 ImportMath3 4 fromDjango.httpImportHttpResponse5 fromDjango.templateImportLoader6 fromPyechartsImportLine3d7 8 9Remote_host ="Https://pyecharts.github.io/assets/js"Ten One A defIndex (Request): -Template = Loader.get_template ('myfirstvis/pyecharts.html') -L3d =Line3d () theContext =Dict ( -myechart=l3d.render_embed (), -host=Remote_host, -script_list=l3d.get_js_dependencies () + ) - returnHttpResponse (Template.render (context, request)) + A at defLine3d (): -_data = [] - forTinchRange (0, 25000): -_t = t/1000 -x = (1 + 0.25 * MATH.COS (* _t)) *Math.Cos (_t) -y = (1 + 0.25 * MATH.COS (* _t)) *Math.sin (_t) inz = _t + 2.0 * Math.sin (75 *_t) - _data.append ([x, Y, z]) toRange_color = [ + '#313695','#4575b4','#74add1','#abd9e9','#e0f3f8','#ffffbf', - '#fee090','#fdae61','#f46d43','#d73027','#a50026'] theLine3d = Line3d ("3D Line Plot demo", width=1200, height=600) *Line3d.add ("", _data, is_visualmap=True, $Visual_range_color=range_color, Visual_range=[0, 30],Panax NotoginsengIs_grid3d_rotate=true, grid3d_rotate_speed=180) - returnLine3d
cript_list
is the Echarts JS Library on which the page () class renders pages, depending on the kind of graphics you want to render.
host
Is the address of the Echarts JS Library, the default address is Https://pyecharts.github.io/assets/js of course, if you want you can also change this address, first cloned https://github.com/pyecharts /assets then js
mount the folder on your own server.
Step 3: Provide your own template for the project
Windows system
Under the Myfirstvis directory, create a new Templates/myfirstvis subdirectory
Myfirstvis Directory
─myfirstvis ├──admin.py ├──apps.py __init__. py ├──migrations │ __init__. py ├──models.py ├──templates │ └──myfirstvis │ └── pyecharts.html ├──tests.py ├──urls.py └──views.py
To save the following HTML template code as pyecharts.html, make sure that the absolute path to the pyecharts.html file is<project root>/myfirstvis/templates/myfirstvis
1 <!--myfirstvis/templates/pyecharts.html -2 <!DOCTYPE HTML>3 <HTML>4 5 <Head>6 <MetaCharSet= "Utf-8">7 <title>Proudly presented by Pyccharts</title>8 {% for jsfile_name in script_list%}9 <Scriptsrc= "{{host}}/{{jsfile_name}}.js"></Script>Ten {% endfor%} One </Head> A - <Body> - {{Myechart|safe}} the </Body> - - </HTML>
Step 4: Run the project
not for 'python manage.py migrate' to apply them. August 08, 2017-05:48:38'myechartsite.settings'starting Development Server at http://127.0.0.1:8000/Quit the server with CONTROL-C.
Visit http://localhost:8000/myfirstvis/and you'll be able to see the cool 3D diagram.
Summary
See, you can use Pyecharts to create a visual chart in just a few simple steps. The Django official tutorial takes seven steps and we're done here in three steps.
Python:django Framework Development Data Visualization website