JUPYTERLAB.adoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. == What is JupyterLab? ==
  2. === The Challenge ===
  3. Try the `wine-sklearn.py` workflow - have a look at the script and try to convince the second model, which you want to compare to the first one, to work.
  4. The example script for two model types using SciKit-Learn trains the first _SVC_ model, but the second model, _NuSVC_, is deliberately commented out because there is an issue with one of its parameters.
  5. The idea is to set up the sample data first, and then train both models on the same dataset.
  6. It's going to take a lot of re-running of the same code while testing it, commenting out certain chunks, hacking away at others, and ultimately a lot of time wasted for boilerplate stuff. You need to re-run the entire script every time you make a change, which is very awkward and time-consuming. Whenever you change something in the lead-up code, you will need to make sure it still works with both models and so on.
  7. If you try executing the same workflow in an interactive interpreter (just starting `python3` and then typing away), by copying the script to a Python shell line by line, it's even worse. It is extremely inconvenient.
  8. If you just want to return a couple of steps to change something about your data, and then re-run the training of a model, it is not very transparent what the state of your data is at the moment and what the correct order of steps should be.
  9. === The Solution ===
  10. JupyterLab Notebooks were designed to resolve those problems by being something in between. You can run them as a script, but you can also run individual blocks of a notebook called _cells_ in isolation.
  11. You can conveniently mix documentation markup with the cells to provide guidance as to how to run the cells and what care should be taken when doing so. Think of those as comments on steroids.
  12. It gets better - you can define different Python kernels which belong to various Conda environments, in the same JupyterLab instance, and simply associate your notebooks with the kernel they need, so that they can run in whichever environment you want them to.
  13. If you want to use your environments that way, the best way to do it is to install `jupyterlab` into the base environment.
  14. === Installing It All ===
  15. [subs="+quotes"]
  16. ----
  17. (_whatever_) $ *mamba activate base*
  18. (base) $ *pip install jupyterlab*
  19. Collecting jupyterlab
  20. ...
  21. Successfully installed MarkupSafe-3.0.2 ... _omitted_ ... websocket-client-1.8.0
  22. ----
  23. Starting Jupyter will automatically open it in your browser.
  24. [subs="+quotes"]
  25. ----
  26. (base) $ *jupyter lab*
  27. [I 2025-04-07 14:54:37.059 ServerApp] jupyter_lsp | extension was successfully linked.
  28. ...
  29. [I 2025-04-07 14:54:39.694 LabApp] Build is up to date
  30. ----
  31. .JupyterLab Web Application
  32. image::images/jupyterlab-welcome.png[width="90%"]
  33. If you want to reopen it at any later point, you can point your browser to `\http://localhost:8888/lab` and it will reload the last state of the workbench before you closed it.
  34. There are a couple of additional startup options you can use, the two most convenient are listed below.
  35. * `--no-browser`, do not open a browser tab/window
  36. * `--notebook-dir=_path_`, where to load notebooks from
  37. You can also control Jupyter server with the `jupyter-server` command.
  38. [subs="+quotes"]
  39. ----
  40. (base) $ *jupyter-server list*
  41. Currently running servers:
  42. http://localhost:8888/?token=xxxx :: /foo/bar/ml-demo
  43. (base) $ *jupyter-server stop*
  44. [JupyterServerStopApp] Shutting down server on 8888...
  45. ----
  46. === Adding Conda Environments to JupyterLab ===
  47. Introduce Jupyter Kernels into each of the conda environments (except `base`, which already has JupyterLab).
  48. [subs="+quotes"]
  49. ----
  50. (base) $ *mamba activate sklearn-16*
  51. (sklearn-16) $ *pip install ipykernel*
  52. Collecting ipykernel
  53. ...
  54. Successfully installed appnope-0.1.4 ... _omitted_ ... wcwidth-0.2.13
  55. ----
  56. Then, while JupyterLab is running, tell the `ipykernel` module to register itself with the default server.
  57. [subs="+quotes"]
  58. ----
  59. (sklearn-16) $ *python -mipykernel install --user --name=sklearn-16*
  60. Installed kernelspec sklearn-16 in /foo/bar/baz/Jupyter/kernels/sklearn-16
  61. ----
  62. Do the same for the other environments.
  63. [subs="+quotes"]
  64. ----
  65. (sklearn-16) $ *mamba activate pytorch-26*
  66. (pytorch-26) $ *pip install ipykernel*
  67. ...
  68. (pytorch-26) $ *python -mipykernel install --user --name=pytorch-26*
  69. ...
  70. (pytorch-26) $ *mamba activate tf-216*
  71. (tf-216) $ *pip install ipykernel*
  72. ...
  73. (tf-216) $ *python -mipykernel install --user --name=tf-216*
  74. ...
  75. ----
  76. As you register additional kernels, you will see them show up in the JupyterLab web application.
  77. .JupyterLab With Installed Kernels
  78. image::images/jupyterlab-kernels.png[width="90%"]
  79. Once you open a notebook, you can select the kernel you need to run it with in the top-right corner menu.
  80. .Kernel Selection for a SciKit-Learn 1.6 Notebook
  81. image:images/jupyterlab-notebook.png[width="100%"]