Skip to content

Commit

Permalink
fix: Add workaround for warning from nbconvert. (#19)
Browse files Browse the repository at this point in the history
When executing a notebook during export to HTML, `nbconvert` prints the
following warning:
```
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
```

The workaround is to temporarily set the environment variable according
to the suggestion in the warning. It can be safely disabled since we do
not need debugging.
  • Loading branch information
mbelak-dtml authored Jul 26, 2023
1 parent 3e4316a commit 2ea1c9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion edvart/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from edvart.report_sections.table_of_contents import TableOfContents
from edvart.report_sections.timeseries_analysis import TimeseriesAnalysis
from edvart.report_sections.univariate_analysis import UnivariateAnalysis
from edvart.utils import env_var


class ReportBase(ABC):
Expand Down Expand Up @@ -225,7 +226,10 @@ def _export_html(

html_exporter = nbconvert.HTMLExporter(**html_exp_kwargs)

html = html_exporter.from_notebook_node(nb)[0]
# Workaround for a warning from `nbconvert` regarding debugging
# and frozen modules. We are not debugging, so we can safely ignore it.
with env_var("PYDEVD_DISABLE_FILE_VALIDATION", "1"):
html = html_exporter.from_notebook_node(nb)[0]

# Save HTML to file
with open(html_filepath, "w") as html_file:
Expand Down
24 changes: 23 additions & 1 deletion edvart/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utils package."""

from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple, Union
import os
from contextlib import contextmanager
from typing import Any, Dict, Iterable, Iterator, List, Literal, Optional, Tuple, Union

import pandas as pd
import seaborn as sns
Expand Down Expand Up @@ -542,3 +544,23 @@ def contingency_table(df: pd.DataFrame) -> pd.DataFrame:
"""
table = sm.stats.Table.from_data(df)
return table.table_orig.astype(int)


@contextmanager
def env_var(name: str, value: str) -> Iterator[None]:
"""
Set an environment variable for the duration of the context.
Parameters
----------
name : str
Name of the environment variable.
value : str
Value of the environment variable.
"""
original_env = os.environ.copy()
os.environ[name] = value
try:
yield
finally:
os.environ = original_env
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import os
import warnings

import numpy as np
Expand Down Expand Up @@ -31,3 +32,11 @@ def test_full_na_series():
assert utils.is_numeric(series)
assert utils.is_categorical(series)
assert utils.num_unique_values(series) == 0


def test_env_var():
test_var_name = "TEST_VAR"
test_var_value = "test"
with utils.env_var(test_var_name, test_var_value):
assert os.environ[test_var_name] == test_var_value
assert test_var_value not in os.environ

0 comments on commit 2ea1c9b

Please sign in to comment.