Skip to content

Instantly share code, notes, and snippets.

@tashrifbillah
Created July 1, 2020 23:32
Show Gist options
  • Save tashrifbillah/304fd47b14a6c6bce6b7572b555514d7 to your computer and use it in GitHub Desktop.
Save tashrifbillah/304fd47b14a6c6bce6b7572b555514d7 to your computer and use it in GitHub Desktop.
Displaying *png, *jpg image on a dash app
import dash
import base64
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_core_components as dcc
app = dash.Dash(__name__)
test_png = r'C:\Users\tashr\Downloads\20190318_114007.jpg'
app.layout = html.Div([
html.Img(id='img'),
dcc.Input(id='var1')
])
@app.callback(Output('img', 'src'),
[Input('var1', 'value')])
def dash_img(var1):
test_base64 = base64.b64encode(open(test_png, 'rb').read()).decode('ascii')
return 'data:image/png;base64,{}'.format(test_base64)
if __name__ == '__main__':
app.run_server(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment