Multi-user notepad dash-app not functioning as expect

hi,new to dash here,any answer’ll be great appreciated.
I created a dash app as a online notepad sharing SAME message with different users:
There are:
1.textarea: edit text content.
2.save button:store textarea content to a local txt file
3.load button:read txt content to textarea
main code below:

import dash
import dash_core_components as dcc
import dash_html_components as html
import random
from time import sleep

app = dash.Dash(__name__)

notepad_value = ''

with open('./database/records.txt', 'r', encoding='utf-8') as f:
	notepad_value = f.read()

app.layout = html.Div([
    html.Div([
        html.Button('save', id='button_save'),
        html.Button('load', id='button_load')
    ]),
    html.Div(dcc.Textarea(value=notepad_value, id='textarea1')),
    html.P(id='p1')
])

total_modify_num = 0

@app.callback(
    dash.dependencies.Output('p1', 'children'),
    [
        dash.dependencies.Input('button_save', 'n_clicks')
    ],
    [
        dash.dependencies.State('textarea1', 'value')
    ]
)
def infoSave(n_clicks, value):
    global notepad_value
    notepad_value = value
    global total_modify_num
    total_modify_num += 1
    with open('./database/records.txt', 'w', encoding='utf-8') as f:
        f.write(value)
    return total_modify_num


@app.callback(
    dash.dependencies.Output('textarea1', 'value'),
    [
        dash.dependencies.Input('button_load', 'n_clicks')
    ]
)
def infoLoad(n_clicks):
    global notepad_value
    with open('./database/records.txt', 'r', encoding='utf-8') as f:
        notepad_value = f.read()
    return notepad_value

if __name__ == '__main__':
    app.run_server(debug=True)
    # app.run_server(debug=False, port=1122, host='0.0.0.0')

The problem is:
everything works fine the at first user:Input,save text,load text
but the second user(ie: different browser or page tag) open the page,press the load button,he can’t get content seved by last user.Instead get the ‘original’ data(the data what is stored in file before user1 overwrite)

Dont know if I explained clear enough…
for simple understanding i draw this:

for me it’s pretty wired,I can only guess when user2 open a new page,the whole dash backend just get initial,dosent go from user1 states?
This there a way to get over this?

I’ve not tried running your code yet but I think I spot the issue already. The app.layout is set at application start and doesn’t change. If you would like a dynamic app.layout (i.e. refreshed every time a page load happens) then you need to provide a function. Try something like this:

notepad_value = ''

def get_layout():
    global notepad_value
    with open('./database/records.txt', 'r', encoding='utf-8') as f:
        notepad_value = f.read()
    
    return html.Div([
        html.Div([
            html.Button('save', id='button_save'),
            html.Button('load', id='button_load')
        ]),
        html.Div(dcc.Textarea(value=notepad_value, id='textarea1')),
        html.P(id='p1')
    ])

app.layout = get_layout

sorry for replying so late,due to busy work :cry:
Thx for the suggestion,but it seems not work…
No matter I use app.layout = get_layout or ‘app.layout = get_layout()’
Application just behaviors as it once was.

Okay, there’s probably some other issue in the code, I’m also very busy but I’ll take a look when I get a chance. It’s definitely possible to do this.

Thx so much.Hope to get solution to fix that.:slightly_smiling_face: