Py.Cafe

maartenbreddels/

dash-local-image

Display a local image using Dash

DocsPricing
  • assets/
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import dash
from dash import dcc, html
import base64

# cat image from https://www.pexels.com/photo/white-and-grey-kitten-on-brown-and-black-leopard-print-textile-45201/


app = dash.Dash()

image_filename = 'cat.png'
image_path_at_server = 'assets/' + image_filename
image_path_webbrowser = app.get_asset_url(image_filename)
encoded_image = base64.b64encode(open(image_path_at_server, 'rb').read()).decode("utf-8")

app.layout = html.Div([
    html.H3("Using a base64 image"),
    html.Img(src='data:image/png;base64,{}'.format(encoded_image)),
    html.H3("Using a get_asset_url"),
    html.Img(src=image_path_webbrowser),
])