Problem
In the instance that you have long-running Python code that needs to execute on Jupyter sessions (either JupyterLab or Jupyter Notebook) you may notice that the computation stops when you exit the browser tab or log out of Workbench. This is currently an issue with Jupyter itself, as seen below:
- Restoring computation output after disconnect in Notebook · Issue #641 · jupyter/notebook
- Reconnecting the kernel after a refresh does not capture the kernel output correctly · Issue #12455 · jupyterlab/jupyterlab
- Reconnect to running session: keeping output · Issue #2833 · jupyterlab/jupyterlab
- Real Time Collaboration · Issue #5382 · jupyterlab/jupyterlab
- How can I resume the long time running cell and continue output? · Issue #9621 · jupyterlab/jupyterlab
- Outputcell update when browser closed · Issue #4237 · jupyterlab/jupyterlab
- Support new kernels REST API · Issue #12867 · jupyterlab/jupyterlab
- Support server-side execution by davidbrochart · Pull Request #15448 · jupyterlab/jupyterlab
RStudio sessions, however, will keep running.
Solution
We do have a feature request for Jupyter sessions to keep running as background jobs even when the browser tab is closed. Unfortunately, the workarounds are not-so-graceful! The alternatives below are ways around this as a temporary workaround:
- Exporting the jupyter notebook as a python script (*.py) and running it in a terminal within tmux or screen to keep the process running after disconnecting the terminal.
- Using nbconvert or papermill to run the jupyter notebook from the command line, again in a terminal within tmux or screen.
- Converting jupyter notebook into an Rmarkdown document using Jupyter lab extension jupytext (and turning code cells into python chunks). Closing and re-opening the RStudio IDE while a .Rmd file is executing allows us to see the new outputs after reconnection (contrary to the Jupyter and VScode IDEs).
The limitation arises from the way the Jupyter kernel handles sessions with the browser. Whilst not official Posit documentation, the following link has some ideas for this workflow:
https://saturncloud.io/blog/long-running-notebooks/