如何将以Dash编写的网站显示为静态PDF(Python)? [英] How do I display a website written in Dash as a static PDF (Python)?

查看:266
本文介绍了如何将以Dash编写的网站显示为静态PDF(Python)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自己在破折号中创建的网站导出为静态PDF.这是我的网站的代码(它只是一个3列的图表):

I want to export the site I've made in dash into a static PDF. Here is the code for my site (it's just a chart with 3 columns):

import dash
import dash_core_components as dcc
import dash_html_components as html
import pdfkit
from flask import Flask, render_template, make_response

app = dash.Dash()
app.layout = html.Div(
                className="three columns",
                children=html.Div([
                    dcc.Graph(
                        id='right-top-graph',
                        figure={
                            'data': [{
                                'x': [1, 2, 3],
                                'y': [3, 1, 2],
                                'type': 'bar'
                            }],
                            'layout': {
                                'height': 400,
                                'margin': {'l': 10, 'b': 20, 't': 0, 'r': 0}
                            }
                        }
                    ),


                ])
            )

app.css.append_css({
    'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})

if __name__ == '__main__':
    app.run_server(debug=True)

我尝试通过将pdfkit代码添加到脚本中来使用pdfkit,但是它没有用(收到一条错误消息,告诉我render_template()接受1个位置参数,但给出了2个位置参数):

I tried using pdfkit by adding this code to my script, but it didn't work (received an error telling me that render_template() takes 1 positional argument but 2 were given):

rendered = render_template('pdf_template.html',app)
pdf = pdfkit.from_string(rendered, False)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'

有人对我如何将仪表板站点转换为PDF有任何想法吗?

谢谢.

推荐答案

您可以使用 pdfkit 通过以下方式:

You can use pdfkit in the following way:

import pdfkit
pdfkit.from_url('http://local.dash.site', 'out.pdf')

与您发布的内容最大的不同是,您可以使用本地Web服务器来呈现页面.

The big difference from what you posted is that you could use the local web server to render the page.

您也可以使用 https://wkhtmltopdf.org/ 这是pdfkit下的lib.

As an alternative, you could also use https://wkhtmltopdf.org/ This is the lib underneath pdfkit.

这篇关于如何将以Dash编写的网站显示为静态PDF(Python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