Good day I would like to save my dash graphs to a pdf and with this code:
import dash
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
import pdfkit
from dash import dcc, html
from dash.dependencies import Input, Output
from flask import Flask, render_template, make_response
pdfdwnld = dash.Dash(__name__)
pdfdwnld.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}
}
}
),
])
)
pdfdwnld.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})
rendered = render_template('pdf_template.html',pdfdwnld)
pdf = pdfkit.from_string(rendered, False)
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'attachment; filename=output.pdf'
if __name__ == '__main__':
pdfdwnld.run_server(debug=True)
I am constantly getting below error:
Documents\AllCode\Multiple Graphs> python .\pdfdwnld.py Traceback (most recent call last): File "C:\Users\MichaelMeuleman\OneDrive - Six Square Networks\Documents\AllCode\Multiple Graphs\pdfdwnld.py", line 42, in rendered = render_template('pdf_template.html',pdfdwnld) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: render_template() takes 1 positional argument but 2 were given
The pdfdwnld.py
is my file name. I'm not 100% sure what to expect with this code. Where I believe that as soon as I run this in my terminal it should save my dash graphs as a pdf in a certain folder.
If I remove the file name in:
rendered = render_template('pdf_template.html',pdfdwnld)
I get this error:
File "C:\Users\Michael\AppData\Roaming\Python\Python311\site-packages\flask\templating.py", line 145, in render_template app = current_app._get_current_object() # type: ignore[attr-defined] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\werkzeug\local.py", line 513, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed the current application. To solve this, set up an application context with app.app_context(). See the documentation for more information.