Skip to content

Cannot display figures when running from terminal/REPL #511

Description

@x-tabdeveloping

I regularly use a REPL or a terminal to run Python code.
Currently, it is not possible to display your charts in a browser tab when running code from a terminal.
In plotly, you can call fig.show(), and the library will automatically detect that it is not running in an IPython notebook and will open the figure in a browser window.

Here is a workaround that would be relatively easy to add:

import time
import tempfile
from pathlib import Path
import webbrowser

def show_xy(chart, **kwargs):
    with tempfile.TemporaryDirectory() as temp_dir:
        # Saves chart to a temporary HTML file
        file_name = Path(temp_dir).joinpath("fig.html")
        chart.to_html(file_name, **kwargs)
        # Opens new browser tab with the chart.
        webbrowser.open("file://" + str(file_name.absolute()), new=2)
        # Makes sure the file is not deleted before the browser loads it.
        time.sleep(2)

It is also possible to detect whether the code is being run in a notebook. Here is tqdm's code to handle this.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions