Kha*_*ria 4 html python plotly-dash
所以我正在研究 Dash Plotly。网页应该有一个文本框和一个按钮。这个想法是,当我写下图像名称然后选择按钮时,它将显示本地目录中的图像。
我试图将整个工作分为两部分。
首先,我有这段代码,它会显示您在文本框中写入的内容,然后单击按钮。代码:
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import os
from PIL import Image
import numpy as np
import plotly.express as px
os.chdir(os.path.dirname(os.path.abspath(__file__)))
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
[
html.I("Try typing the image name in input and observe it"),
html.Br(),
dcc.Input(id="input", type="text", placeholder=""),
html.Div(id="output"),
]
)
@app.callback(
Output("output", "children"),
Input("input", "value")
)
def update_output(input):
return u'Input {}'.format(input)
if __name__ == "__main__":
app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)
接下来,我有这段代码,当在本地运行时,它只会向您显示网页中的图像。要点:python 文件和图像位于同一本地目录中。代码:
import os
from PIL import Image
import numpy as np
import plotly.express as px
# Change working dir to current running script dir:
print('[change directory]')
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def test():
# load the image
img = np.array(Image.open('img name.tiff'))
fig = px.imshow(img, color_continuous_scale='gray')
fig.update_layout(coloraxis_showscale=False)
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(showticklabels=False)
fig.show()
test()
Run Code Online (Sandbox Code Playgroud)
现在,我需要一种方法来添加这两个代码,以便当我在文本框中写入“img name.tiff”并选择按钮时,它将显示网页中的图片。
我对 Dash 和 Plotly 很陌生。我不知道该怎么做。我尝试研究它,但找不到任何有用的东西。有人可以帮忙吗?
您可以将回调更改为如下所示:
@app.callback(Output("output", "children"), Input("input", "value"))
def update_output(input):
if not input:
raise PreventUpdate
try:
img = np.array(Image.open(f"assets/{input}"))
except OSError:
raise PreventUpdate
fig = px.imshow(img, color_continuous_scale="gray")
fig.update_layout(coloraxis_showscale=False)
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(showticklabels=False)
return dcc.Graph(figure=fig)
Run Code Online (Sandbox Code Playgroud)
如果没有输入值或者无法打开用户指定路径的图像,我们使用PreventUpdate:
在某些情况下,您不想更新回调输出。您可以通过在回调函数中引发 PreventUpdate 异常来实现此目的。
注意:此示例假设图像存储在该assets文件夹中。
更新:使用按钮实现
@app.callback(
Output("output", "children"),
Input("input", "value"),
Input("show-image", "n_clicks"),
prevent_initial_call=True,
)
def update_output(input, n_clicks):
if not input:
raise PreventUpdate
ctx = dash.callback_context
if ctx.triggered[0]["prop_id"].split(".")[0] != "show-image":
raise PreventUpdate
try:
img = np.array(Image.open(f"assets/{input}"))
except OSError:
raise PreventUpdate
fig = px.imshow(img, color_continuous_scale="gray")
fig.update_layout(coloraxis_showscale=False)
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(showticklabels=False)
return dcc.Graph(figure=fig)
Run Code Online (Sandbox Code Playgroud)
在前面查找可以打开的图像的示例中,回调返回带有图像的图形。如果您想推迟显示图像直到单击按钮,您可以向布局添加一个按钮
html.Button("Show Image", id="show-image", n_clicks=0)
Run Code Online (Sandbox Code Playgroud)
并用于callback_context确定哪个Input触发了按钮。如果id此输入的 等于"show-input"(id我们按钮的),我们将显示图形和图像。
| 归档时间: |
|
| 查看次数: |
11945 次 |
| 最近记录: |