Dash, save Input field values

Hey guys, so I have a dash-app that takes a bunch of inputs (at least 18), does some computation and appends the output to a CSV file. I’m using the submit button as a way of appending values to the csv file.

The problem is that every time I click on the submit button, my the page refreshes and as a result my Input fields are reset back to the default values. Is there a way to disable the page refresh or at the very least conserve the values on the Input fields?

Here is the code snippet responsible for appending the csv.

@app.callback(
Output(“nothing”, “data”),
[Input(“submit-button”, “n_clicks”)],
[State(“output_1”, “data”), State(“output_2”, “data”)],
)
def append_to_csv(n_clicks, output_1, output_2):
if n_clicks is None:
raise PreventUpdate
if (output_1 is None) or (output_2 is None):
raise PreventUpdate
else:
with open(“result.csv”, “a+”) as f:
f.write(f"{round(output_1,2)}, {round(output_2,2)}\n")
f.close()
return 1

Thanks!

Is your submit button inside an HTML form with a post action? If so you don’t need to do that, remove any form or form action.

Also if you want values to persist after a page refreshing anyway, look up the new persistence attribute: 📣 Dash 1.3.0 released

1 Like

yea the submit button is inside the html form, although I didn’t set any type of action or method.

persistence = True has solved my issue though. Thank you very very much!!

Awesome!

But if you don’t the page to refresh at all you can remove the form all together. The button will still work as it’s tied to the callback, it doesn’t rely on classical HTML POST/GET form actions.

1 Like