Skip to content

Conversation

none2003
Copy link
Contributor

@none2003 none2003 commented Jun 24, 2025

refactor(utils): update matplotlib magic command for Jupyter compatibility

  • Replace deprecated magic method with run_line_magic for better Jupyter integration

Description by Korbit AI

What change is being made?

Update quantstats_lumi/utils.py to use get_ipython().run_line_magic() instead of get_ipython().magic() for specifying "matplotlib inline" in Jupyter notebooks.

Why are these changes being made?

This change addresses compatibility issues with IPython 9.x.x that prevent magic() from functioning as expected, ensuring that the library continues to provide inline plotting capabilities in newer Jupyter notebook environments.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

…ility

- Replace deprecated `magic` method with `run_line_magic` for better Jupyter integration
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Performance Redundant Matplotlib Backend Configuration ▹ view
Files scanned
File Path Reviewed
quantstats_lumi/utils.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant