diff --git a/muutils/nbutils/configure_notebook.py b/muutils/nbutils/configure_notebook.py index a67a2805..0bc0bb29 100644 --- a/muutils/nbutils/configure_notebook.py +++ b/muutils/nbutils/configure_notebook.py @@ -25,6 +25,7 @@ # handling figures PlottingMode = typing.Literal["ignore", "inline", "widget", "save"] PLOT_MODE: PlottingMode = "inline" +CONVERSION_PLOTMODE_OVERRIDE: PlottingMode | None = None FIG_COUNTER: int = 0 FIG_OUTPUT_FMT: str | None = None FIG_NUMBERED_FNAME: str = "figure-{num}" @@ -69,10 +70,16 @@ def setup_plots( close_after_plotshow: bool = False, ) -> None: """Set up plot saving/rendering options""" - global PLOT_MODE, FIG_COUNTER, FIG_OUTPUT_FMT, FIG_NUMBERED_FNAME, FIG_CONFIG, FIG_BASEPATH, CLOSE_AFTER_PLOTSHOW + global PLOT_MODE, CONVERSION_PLOTMODE_OVERRIDE, FIG_COUNTER, FIG_OUTPUT_FMT, FIG_NUMBERED_FNAME, FIG_CONFIG, FIG_BASEPATH, CLOSE_AFTER_PLOTSHOW + + # set plot mode, handling override + if CONVERSION_PLOTMODE_OVERRIDE is not None: + # override if set + PLOT_MODE = CONVERSION_PLOTMODE_OVERRIDE + else: + # otherwise use the given plot mode + PLOT_MODE = plot_mode - # set plot mode - PLOT_MODE = plot_mode FIG_COUNTER = 0 CLOSE_AFTER_PLOTSHOW = close_after_plotshow @@ -81,7 +88,9 @@ def setup_plots( ipython = get_ipython() ipython.magic("matplotlib inline") else: - raise RuntimeError("Cannot use inline plotting outside of Jupyter") + raise RuntimeError( + f"Cannot use inline plotting outside of Jupyter\n{PLOT_MODE = }\t{CONVERSION_PLOTMODE_OVERRIDE = }" + ) return elif PLOT_MODE == "widget": if IN_JUPYTER: diff --git a/muutils/nbutils/convert_ipynb_to_script.py b/muutils/nbutils/convert_ipynb_to_script.py index 5abbde53..dedfd181 100644 --- a/muutils/nbutils/convert_ipynb_to_script.py +++ b/muutils/nbutils/convert_ipynb_to_script.py @@ -34,6 +34,11 @@ def new_render( render = new_render # ------------------------------------------------------------ +""" + ], + "muutils": [ + """import muutils.nbutils.configure_notebook as nb_conf +nb_conf.CONVERSION_PLOTMODE_OVERRIDE = "ignore" """ ], } @@ -53,6 +58,10 @@ def disable_plots_in_script(script_lines: list[str]) -> list[str]: """Disable plots in a script by adding cursed things after the import statements""" result_str_TEMP: str = "\n\n".join(script_lines) script_lines_new: list[str] = script_lines + + if "muutils" in result_str_TEMP: + script_lines_new = DISABLE_PLOTS["muutils"] + script_lines_new + if "matplotlib" in result_str_TEMP: assert ( "import matplotlib.pyplot as plt" in result_str_TEMP diff --git a/pyproject.toml b/pyproject.toml index b4584326..41b12953 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "muutils" -version = "0.5.4" +version = "0.5.5" description = "A collection of miscellaneous python utilities" license = "GPL-3.0-only" authors = ["mivanit "]