From 664ad2725d86da710f7c072fd01f4a64605c5e3d Mon Sep 17 00:00:00 2001 From: shibanib Date: Wed, 20 Sep 2023 11:05:16 -0700 Subject: [PATCH 1/3] update docs for the polyglot notebooks to include using venvs --- docs/jupyter-in-polyglot-notebooks.md | 67 +++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/docs/jupyter-in-polyglot-notebooks.md b/docs/jupyter-in-polyglot-notebooks.md index ee374c45c4..fc50a0ab32 100644 --- a/docs/jupyter-in-polyglot-notebooks.md +++ b/docs/jupyter-in-polyglot-notebooks.md @@ -6,13 +6,20 @@ This feature is currently in preview. ## Setup Before you begin, make sure you have the following installed: -1. [The Anaconda distribution](https://docs.anaconda.com/free/anaconda/install/index.html) -2. [Python](https://www.python.org/downloads/) and/or [R](https://cran.r-project.org/) +1. [The Anaconda distribution](https://docs.anaconda.com/free/anaconda/install/index.html). Comes with Python and Jupyter. +2. OR Install [Python](https://www.python.org/downloads/) and add to your PATH. You would need to install [Jupyter](https://jupyter.org/install#jupyter-notebook) +3. If you are working with R - Install [R](https://cran.r-project.org/) Please note that if you want to use Python and R in Polyglot Notebooks, you must open VS Code from the Anaconda prompt. Open the Anaconda prompt and activate the environment you want to work in. Then navigate to the desired folder and open VS code from the Anaconda prompt using the `code .` command. ## Connecting to a Python kernel Run the following command in a notebook cell: +If working with Anaconda: +``` +#!connect jupyter --kernel-name pythonkernel --kernel-spec python3 +``` + +If working with Python and Jupyter directly: ``` #!connect jupyter --kernel-name pythonkernel --kernel-spec python3 ``` @@ -30,7 +37,12 @@ install.packages('IRkernel') IRkernel::installspec() ``` -Then restart VS Code from the Anaconda prompt, and run the following command in a notebook cell: +You might need to restart VSCode. +If working with Anaconda, run the following command in a notebook cell: +``` +#!connect jupyter --kernel-name Rkernel --kernel-spec ir +``` +If working with Jupyter directly: ``` #!connect jupyter --kernel-name Rkernel --kernel-spec ir ``` @@ -42,4 +54,51 @@ To connect to a remote Jupyter server, run this command in a notebook cell: ``` #!connect jupyter --url --token --kernel-name pythonkernel --kernel-spec python3 ``` -For R, run the same command but replace `python3` with `ir` under `--kernel-spec` and give a new name for `kernel-name`. \ No newline at end of file +For R, run the same command but replace `python3` with `ir` under `--kernel-spec` and give a new name for `kernel-name`. + + +## Using Virtual environments + +Both with Python venv and Conda environments, you can create the environments and add them to Jupyter as a kernel spec. + +For Python venv, run the following commands in the terminal: +``` +python3 -m venv myenv +myenv\Scripts\activate + +pip install ipykernel +python -m ipykernel install --user --name=myenv +``` + +For Conda, run the following commands in the terminal or Anaconda Bash Prompt (Windows): +``` +conda create -n myenv +conda activate myenv + +conda install ipykernel +python -m ipykernel install --user --name=myenv +``` + +These environments can then be accessible as a kernel-spec in `#connect` command. + +Additionally, for Conda environments, you can use the `--conda-env` option in the `#connect` command to use the environment. + +For example, if you create a conda environment `condaenvpython3.9` and use the python3.9 version. +``` +conda create -n condaenvpython3.9 python==3.9 +conda activate condaenvpython3.9 + +conda install ipykernel +python -m ipykernel install --user --name=condaenvpython3.9 +``` + +You can target it using the following command and be able to use python==3.9 in your notebook. +``` +#!connect jupyter --kernel-name pythonkernel --conda-env condaenvpython3.9 --kernel-spec python3 +``` + +Or, you can also do this by adding `condaenvpython3.9` as a kernel spec to Jupyter and then using the `--kernel-spec` option to target it. + +``` +#!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec condaenvpython3.9 +``` \ No newline at end of file From cd75d2cf7af346ec276394e9ed961a08994e82f5 Mon Sep 17 00:00:00 2001 From: shibanib Date: Wed, 20 Sep 2023 13:40:00 -0700 Subject: [PATCH 2/3] fix merge issues --- docs/jupyter-in-polyglot-notebooks.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/jupyter-in-polyglot-notebooks.md b/docs/jupyter-in-polyglot-notebooks.md index 27c29b35b6..28260a3ce7 100644 --- a/docs/jupyter-in-polyglot-notebooks.md +++ b/docs/jupyter-in-polyglot-notebooks.md @@ -14,12 +14,12 @@ Before you begin, make sure you have the following installed: Run the following command in a notebook cell: If working with Anaconda: ``` -#!connect jupyter --kernel-name pythonkernel --kernel-spec python3 +#!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec python3 ``` If working with Python and Jupyter directly: ``` -#!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec python3 +#!connect jupyter --kernel-name pythonkernel --kernel-spec python3 ``` Once connected, create a new cell and select your Python kernel from the kernel picker in the bottom right hand corner of the cell. @@ -38,11 +38,11 @@ IRkernel::installspec() You might need to restart VSCode. If working with Anaconda, run the following command in a notebook cell: ``` -#!connect jupyter --kernel-name Rkernel --kernel-spec ir +#!connect jupyter --kernel-name Rkernel --conda-env base --kernel-spec ir ``` If working with Jupyter directly: ``` -#!connect jupyter --kernel-name Rkernel --conda-env base --kernel-spec ir +#!connect jupyter --kernel-name Rkernel --kernel-spec ir ``` Once connected, create a new cell and select your R kernel from the kernel picker. @@ -81,7 +81,7 @@ These environments can then be accessible as a kernel-spec in `#connect` command Additionally, for Conda environments, you can use the `--conda-env` option in the `#connect` command to use the environment. -For example, if you create a conda environment `condaenvpython3.9` and use the python3.9 version. +For example, if you create a conda environment `condaenvpython3.9` to use the python==3.9 version: ``` conda create -n condaenvpython3.9 python==3.9 conda activate condaenvpython3.9 @@ -95,7 +95,7 @@ You can target it using the following command and be able to use python==3.9 in #!connect jupyter --kernel-name pythonkernel --conda-env condaenvpython3.9 --kernel-spec python3 ``` -Or, you can also do this by adding `condaenvpython3.9` as a kernel spec to Jupyter and then using the `--kernel-spec` option to target it. +Or, you can get similar experience by adding `condaenvpython3.9` as a kernel spec to Jupyter and then using the `--kernel-spec` option to target it. ``` #!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec condaenvpython3.9 From 95129fb77b646a51b358794dffa4571ff7fa1602 Mon Sep 17 00:00:00 2001 From: shibanib Date: Wed, 20 Sep 2023 16:34:17 -0700 Subject: [PATCH 3/3] update based on feedback --- docs/jupyter-in-polyglot-notebooks.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/jupyter-in-polyglot-notebooks.md b/docs/jupyter-in-polyglot-notebooks.md index 28260a3ce7..c6a69143ba 100644 --- a/docs/jupyter-in-polyglot-notebooks.md +++ b/docs/jupyter-in-polyglot-notebooks.md @@ -12,12 +12,13 @@ Before you begin, make sure you have the following installed: ## Connecting to a Python kernel Run the following command in a notebook cell: -If working with Anaconda: + +If working with Jupyter using Anaconda: ``` #!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec python3 ``` -If working with Python and Jupyter directly: +If working with Python and Jupyter directly without Anaconda: ``` #!connect jupyter --kernel-name pythonkernel --kernel-spec python3 ``` @@ -35,12 +36,13 @@ install.packages('IRkernel') IRkernel::installspec() ``` -You might need to restart VSCode. -If working with Anaconda, run the following command in a notebook cell: +If you installed a new kernelspec or added new environment variables, you will need to restart VSCode. + +If working with Jupyter using Anaconda, run the following command in a notebook cell: ``` #!connect jupyter --kernel-name Rkernel --conda-env base --kernel-spec ir ``` -If working with Jupyter directly: +If working with Jupyter directly without Anaconda: ``` #!connect jupyter --kernel-name Rkernel --kernel-spec ir ```