日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在 Django 中使用 pyecharts

發布時間:2025/3/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在 Django 中使用 pyecharts 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

文章目錄

    • 1.Django 與 pyecharts
      • 1.1 官網的在 Django 中使用 pyecharts教程 :
      • 1.2 Django中實現頁面跳轉
      • 1.3 一個頁面內多個圖表
      • 2.生成詞云所需的元組

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 indexurlpatterns = [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 SymbolTypewords = [("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,wordcloudurlpatterns = [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.html 和 simple_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>

其中data1和data2是在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 cwordcloud_base().render() # 生成的HTML存在了 C:\\Users\\asus\\render.html

總結

以上是生活随笔為你收集整理的在 Django 中使用 pyecharts的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。