pyecharts django

pyecharts + django 分离式调用不显示?
https://bbs.csdn.net/topics/398612805?spm=1001.2014.3001.6376
有人知道django和pyechart分离后地图不加载该怎么解决吗

img

和上面那个链接的问题一样

【相关推荐】



  • 文章:django项目中pyecharts详细使用教程2 中也许有你想要的答案,请看下吧
  • 除此之外, 这篇博客: 利用 Django 动态展示 Pyecharts 图表数据的几种方法中的 3. 编写 Django 和 pyecharts 代码渲染图表 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    由于 json 数据类型的问题,无法将 pyecharts 中的 JSCode 类型的数据转换成 json 数据格式返回到前端页面中使用。

    因此在使用前后端分离的情况下尽量避免使用 JSCode 进行画图。

    将下列代码保存到 demo/views.py 中

    from django.shortcuts import render
    
    # Create your views here.
    
    import json
    from random import randrange
    
    from django.http import HttpResponse
    from rest_framework.views import APIView
    
    from pyecharts.charts import Bar, Pie
    from pyecharts.faker import Faker
    from pyecharts import options as opts
    
    
    # Create your views here.
    def response_as_json(data):
        json_str = json.dumps(data)
        response = HttpResponse(
            json_str,
            content_type="application/json",
        )
        response["Access-Control-Allow-Origin"] = "*"
        return response
    
    
    def json_response(data, code=200):
        data = {
            "code": code,
            "msg": "success",
            "data": data,
        }
        return response_as_json(data)
    
    
    def json_error(error_string="error", code=500, **kwargs):
        data = {
            "code": code,
            "msg": error_string,
            "data": {}
        }
        data.update(kwargs)
        return response_as_json(data)
    
    
    JsonResponse = json_response
    JsonError = json_error
    
    
    def pie_base() -> Pie:
        c = (
            Pie()
                .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
                .set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])
                .set_global_opts(title_opts=opts.TitleOpts(title="Pie-示例"))
                .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
                .dump_options_with_quotes()
        )
        return c
    
    class ChartView(APIView):
        def get(self, request, *args, **kwargs):
            return JsonResponse(json.loads(pie_base()))
    
    class IndexView(APIView):
        def get(self, request, *args, **kwargs):
            return HttpResponse(content=open("./templates/index.html").read())

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^