From d805bdf447558c779d97e2e91df82cac5937e060 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Fri, 17 Jan 2025 11:16:39 -0500 Subject: [PATCH] Default to not caching source in notebooks --- CHANGELOG.md | 1 + large_image/config.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eecd6f558..42b92bbe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Provide some latitude in vips multiframe detection ([#1770](../../pull/1770)) - Don't read multiplane ndpi files with openslide ([#1772](../../pull/1772)) - Harden sources based on more fuzz testing ([#1774](../../pull/1774)) +- Default to not caching source in notebooks ([#1776](../../pull/1776)) ### Bug Fixes diff --git a/large_image/config.py b/large_image/config.py index e237834b9..f1975ac8e 100644 --- a/large_image/config.py +++ b/large_image/config.py @@ -21,6 +21,23 @@ fallbackLogHandler.setLevel(logging.NOTSET) fallbackLogger.addHandler(fallbackLogHandler) + +def _in_notebook() -> bool: + """ + Try to detect if we are in an interactvie notebook. + + :returns: True if we think we are in an interactive notebook. + """ + try: + shell = get_ipython().__class__.__name__ # type: ignore[name-defined] + # Jupyter + if shell == 'ZMQInteractiveShell': + return True + return False + except NameError: + return False + + ConfigValues = { 'logger': fallbackLogger, 'logprint': fallbackLogger, @@ -40,7 +57,7 @@ # substantial performance penalties if sources are used multiple times, so # should only be set in singular dynamic environments such as experimental # notebooks. - 'cache_sources': True, + 'cache_sources': not _in_notebook(), # Generally, these keys are the form of "cache__"