Cannot display figures when running from terminal/REPL

Open
#511 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

Start by locating the chart rendering or to_html entry point and review tqdm's autonotebook.py approach for detecting notebook execution. The work is complete when charts can open in a browser from a terminal or REPL while existing notebook behavior remains supported.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
1.8k
Forks
76
Avg merge
1h 7m
Merged PRs (30d)
8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from reflex-dev/xy

All issues in reflex-dev/xy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.