django html和pdf图

发布于 2025-01-31 18:51:15 字数 1257 浏览 3 评论 0原文

我正在使用django应用程序使用plotly生成一些图形,因此

我尝试生成以下图的pdf文件 我的html页面是:

        <center> <p class="lead"><strong><em>&nbsp;All Plots&nbsp;</em></strong></p></center>
        <div class="row">
            <div class="col-md-6" >
                {{ sunburst|safe}}
            </div>
        
            <div class="col-md-6" >
                {{ sunburst2|safe}}
            </div>
        </div>

和我的url.py:

    path('export_pdf/', views.export_pdf, name='export_pdf'),

和views.py;

def export_pdf(request):
    df = px.data.gapminder().query("country=='Canada'")
    fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
    df2 = px.data.gapminder().query("continent=='Oceania'")
    fig2 = px.line(df2, x="year", y="lifeExp", color='country')

    chart = fig.to_html()
    chart2 = fig2.to_html()
    context= {
        'sunburst':chart,
        'sunburst2':chart2,
        }
    return render(request, 'Home/Reporting/part_pdf.html',context)

我尝试了此页面中的一些代码,但无法生成文件任何帮助?

“ https://plotly.com/python/v3/pdf-reports/”

我最好的

I am using the Django application to generate some graphs using Plotly so

I tried to generate a pdf file of the following plots of
my HTML page is:

        <center> <p class="lead"><strong><em> All Plots </em></strong></p></center>
        <div class="row">
            <div class="col-md-6" >
                {{ sunburst|safe}}
            </div>
        
            <div class="col-md-6" >
                {{ sunburst2|safe}}
            </div>
        </div>

and my url.py :

    path('export_pdf/', views.export_pdf, name='export_pdf'),

and the views.py;

def export_pdf(request):
    df = px.data.gapminder().query("country=='Canada'")
    fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
    df2 = px.data.gapminder().query("continent=='Oceania'")
    fig2 = px.line(df2, x="year", y="lifeExp", color='country')

    chart = fig.to_html()
    chart2 = fig2.to_html()
    context= {
        'sunburst':chart,
        'sunburst2':chart2,
        }
    return render(request, 'Home/Reporting/part_pdf.html',context)

I tried some code from this page but I can't generate the file any help?

https://plotly.com/python/v3/pdf-reports/

are there any other methods for doing that (html page with plots and tables)?

All my best

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2025-02-07 18:51:15

我搜索高低,找不到任何东西。将一个对我非常有效的解决方案入侵:

import plotly.graph_objs as go
import plotly.io as pio

labels = ['Alice','Bob','Carl']
vals = [2,5,4]

data = [go.Bar(x=vals, y=labels, orientation='h')]

layout = go.Layout(margin_pad=10)
fig = go.Figure(data=data,layout=layout)

svg = pio.to_image(fig, format="svg")
context["svg"] = svg.decode("utf-8")

然后在类似的模板中,

<div>
{{ svg|safe }}
</div>

第一个解决方案也是您所引用的解决方案。质量非常糟糕。该解决方案更加清脆。

我使用了Weasyprint,但我敢肯定它也可以与其他解决方案一起使用。

I searched high and low and could not find anything. Hacked together a solution that worked very well for me:

import plotly.graph_objs as go
import plotly.io as pio

labels = ['Alice','Bob','Carl']
vals = [2,5,4]

data = [go.Bar(x=vals, y=labels, orientation='h')]

layout = go.Layout(margin_pad=10)
fig = go.Figure(data=data,layout=layout)

svg = pio.to_image(fig, format="svg")
context["svg"] = svg.decode("utf-8")

and then in the template something like this

<div>
{{ svg|safe }}
</div>

The first solution was the one you referenced as well. The quality was very bad though. This solution is a lot more crisp.

I used it with weasyprint, but I'm sure it will work with other solutions as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文