简体   繁体   English

根据用户输入更新 plotly Dash dcc.Textarea 值

[英]Update the plotly Dash dcc.Textarea value from what user inputs

I am creating a plotly Dash app with a text area from dash core components (dcc.Textarea).我正在创建一个 plotly Dash 应用程序,其中包含来自 dash 核心组件 (dcc.Textarea) 的文本区域。 The end user wants to save the input from text area so that it can be reused next time the user goes in to update that field.最终用户希望保存来自文本区域的输入,以便下次用户进入更新该字段时可以重用它。 I was able to save the save the text area content, but how do I update the default value for the field with the latest edit?我能够保存保存文本区域的内容,但是如何使用最新编辑更新该字段的默认值? I tried updating the 'value' and 'children' for the text area, but did not work.我尝试更新文本区域的“价值”和“孩子”,但没有奏效。 Here's the code.这是代码。

folder=r'C:\Temp'
def text_ar_val(file='TextBox1.txt'):
    with open(folder+fr'\Data\{file}','r') as f:
        return f.read()
app = dash.Dash(__name__)
app.layout = html.Div([dcc.Textarea(
                                id='textarea1', className='textarea1',
                                value=text_ar_val()
                            ),
                            html.Div(daq.StopButton(id='save1',className="button",
                                                    buttonText='Save', n_clicks=0))
                        ])
@app.callback(Output('textarea1', 'children'),
              [Input('save1', 'n_clicks'), Input('textarea1','value')])
def textbox1(n_clicks, value):
    if n_clicks>0:
        global folder
        with open(folder+r'\Data\TextBox1.txt','w') as file:
            file.write(value)
            return value

I also tried:我也试过:

@app.callback(Output('textarea1', 'value'),
          [Input('save1', 'n_clicks'), Input('textarea1','value')])

The dcc.Textarea module takes arguments persistence and persistence_type. dcc.Textarea 模块采用 arguments persistence 和 persistence_type。 Set persistence=True and persistence_type='local'.设置 persistence=True 和 persistence_type='local'。 Setting persistence to True will tell the application to save the last data entered and setting persistence_type to 'local' will save the last entered data to window.localStorage.将 persistence 设置为 True 将告诉应用程序保存最后输入的数据,将 persistence_type 设置为 'local' 会将最后输入的数据保存到 window.localStorage。 So when the user quits the browser and comes back, the filed will have a default value of the data last entered.因此,当用户退出浏览器并返回时,该字段将具有上次输入数据的默认值。 The code will be:代码将是:

app.layout = html.Div([dcc.Textarea(
                                id='textarea1', className='textarea1',
                                value=text_ar_val(),
                                persistence=True, persistence_type='local'
                            ),
                            html.Div(daq.StopButton(id='save1',className="button",
                                                    buttonText='Save', n_clicks=0))
                        ])

Everything about and below this code remains the same.这段代码前后的所有内容都保持不变。 Almost all the other dcc compoments will take in argument for persistence and persistence_type (eg. dcc.Dropdown).几乎所有其他 dcc 组件都将接受持久性和持久性类型的参数(例如 dcc.Dropdown)。

For more info, you can go to:有关更多信息,您可以 go 到:

https://dash.plotly.com/dash-core-components/textarea https://dash.plotly.com/dash-core-components/textarea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM