Intermittent KeyError with Dash-Plotly Python app deployed to Heroku

  heroku, hyphen, intermittent, keyerror, python

I have deployed a Dash-Plotly Python application to Heroku using Heroku’s "heroku git CLI" option. I followed Heroku’s instructions and the deployment itself was uneventful. In fact, the app works like its supposed to 75% of the time. (Also note that the app works perfectly when run locally.)

The app itself is designed to search a product database and display performance characteristics about a selected product. The user inputs search criteria into a webform and initiates the search with a "search" button press. All product data is stored local to the app in a "data" folder (excel files).

The issue I’m having is that the app "appears" un-responsive on Heroku, requiring multiple clicks on buttons or checkboxes to elicit the desired action.

When I dug into the logs, I see the following output after a button press:

2021-10-15T19:25:17.768450+00:00 app[web.1]: Exception on /_dash-update-component [POST]
2021-10-15T19:25:17.768474+00:00 app[web.1]: Traceback (most recent call last):
2021-10-15T19:25:17.768475+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
2021-10-15T19:25:17.768475+00:00 app[web.1]: response = self.full_dispatch_request()
2021-10-15T19:25:17.768476+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
2021-10-15T19:25:17.768476+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-10-15T19:25:17.768476+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
2021-10-15T19:25:17.768477+00:00 app[web.1]: rv = self.dispatch_request()
2021-10-15T19:25:17.768477+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
2021-10-15T19:25:17.768477+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2021-10-15T19:25:17.768478+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/dash/dash.py", line 1096, in dispatch
2021-10-15T19:25:17.768478+00:00 app[web.1]: response.set_data(func(*args, outputs_list=outputs_list))
2021-10-15T19:25:17.768478+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/dash/dash.py", line 1017, in add_context
2021-10-15T19:25:17.768479+00:00 app[web.1]: output_value = func(*args, **kwargs)  # %% callback invoked %%
2021-10-15T19:25:17.768479+00:00 app[web.1]: File "/app/productsearch.py", line 1329, in table_interact
2021-10-15T19:25:17.768480+00:00 app[web.1]: products = search_products(classname, inputs)
2021-10-15T19:25:17.768480+00:00 app[web.1]: File "/app/productsearch.py", line 943, in search_products
2021-10-15T19:25:17.768480+00:00 app[web.1]: elif (low == None or (product['freq-low'] <= low and high <= product['freq-high'])):
2021-10-15T19:25:17.768481+00:00 app[web.1]: KeyError: 'freq-low'

‘freq-low’ is a value input by the user before a "search" button press. I know the value is there because its manually entered into the webpage before pressing search.

Note that the logs above are for an example of the ‘freq-low’ variable causing a KeyError but the error will occur with other variables too. Its not simply limited to the code around the ‘freq-low’ variable.

The other important thing to note is that this behavior is not deterministic. Its not repeatable in the sense that it always fails on the first or second try etc. Sometimes it works right away and sometimes it takes 3-4 clicks. But so far, it ALWAYS works given enough clicks.

I would mention troubleshooting but so far I don’t know what to try since I’m not getting indication of any single variable or function failure.

Source: Python Questions

LEAVE A COMMENT