I am struggling to execute my functions in the intended sequence. By that, I mean each of the functions below depend on the creation of files by the preceding function in order to execute. I have reviewed the docs and viewed the videos about callbacks, but I don’t see much information about how to use them to control timing issues. I understand that’s possible, but what does it look like? As shown below, I tried to use the the "if file “exists” strategy, but it did not work. I still receive a “file not found” error message. I even tried to use the time.sleep() method to delay their execution, but that’s not a good solution. Not at all
Any constructive feedback is welcome.
app = dash.Dash(__name__)
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 submit_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_unicode_fractions(n_clicks, value):
if n_clicks > 0:
if os.path.exists('temp/recipe_contents.txt'):
print("recipe_contents.txt exists")
vulgar_fractions = convert_unicode_fractions()
print("Output: Convert Unicode Fractions")
print(vulgar_fractions)
print(" ")
return "Recipe is cleared of unicode fractions"
def convert_regular_fractions(n_clicks, value):
if n_clicks > 0:
if os.path.exists('temp/vulgar_free_recipe.txt'):
print("vulgar_free_recipe.txt exists")
common_fractions = convert_regular_fractions()
print("Output: Convert Unicode Fractions")
print(common_fractions)
print(" ")
return "Recipe is cleared of regular fractions"
def clean_recipe_textacy(n_clicks, value):
if n_clicks > 0:
if os.path.exists('temp/fraction_free_recipe.txt'):
print("fraction_free_recipe.txt exists")
clean_text = clean_recipe_text()
print("Output: Convert Unicode Fractions")
print(clean_text)
print(" ")
return "Recipe text is cleaned by textacy."
if __name__ == '__main__':
app.run_server(debug=True, dev_tools_silence_routes_logging=False)