【发布时间】:2021-08-11 18:58:29
【问题描述】:
所以我正在开发 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)
接下来,我有这段代码,当在本地运行时,它只会显示网页中的图像。要点: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()
现在,我需要一种方法来添加这两个代码,以便当我在文本框中输入“img name.tiff”并选择按钮时,它会显示网页中的图片。
我是 Dash 和 Plotly 的新手。我不知道该怎么做。我试图研究它,但找不到任何有用的东西。有人可以帮忙吗?
【问题讨论】:
标签: python html plotly-dash