Skip to content

Commit d04416b

Browse files
authored
Merge pull request #187 from andreytaboola/master
Add support of keeping the local context of the python scripts and notebooks by preserving working directory
2 parents eeccf25 + 6a0c2fa commit d04416b

File tree

14 files changed

+166
-8
lines changed

14 files changed

+166
-8
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ jobs:
220220
PYTHON_VERSION: "3_6"
221221
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts/3_6
222222
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results/3_6
223-
VERSION: 0.6.2
223+
VERSION: 0.6.3
224224
PANDOC_RELEASES_URL: https://github.com/jgm/pandoc/releases
225225
YARN_STATIC_DIR: notebooker/web/static/
226226
IMAGE_NAME: mangroup/notebooker
@@ -236,7 +236,7 @@ jobs:
236236
environment:
237237
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts/3_7
238238
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results/3_7
239-
VERSION: 0.6.2
239+
VERSION: 0.6.3
240240
PANDOC_RELEASES_URL: https://github.com/jgm/pandoc/releases
241241
YARN_STATIC_DIR: notebooker/web/static/
242242
IMAGE_NAME: mangroup/notebooker
@@ -250,7 +250,7 @@ jobs:
250250
environment:
251251
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts/3_8
252252
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results/3_8
253-
VERSION: 0.6.2
253+
VERSION: 0.6.3
254254
PANDOC_RELEASES_URL: https://github.com/jgm/pandoc/releases
255255
YARN_STATIC_DIR: notebooker/web/static/
256256
IMAGE_NAME: mangroup/notebooker
@@ -264,7 +264,7 @@ jobs:
264264
environment:
265265
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts/3_11
266266
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results/3_11
267-
VERSION: 0.6.2
267+
VERSION: 0.6.3
268268
PANDOC_RELEASES_URL: https://github.com/jgm/pandoc/releases
269269
YARN_STATIC_DIR: notebooker/web/static/
270270
IMAGE_NAME: mangroup/notebooker

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.6.3 (2024-07-11)
2+
------------------
3+
* Feature: Flag to preserve original working directory when running notebooks to make local imports and relative paths work.
4+
15
0.6.2 (2024-05-02)
26
------------------
37
* Bugfix: Folders with spaces in their names are now correctly handled in the webapp.

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
author = "Man Group Quant Tech"
2424

2525
# The full version, including alpha/beta/rc tags
26-
release = "0.6.2"
26+
release = "0.6.3"
2727

2828

2929
# -- General configuration ---------------------------------------------------

