gstaff commited on
Commit
fd3511e
β€’
1 Parent(s): 3df3aa7

Implement basic import auto-healing for streamlit.

Browse files
Files changed (1) hide show
  1. templates.py +8 -1
templates.py CHANGED
@@ -214,8 +214,15 @@ def update_iframe_js(demo_type: DemoType) -> str:
214
  }}
215
  const newCode = code + ` # Update tag ${{Math.random()}}`;
216
  const entrypointFile = "streamlit_app.py";
217
- // TODO: As code rerun happens inside streamlit this won't throw an error for self-healing imports
218
  await appController.writeFile(entrypointFile, newCode);
 
 
 
 
 
 
 
219
  }}
220
  catch (e) {{
221
  // If the error is caused by a missing module try once to install it and update again.
 
214
  }}
215
  const newCode = code + ` # Update tag ${{Math.random()}}`;
216
  const entrypointFile = "streamlit_app.py";
217
+ // As code rerun happens inside streamlit this won't throw an error for self-healing imports.
218
  await appController.writeFile(entrypointFile, newCode);
219
+ // So instead wait 500 milliseconds to see if the streamlit error banner appeared with an error.
220
+ // TODO: Consider a way to make this not rely on streamlit refresh timing; otherwise user can just re-update and this will trigger.
221
+ await new Promise(r => setTimeout(r, 500));
222
+ const messageDiv = document.getElementById('stlite-iframe').contentWindow.document.querySelector('.message');
223
+ if (messageDiv) {{
224
+ throw Error(messageDiv.innerHTML);
225
+ }}
226
  }}
227
  catch (e) {{
228
  // If the error is caused by a missing module try once to install it and update again.