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?