Use custom conda environment in Jupyter Notebook when user wants to use it (#2050)

* Modify the custom Python kernel

- to activate the custom environment
- for the respective Jupyter Notebook and Jupyter Console

Signed-off-by: Ayaz Salikhov <mathbunnyru@gmail.com>

* Add DL3059 to hadolint ignore list

Signed-off-by: Ayaz Salikhov <mathbunnyru@gmail.com>

* Move hadolint ignore to a single line

* Use python heredoc

* Remove unused print

* Fix style

* Do not hardcode CONDA_DIR

* Update custom_environment.dockerfile

* Use indent=1

* Implement activate_notebook_custom_env.py as a separate script

* Do not call Python manually

---------

Signed-off-by: Ayaz Salikhov <mathbunnyru@gmail.com>
Co-authored-by: Olivier Benz <olivier.benz@b-data.ch>
This commit is contained in:
Ayaz Salikhov
2023-12-08 03:03:32 +01:00
committed by GitHub
parent 01e2a4e0a9
commit c1d2127843
2 changed files with 31 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python3
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
import sys
from pathlib import Path
env_name = sys.argv[1]
CONDA_DIR = os.environ["CONDA_DIR"]
file = Path.home() / f".local/share/jupyter/kernels/{env_name}/kernel.json"
content = json.loads(file.read_text())
content["env"] = {
"XML_CATALOG_FILES": "",
"PATH": f"{CONDA_DIR}/envs/{env_name}/bin:$PATH",
"CONDA_PREFIX": f"{CONDA_DIR}/envs/{env_name}",
"CONDA_PROMPT_MODIFIER": f"({env_name}) ",
"CONDA_SHLVL": "2",
"CONDA_DEFAULT_ENV": env_name,
"CONDA_PREFIX_1": CONDA_DIR,
}
file.write_text(json.dumps(content, indent=1))