Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique session id for different dash sessions

I would like to create unique session ids for each time a user opens the dash app in browser.

I have been following the tutorial here:

https://dash.plot.ly/sharing-data-between-callbacks

This is my code:

import dash
import dash_html_components as html
import dash_core_components as dcc
import flask
import datetime 
import uuid

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = flask.Flask(__name__)
dash_app = dash.Dash(__name__,server=app,url_base_pathname="/",external_stylesheets=external_stylesheets)

def serve_layout():
    session_id = str(uuid.uuid4())
    return html.Div([
    html.Div(session_id, id='session-id', style={'display': 'none'}),
    html.Div(dcc.Input(id="input_session_id",type="text",value=session_id))
    ])
dash_app.layout = serve_layout()

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=80)

It seems like the session ids are different if I use different computers but if I use the same computer, it will stay the same.

Is there a way to generate an unique session each time an user opens the url for the dash app?

like image 809
Bo Qiang Avatar asked Sep 21 '25 01:09

Bo Qiang


1 Answers

Obviously problem was in this line: dash_app.layout = serve_layout() You had to use it without parenthesis:

dash_app.layout = serve_layout

In fact you were assigning not a function, but a result of the function called once at the first page load.

like image 115
Anatoly Alekseev Avatar answered Sep 23 '25 05:09

Anatoly Alekseev