This is a rookie question. I’ve completed writing my first Python app (took months).
I am bringing it together in Dash, converting from Streamlit.
Now, I am suddenly receiving an error message I did not receive in Streamlit, as if the functions are running concurrently. I know that’s not possible in Python, but I am receiving mysterious error messages like this one:
"File “C:\Users\rober\ings\clean_recipe_text.py”, line 14, in clean_recipe_text
with open(“temp/fraction_free_recipe.txt”, “r”, encoding=‘utf-8’) as decimal_recipe:
FileNotFoundError: [Errno 2] No such file or directory: ‘temp/fraction_free_recipe.txt’
PS C:\Users\xxxxx\ings> "
This file does not exist because it cannot exist unless I submit a recipe, which it is not letting me do. Instead, it is running the last function in the code (so far) and looking for a file that does not exist yet. This has never happened before, and I am wondering have I missed something obvious? Or is this issue particular to Dash for some reason?
I’ve tried some solutions like forcing the function to wait until the submit button has been clicked at least once before it runs the code inside the functions, but that does not solve the problem. I would greatly appreciate any constructive feedback. I’ve decided to post the code below though it may seem like too much.
It was producing the correct results up until I added the last function.
app.layout = html.Div([
dcc.Textarea(
id='textarea-state-example',
value='',
style={'width': '100%', 'height': 200},
placeholder='Please note: Information inside brackets or parantheses will be deleted.'
),
html.Button('Submit', id='textarea-state-example-button', n_clicks=0),
html.Div(id='textarea-state-example-output',
style={'whiteSpace': 'pre-line'})
])
@app.callback(
Output('textarea-state-example-output', 'children'),
Input('textarea-state-example-button', 'n_clicks'),
State('textarea-state-example', 'value')
)
def update_recipe(n_clicks, value):
if n_clicks > 0:
with open("temp/recipe_contents.txt", "w", encoding='utf-8') as recipe_contents:
recipe = value.split('\n')
for line in recipe:
print(line)
recipe_contents.write(line)
recipe_contents.write('\n')
return "Recipe contents written to file."
def convert_fractions(n_clicks):
if n_clicks > 0:
vulgar_fractions = convert_unicode_fractions()
common_fractions = convert_regular_fractions()
print("Output: Vulgar Fractions:")
print(vulgar_fractions)
time.sleep(10)
print("")
print("Output: Common Fractions:")
print(common_fractions)
time.sleep(10)
print("")
def clean_recipe_textacy(n_clicks, filename):
if n_clicks > 0:
clean_recipe = clean_recipe_text()
print("Output: Clean Recipe Text:")
print(clean_recipe)
time.sleep(10)
print("")
clean_recipe_textacy('temp/fraction_free_recipe.txt')
if __name__ == '__main__':
app.run_server(debug=True, dev_tools_silence_routes_logging=False)