在 Django 中使用 pyecharts

pyecharts 官方文档
https://pyecharts.org/#/

1.Django 与 pyecharts

1.1 官网的在 Django 中使用 pyecharts教程 :

https://pyecharts.org/#/zh-cn/web_django

其中配置pyecharts_django_demo/urls.py中,修改如下,可以不用再demo/下创建urls.py(当然这也可能是新版本的标准)

from django.conf.urls import include, url
from django.contrib import admin
from demo.views import index

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', index)
]

这样打开 http://127.0.0.1:8000/index/ , 即可在浏览器中看到如下界面
在这里插入图片描述
绘制词云
按照网上教程操作一直报错,原来是官方导包的方式改了

from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType


words = [
    ("Sam S Club", 10000),
    ("Macys", 6181),
    ("Amy Schumer", 4386),
    ("Jurassic World", 4055),
    ("Charter Communications", 2467),
    ("Chick Fil A", 2244),
    ("Planet Fitness", 1868),
    ("Pitch Perfect", 1484),
    ("Express", 1112),
    ("Home", 865),
    ("Johnny Depp", 847),
    ("Lena Dunham", 582),
    ("Lewis Hamilton", 555),
    ("KXAN", 550),
    ("Mary Ellen Mark", 462),
    ("Farrah Abraham", 366),
    ("Rita Ora", 360),
    ("Serena Williams", 282),
    ("NCAA baseball tournament", 273),
    ("Point Break", 265),
]


def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100])
        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
    )
    return c

解释:上面的函数wordcloud_base()后面的-> WordCloud是指这个函数返回值为一个 WordCloud类的对象 (有待修改),这是python中的 注解 . 还有一种是
def wordcloud_base(s: 'str')-> WordCloud: 括号中的是希望传入的参数类型.
如果我们想在Django中显示词云的话,只需对上面demo做修改,
(1) views.py中添加

def wordcloud(request):
    words = [
    ("Sam S Club", 10000),
    ("Macys", 6181),
    ("Amy Schumer", 4386),
    ("Jurassic World", 4055),
    ("Charter Communications", 2467),
    ("Chick Fil A", 2244),
    ("Planet Fitness", 1868),
    ("Pitch Perfect", 1484),
    ("Express", 1112),
    ("Home", 865),
    ("Johnny Depp", 847),
    ("Lena Dunham", 582),
    ("Lewis Hamilton", 555),
    ("KXAN", 550),
    ("Mary Ellen Mark", 462),
    ("Farrah Abraham", 366),
    ("Rita Ora", 360),
    ("Serena Williams", 282),
    ("NCAA baseball tournament", 273),
    ("Point Break", 265),
    ]

    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100])
        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
    )
    return HttpResponse(c.render_embed())

(2) 然后在urls.py中添加

from django.conf.urls import include, url
from django.contrib import admin
from demo.views import index,wordcloud

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # url(r'demo/', include('demo.urls')),  # <---
    url(r'^index/', index),
    url(r'^wordcloud/', wordcloud)
]

(3)浏览器中打开 http://127.0.0.1:8000/wordcloud/ 即可看到如下
在这里插入图片描述
对于官方这个demo,使用的哪个模板有点搞不清楚,于是借鉴下面博客的内容,加以改进,自己制定一个前端页面

http://www.cnblogs.com/wumingxiaoyao/p/8508060.html

推荐方式

def wordcloud(request):
    words = [
    ("Sam S Club", 10000),
    ("Macys", 6181),
    ("Amy Schumer", 4386),
    ("Jurassic World", 4055),
    ("Charter Communications", 2467),
    ("Chick Fil A", 2244),
    ("Planet Fitness", 1868),
    ("Pitch Perfect", 1484),
    ("Express", 1112),
    ("Home", 865),
    ("Johnny Depp", 847),
    ("Lena Dunham", 582),
    ("Lewis Hamilton", 555),
    ("KXAN", 550),
    ("Mary Ellen Mark", 462),
    ("Farrah Abraham", 366),
    ("Rita Ora", 360),
    ("Serena Williams", 282),
    ("NCAA baseball tournament", 273),
    ("Point Break", 265),
    ]

    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100])
        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
    )
    # return HttpResponse(c.render_embed())
    data = {'data': c.render_embed()}
    return render(request,"simple_chart2.html",data)

1.2 Django中实现页面跳转

现在有两个html文件,分别为simple_chart.htmlsimple_chart2.html ,我们在simple_chart2.html中加一个链接实现点击之后跳转到simple_chart.html,

  • simple_chart.html在urls.py中对应index
  • simple_chart2.html在urls.py中对应wordcloud

在simple_chart2.html中添加如下链接

<a href="{% url 'index' %}">static_page_B</a>

注意,urls.py中配置url必须加上name参数才行,否则会反解析错误

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # url(r'demo/', include('demo.urls')),  # <---
    url(r'^index/', index,name='index'),
    url(r'^wordcloud/', wordcloud,name='wordcloud')
]

1.3 一个页面内多个图表

实现这个功能要充分理解views.py中函数的返回值,也就是render()的三个参数
return render(request, "simple_chart2.html", data)
最后的data(context)是一个字典,里面包含了要在simple_chart2.html 界面渲染的数据,simple_chart2.html 内容如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>词云</title>
  
<body>
    {{ data1|safe }}
    {{ data2|safe }}
     <a href="{% url 'index' %}">static_page_B</a>
</body>
</html>

其中data1data2是在views.py中生成的

    data = {}
    data['data1'] = c.render_embed()
    data['data2'] = c2.render_embed()
    return render(request, "simple_chart2.html", data)

2.生成词云所需的元组

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
data = {'词语':['续航好','速度快','发热小'],'词频':[100,500,120]}
df = DataFrame(data)
word_frequence = [(x[0],x[1]) for x in df.values]
# word_frequence
def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
        .add("", word_frequence, word_size_range=[20, 100])
        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
    )
    return c

wordcloud_base().render()  #  生成的HTML存在了 C:\\Users\\asus\\render.html

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值