英文:
Add button to callback to export figure - dash Plotly
问题
I know Plotly has a built-in function to export a figure. But the resolution is poor and there is limited control over additional parameters. Is it possible to include a button that links to a function? That way, it provides more parameters to the output figure, such as, size, resolution, file type etc.
我知道Plotly有一个内置函数来导出图形。但分辨率较低,对附加参数的控制有限。是否可以包括一个按钮,将其链接到一个函数?这样,它可以为输出图形提供更多参数,如大小、分辨率、文件类型等。
I'm aiming to include a button within the figure (see below) that can be assigned to a callback function that exports a figure.
我希望在图形内包括一个按钮(见下文),可以分配给导出图形的回调函数。
# button to export figure
def export_figure(fig):
fig.write_image('output.png', scale=3)
return fig
button to export figure
导出图形的按钮
英文:
I know Plotly has a built-in function to export a figure. But the resolution is poor and there is limited control over additional parameters. Is it possible to include a button that links to a function? That way, it provides more parameters to the output figure, such as, size, resolution, file type etc.
I'm aiming to include a button within the figure (see below) that can be assigned to a callback function that exports a figure.
import pandas as pd
import plotly.graph_objs as go
import dash
from dash import dcc
from dash import html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df = pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
external_stylesheets = [dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
nav_bar = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id = 'Site',
value = 'Site',
options = [{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable = False
),
])
app.layout = dbc.Container([
dbc.Row([
dbc.Col(html.Div(nav_bar, className = "bg-secondary h-100"), width = 2),
dbc.Col([
dbc.Row([
dbc.Col(dcc.Graph(id = 'pie-chart')),
]),
], width = 5),
])
], fluid = True)
@app.callback(
Output("pie-chart", "figure"),
[Input("Site", "value")])
def generate_chart(value):
pie_data = spacex_df[spacex_df['Site'] == value]
success_count = sum(pie_data['class'] == 0)
failure_count = sum(pie_data['class'] == 1)
fig = go.Figure(data=, values=[success_count, failure_count])])
fig.update_layout(title=f"Site: {value}")
return fig
if __name__ == '__main__':
app.run_server(debug=True)
# button to export figure
def export_figure(fig):
fig.write_image('output.png', scale = 3)
return fig
答案1
得分: 1
你有一个回调函数,它用图形填充接口。回调函数调用另一个函数通过点击按钮将图形导出到硬盘上。在这个函数中,你可以像下面这样更改导出的参数:
import pandas as pd
import plotly.graph_objs as go
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df = pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
external_stylesheets = [dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
nav_bar = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id = 'Site',
value = 'Site',
options = [{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable = False
),
])
app.layout = dbc.Container([
dbc.Row([
dbc.Col(html.Div(nav_bar, className = "bg-secondary h-100"), width = 2),
dbc.Col([
dbc.Row([
dbc.Col(dcc.Graph(id = 'pie-chart')),
]),
dbc.Row([
html.Button('Export', id='export-val', n_clicks=0),
]),
dbc.Row([
html.Div(id='confirm')
])
], width = 8),
])
], fluid = True)
@app.callback(
[Output("pie-chart", "figure"),
Output("confirm", "children")],
[Input("Site", "value"),
Input("export-val", "n_clicks"),]
)
def generate_chart(value, n_clicks):
pie_data = spacex_df[spacex_df['Site'] == value]
success_count = sum(pie_data['class'] == 0)
failure_count = sum(pie_data['class'] == 1)
fig = go.Figure(data=, values=[success_count, failure_count])])
fig.update_layout(title=f"Site: {value}")
text = export_figure(fig, n_clicks)
return fig,text
# button to export figure
def export_figure(fig, n_clicks):
if n_clicks != 0:
fig.write_image('output.png', scale = 6, width=500, height=600)
return "Your figure has been successfully exported!"
else:
return ""
if __name__ == '__main__':
app.run_server(debug=True)
输出:
英文:
You have one callback which fills the interface up with the figure. The callback calls another function to exports the figure to your hard drive through clicking the button. In the function, you can change the parameters of exporting as you would:
import pandas as pd
import plotly.graph_objs as go
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_dash.csv"
spacex_df = pd.read_csv(url)
spacex_df.rename(columns={'Launch Site':'Site'}, inplace=True)
external_stylesheets = [dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets = external_stylesheets)
app = JupyterDash(__name__, external_stylesheets = external_stylesheets)
nav_bar = html.Div([
html.P("site-dropdown:"),
dcc.Dropdown(
id = 'Site',
value = 'Site',
options = [{'value': x, 'label': x}
for x in ['CCAFS LC-40', 'CCAFS SLC-40', 'KSC LC-39A', 'VAFB SLC-4E']],
clearable = False
),
])
app.layout = dbc.Container([
dbc.Row([
dbc.Col(html.Div(nav_bar, className = "bg-secondary h-100"), width = 2),
dbc.Col([
dbc.Row([
dbc.Col(dcc.Graph(id = 'pie-chart')),
]),
dbc.Row([
html.Button('Export', id='export-val', n_clicks=0),
]),
dbc.Row([
html.Div(id='confirm')
])
], width = 8),
])
], fluid = True)
@app.callback(
[Output("pie-chart", "figure"),
Output("confirm", "children")],
[Input("Site", "value"),
Input("export-val", "n_clicks"),]
)
def generate_chart(value, n_clicks):
pie_data = spacex_df[spacex_df['Site'] == value]
success_count = sum(pie_data['class'] == 0)
failure_count = sum(pie_data['class'] == 1)
fig = go.Figure(data=, values=[success_count, failure_count])])
fig.update_layout(title=f"Site: {value}")
text = export_figure(fig, n_clicks)
return fig,text
# button to export figure
def export_figure(fig, n_clicks):
if n_clicks != 0:
fig.write_image('output.png', scale = 6, width=500, height=600)
return "Your figure has been successfully exported!"
else:
return ""
if __name__ == '__main__':
app.run_server(debug=True)
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论