notebooker/_entrypoints.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def filesystem_default_value(dirname):
6464
is_flag=True,
6565
help="If selected, notebooker will not try to pull the latest version of python templates from git.",
6666
)
67+
@click.option(
68+
"--execute-at-origin",
69+
default=False,
70+
is_flag=True,
71+
help="If selected, notebooker set current working directory to absolute path of the notebook to keep it local context available",
72+
)
6773
@click.option(
6874
"--default-mailfrom", default=DEFAULT_MAILFROM_ADDRESS, help="Set a new value for the default mailfrom setting."
6975
)
@@ -84,6 +90,7 @@ def base_notebooker(
8490
py_template_base_dir,
8591
py_template_subdir,
8692
notebooker_disable_git,
93+
execute_at_origin,
8794
default_mailfrom,
8895
running_timeout,
8996
serializer_cls,
@@ -98,6 +105,7 @@ def base_notebooker(
98105
PY_TEMPLATE_BASE_DIR=py_template_base_dir,
99106
PY_TEMPLATE_SUBDIR=py_template_subdir,
100107
NOTEBOOKER_DISABLE_GIT=notebooker_disable_git,
108+
EXECUTE_AT_ORIGIN=execute_at_origin,
101109
DEFAULT_MAILFROM=default_mailfrom,
102110
RUNNING_TIMEOUT=running_timeout,
103111
)

notebooker/execute_notebook.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def _run_checks(
4848
email_subject: Optional[str] = "",
4949
prepare_only: Optional[bool] = False,
5050
notebooker_disable_git: bool = False,
51+
execute_at_origin: bool = False,
5152
py_template_base_dir: str = "",
5253
py_template_subdir: str = "",
5354
scheduler_job_id: Optional[str] = None,
@@ -82,6 +83,10 @@ def _run_checks(
8283
Comma-separated email addresses to send on error.
8384
prepare_only : `Optional[bool]`
8485
Internal usage. Whether we want to do everything apart from executing the notebook.
86+
notebooker_disable_git : `bool`
87+
Whether to disable git pulling of the notebook templates.
88+
execute_at_origin : `bool`
89+
Whether to execute the notebook at the original path of the template notebook.
8590
scheduler_job_id : `Optional[str]`
8691
If available, it will be part of the Error or Completed run report.
8792
mailfrom : `Optional[str]`
@@ -111,8 +116,18 @@ def _run_checks(
111116
ipynb_executed_path = os.path.join(output_dir, output_ipynb)
112117

113118
logger.info("Executing notebook at {} using parameters {} --> {}".format(ipynb_raw_path, overrides, output_ipynb))
119+
working_dir = None
120+
if execute_at_origin:
121+
working_dir = os.path.dirname(os.path.join(py_template_dir, template_name))
122+
logger.info("Setting working directory for execution {}".format(working_dir))
123+
114124
pm.execute_notebook(
115-
ipynb_raw_path, ipynb_executed_path, parameters=overrides, log_output=True, prepare_only=prepare_only
125+
ipynb_raw_path,
126+
ipynb_executed_path,
127+
parameters=overrides,
128+
log_output=True,
129+
prepare_only=prepare_only,
130+
cwd=working_dir,
116131
)
117132
with open(ipynb_executed_path, "r") as f:
118133
raw_executed_ipynb = f.read()
@@ -163,6 +178,7 @@ def run_report(
163178
hide_code=False,
164179
prepare_only=False,
165180
notebooker_disable_git=False,
181+
execute_at_origin=False,
166182
py_template_base_dir="",
167183
py_template_subdir="",
168184
scheduler_job_id=None,
@@ -200,6 +216,7 @@ def run_report(
200216
hide_code=hide_code,
201217
prepare_only=prepare_only,
202218
notebooker_disable_git=notebooker_disable_git,
219+
execute_at_origin=execute_at_origin,
203220
py_template_base_dir=py_template_base_dir,
204221
py_template_subdir=py_template_subdir,
205222
scheduler_job_id=scheduler_job_id,
@@ -353,6 +370,7 @@ def execute_notebook_entrypoint(
353370
output_dir, template_dir, _ = initialise_base_dirs(output_dir=config.OUTPUT_DIR, template_dir=config.TEMPLATE_DIR)
354371
all_overrides = _get_overrides(overrides_as_json, iterate_override_values_of)
355372
notebooker_disable_git = config.NOTEBOOKER_DISABLE_GIT
373+
execute_at_origin = config.EXECUTE_AT_ORIGIN
356374
py_template_base_dir = config.PY_TEMPLATE_BASE_DIR
357375
py_template_subdir = config.PY_TEMPLATE_SUBDIR
358376

@@ -376,6 +394,7 @@ def execute_notebook_entrypoint(
376394
logger.info("prepare_notebook_only = %s", prepare_notebook_only)
377395
logger.info("scheduler job id = %s", scheduler_job_id)
378396
logger.info("notebooker_disable_git = %s", notebooker_disable_git)
397+
logger.info("execute_at_origin = %s", execute_at_origin)
379398
logger.info("py_template_base_dir = %s", py_template_base_dir)
380399
logger.info("py_template_subdir = %s", py_template_subdir)
381400
logger.info("serializer_cls = %s", config.SERIALIZER_CLS)
@@ -402,6 +421,7 @@ def execute_notebook_entrypoint(
402421
hide_code=hide_code,
403422
prepare_only=prepare_notebook_only,
404423
notebooker_disable_git=notebooker_disable_git,
424+
execute_at_origin=execute_at_origin,
405425
py_template_base_dir=py_template_base_dir,
406426
py_template_subdir=py_template_subdir,
407427
scheduler_job_id=scheduler_job_id,
@@ -532,6 +552,7 @@ def run_report_in_subprocess(
532552
base_config.DEFAULT_MAILFROM,
533553
]
534554
+ (["--notebooker-disable-git"] if base_config.NOTEBOOKER_DISABLE_GIT else [])
555+
+ (["--execute-at-origin"] if base_config.EXECUTE_AT_ORIGIN else [])
535556
+ ["--serializer-cls", result_serializer.__class__.__name__]
536557
+ result_serializer.serializer_args_to_cmdline_args()
537558
+ [

notebooker/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class BaseConfig:
2525
# or list the available templates.
2626
NOTEBOOKER_DISABLE_GIT: bool = False
2727

28+
# A boolean flag to dictate whether we should execute the notebook at the origin or not.
29+
EXECUTE_AT_ORIGIN: bool = False
30+
2831
# The serializer class we are using for storage, e.g. PyMongoResultSerializer
2932
SERIALIZER_CLS: DEFAULT_SERIALIZER = None
3033
# The dictionary of parameters which are used to initialize the serializer class above

notebooker/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.6.2"
1+
__version__ = "0.6.3"

notebooker/web/static/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notebooker",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"description": "Notebooker - Turn notebooks into reports",
55
"dependencies": {
66
"bootstrap-table": "1.20.2",

tests/regression/local_context/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello, World!
2+
This is a sample text file.

0 commit comments

Comments
 (0)