Plotly Dash - 图像不显示?

发布于 2025-01-09 22:23:33 字数 1740 浏览 1 评论 0原文

我正在学习 Ploty Dash 并尝试了基本的多输出回调。我尝试检索取决于(数字轮和颜色)组合的图像。单选按钮正常并且工作正常,但图像不显示。它只是显示“撕裂图像图标”和方形白色空白。

我的图像以.jpg格式存储在我的C计算机上(路径如下面的代码所示)。我尝试从本课程附加的文件中复制代码,但仍然无法显示该图像。

因为这是我学习Python的第一周,所以我除了四处询问哈哈之外什么也做不了。感谢您的帮助。

  app = dash.Dash()
    

def encode_image(image_file):
    encoded = base64.b64encode(open(image_file, 'rb').read())
    return 'data:image/png;base64,{}'.format(encoded.decode())


app.layout = html.Div([
            dcc.RadioItems(id='wheels',
                options=[{'label': i,'value':i} for i in df['wheels'].unique()],
                           value=1
                          ),
            html.Div(id='wheels-output'),
    
            html.Hr(),
    
            dcc.RadioItems(id='colors',
                 options=[{'label': i,'value':i} for i in df['color'].unique()],
                           value='blue'),
            html.Div(id='colors-output'),
            
            html.Img(id='display-image', src='children', height=300)
      
], style={'fontFamily':'helvetica','fontsize':18})

@app.callback(Output('wheels-output','children'),
             [Input('wheels','value')])

def callback_a(wheels_value):
    return "You Chose {}".format(wheels_value)

@app.callback(Output('colors-output','children'),
             [Input('colors','value')])

def callback_b(colors_value):
    return "You Chose {}".format(colors_value)

@app.callback(Output('display-image','src'),
             [Input('wheels','value'),
             Input('colors','value')])

def callback_image(wheel, color):
    path = '/Python/Udemy-Plotly/Data/Images/'
    return encode_image(path+df[(df['wheels']==wheel) &  
                                (df['color']==color)]['image'].value[0])

I'm learning Ploty Dash and tried basic Multiple Output Callback. I tried to retrieve images depended to combination of (Number Wheels and Colors). The radio button is OK and work fine, but image does not show up. It just displayed "ripped image icon" and square white blank.

My images stored in .jpg format at my C computer (path as shown in code below). I tried to copy the code from file attached from this course, but still wont displayed that image.

Since this is my first week learn python, I cannot do anything but asking aroung lol. Thanks for help.

  app = dash.Dash()
    

def encode_image(image_file):
    encoded = base64.b64encode(open(image_file, 'rb').read())
    return 'data:image/png;base64,{}'.format(encoded.decode())


app.layout = html.Div([
            dcc.RadioItems(id='wheels',
                options=[{'label': i,'value':i} for i in df['wheels'].unique()],
                           value=1
                          ),
            html.Div(id='wheels-output'),
    
            html.Hr(),
    
            dcc.RadioItems(id='colors',
                 options=[{'label': i,'value':i} for i in df['color'].unique()],
                           value='blue'),
            html.Div(id='colors-output'),
            
            html.Img(id='display-image', src='children', height=300)
      
], style={'fontFamily':'helvetica','fontsize':18})

@app.callback(Output('wheels-output','children'),
             [Input('wheels','value')])

def callback_a(wheels_value):
    return "You Chose {}".format(wheels_value)

@app.callback(Output('colors-output','children'),
             [Input('colors','value')])

def callback_b(colors_value):
    return "You Chose {}".format(colors_value)

@app.callback(Output('display-image','src'),
             [Input('wheels','value'),
             Input('colors','value')])

def callback_image(wheel, color):
    path = '/Python/Udemy-Plotly/Data/Images/'
    return encode_image(path+df[(df['wheels']==wheel) &  
                                (df['color']==color)]['image'].value[0])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2025-01-16 22:23:33

尝试将下面的行修改

'data:image/png;base64,{}'.format(encoded.decode())

'data:image/jpg;base64,{}'.format(encoded.decode())

Try modify bellow line

'data:image/png;base64,{}'.format(encoded.decode())

to

'data:image/jpg;base64,{}'.format(encoded.decode())
三生殊途 2025-01-16 22:23:33

我提供了完整的路径名。

path = '/Users/sathya/PythonClass/Data_Analysis_with_Python/Data_Visualization/Dash/Image/'   

'data:image/png;base64,{}'.format(encoded.decode()) 

改为

'data:image/jpg;base64,{}'.format(encoded.decode())

I provided the full path name.

path = '/Users/sathya/PythonClass/Data_Analysis_with_Python/Data_Visualization/Dash/Image/'   

Also Changed

'data:image/png;base64,{}'.format(encoded.decode()) 

to

'data:image/jpg;base64,{}'.format(encoded.decode())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文