Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quantstats_lumi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def _in_notebook(matplotlib_inline=False):
if shell == "ZMQInteractiveShell":
# Jupyter notebook or qtconsole
if matplotlib_inline:
get_ipython().magic("matplotlib inline")
get_ipython().run_line_magic("matplotlib", "inline")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant Matplotlib Backend Configuration category Performance

Tell me more
What is the issue?

The _in_notebook() function always executes the matplotlib magic command when matplotlib_inline=True, without caching the result or checking if it was already set.

Why this matters

Repeatedly setting matplotlib's backend is an unnecessary operation that adds overhead, especially when the function is called multiple times in a notebook session.

Suggested change ∙ Feature Preview

Cache the matplotlib backend setting state using a module-level variable to avoid redundant configurations:

_matplotlib_inline_configured = False

def _in_notebook(matplotlib_inline=False):
    global _matplotlib_inline_configured
    try:
        shell = get_ipython().__class__.__name__
        if shell == "ZMQInteractiveShell":
            if matplotlib_inline and not _matplotlib_inline_configured:
                get_ipython().run_line_magic("matplotlib", "inline")
                _matplotlib_inline_configured = True
            return True
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

return True
if shell == "TerminalInteractiveShell":
# Terminal running IPython
Expand Down