@@ -23,13 +23,17 @@ class CondaBuildPack(BaseImage):
2323
2424 """
2525
26- # The kernel environment file, if any.
26+ # The kernel conda environment file, if any.
2727 # As an absolute path within the container.
2828 _kernel_environment_file = ""
29+ # extra pip requirements.txt for the kernel
30+ _kernel_requirements_file = ""
2931
30- # The notebook server environment file, if any .
32+ # The notebook server environment file.
3133 # As an absolute path within the container.
3234 _nb_environment_file = ""
35+ # extra pip requirements.txt for the notebook env
36+ _nb_requirements_file = ""
3337
3438 def get_build_env (self ):
3539 """Return environment variables to be set.
@@ -38,18 +42,28 @@ def get_build_env(self):
3842 the `NB_PYTHON_PREFIX` to the location of the jupyter binary.
3943
4044 """
45+ if not self ._nb_environment_file :
46+ # get_build_scripts locates requirements/environment files
47+ self .get_build_scripts ()
48+
4149 env = super ().get_build_env () + [
4250 ("CONDA_DIR" , "${APP_BASE}/conda" ),
4351 ("NB_PYTHON_PREFIX" , "${CONDA_DIR}/envs/notebook" ),
4452 ("NB_ENVIRONMENT_FILE" , self ._nb_environment_file ),
4553 ]
46- if self .py2 :
54+ if self ._nb_requirements_file :
55+ env .append (("NB_REQUIREMENTS_FILE" , self ._nb_requirements_file ))
56+
57+ if self ._kernel_environment_file :
58+ # if kernel environment file is separate
4759 env .extend (
4860 [
4961 ("KERNEL_PYTHON_PREFIX" , "${CONDA_DIR}/envs/kernel" ),
5062 ("KERNEL_ENVIRONMENT_FILE" , self ._kernel_environment_file ),
5163 ]
5264 )
65+ if self ._kernel_requirements_file :
66+ env .append (("KERNEL_REQUIREMENTS_FILE" , self ._kernel_requirements_file ))
5367 else :
5468 env .append (("KERNEL_PYTHON_PREFIX" , "${NB_PYTHON_PREFIX}" ))
5569 return env
@@ -95,7 +109,7 @@ def get_build_scripts(self):
95109 r"""
96110 TIMEFORMAT='time: %3R' \
97111 bash -c 'time /tmp/install-miniforge.bash' && \
98- rm /tmp/install-miniforge.bash ${NB_ENVIRONMENT_FILE}
112+ rm -rf /tmp/install-miniforge.bash /tmp/env
99113 """ ,
100114 )
101115 ]
@@ -129,24 +143,37 @@ def get_build_script_files(self):
129143 # If no version is specified or no matching X.Y version is found,
130144 # the default base environment is used.
131145 frozen_name = "environment.lock"
146+ pip_frozen_name = "requirements.txt"
132147 if py_version :
133148 if self .py2 :
134149 # python 2 goes in a different env
135150 files [
136151 "conda/environment.py-2.7.lock"
137- ] = self ._kernel_environment_file = "/tmp/kernel-environment.lock"
152+ ] = self ._kernel_environment_file = "/tmp/env/kernel-environment.lock"
153+ # additional pip requirements for kernel env
154+ if os .path .exists (os .path .join (HERE , "requirements.py-2.7.txt" )):
155+ files [
156+ "conda/requirements.py-2.7.txt"
157+ ] = (
158+ self ._kernel_requirements_file
159+ ) = "/tmp/env/kernel-requirements.txt"
138160 else :
139- for ext in [".lock" , ".frozen.yml" ]:
140- py_frozen_name = f"environment.py-{ py_version } { ext } "
141- if os .path .exists (os .path .join (HERE , py_frozen_name )):
142- frozen_name = py_frozen_name
143- break
161+ py_frozen_name = f"environment.py-{ py_version } .lock"
162+ if os .path .exists (os .path .join (HERE , py_frozen_name )):
163+ frozen_name = py_frozen_name
164+ pip_frozen_name = f"requirements.py-{ py_version } .txt"
144165 if not frozen_name :
145166 self .log .warning (f"No frozen env for { py_version } " )
146- _ , frozen_ext = os .path .splitext (frozen_name )
147167 files [
148168 "conda/" + frozen_name
149- ] = self ._nb_environment_file = f"/tmp/environment{ frozen_ext } "
169+ ] = self ._nb_environment_file = "/tmp/env/environment.lock"
170+
171+ # add requirements.txt, if present
172+ if os .path .exists (os .path .join (HERE , pip_frozen_name )):
173+ files [
174+ "conda/" + pip_frozen_name
175+ ] = self ._nb_requirements_file = "/tmp/env/requirements.txt"
176+
150177 files .update (super ().get_build_script_files ())
151178 return files
152179
0 commit comments