✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Session id on dash application

Hello!
I’m developing a small Dash application based on sessions into a Flask environment that generates a file to print, including the session_id in the name (for user concurrence). When the user close session, the file is deleted.
The problem is that if I refresh the page, the session_id changes and then the file is not deleted successfully.
This is the code:

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

server=Flask(__name__)

app = dash.Dash(__name__,server=server,routes_pathname_prefix='/dash/', external_stylesheets=external_stylesheets)

server.secret_key=b'_5#y2L"F4Q8z\n\xec]/'
@server.route('/', methods=['GET', 'POST'])
def index():
    if request.method=='POST':
        session['username']=request.form['username']
        return redirect('/dash')
    return  '''
       <form method="post">
            <p><input type=text name=username>
            <p><input type=submit value=Login>
        </form>
            '''
    
@server.route('/logout/<string:session_id>')
def clearfiles(session_id):
    if os.path.exists("C:/xampp/htdocs/file"+session_id+".docx"):
        os.remove("C:/xampp/htdocs/file"+session_id+".docx")
    session.pop('username',None)
    return redirect(url_for('index'))

def serve_layout():
    session_id=str(uuid.uuid4())
    return html.Div([
    ...
    html.Button(id="generate-file", n_clicks=0, children="generate-file"),
    html.A(html.Button('Close session'), href="../logout/"+session_id)
    html.Div(session_id, id="session-id",
                    style={'display': 'none'})
    ])

I could generate the session_id in the index page and pass it to dash page. The problem is, as dash is not described into a server.route clause, it’s not that immediate for me. I’m stuck.
Any idea? I would really appreciate!