Skip to content

Commit

Permalink
update the interactive repl heuristic
Browse files Browse the repository at this point in the history
The current heuristic doesn't detect ipython as an
interactive environment. In addition, forgetting
the `-i` during startup is a common UX error.

Because `sys.ps1` is documented to imply an interactive
environment, we can add it as an additional signal for our
heuristic. https://docs.python.org/3/library/sys.html#sys.ps1

A dataset gathered by the user `pyjamas` on StackOverflow
implies that this should work reasonably well:
https://stackoverflow.com/a/64523765
  • Loading branch information
ayorgo authored and jktr committed Sep 6, 2023
1 parent d81fa90 commit cbe4fe2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ the following sample code to draw a plot in your terminal.

```
$ export MPLBACKEND='module://matplotlib-backend-kitty'
$ python -i
$ python
>>> n = 10000
>>> df = pd.DataFrame({'x': np.random.randn(n),
'y': np.random.randn(n)})
Expand All @@ -38,16 +38,16 @@ $ python -i
```

If you set your matplotlib to interactive mode via
`matplotlib.pyplot.ion()` or by running python as
`python -i`, non-empty figures are drawn on construction
where possible. This allows you to use pandas' `plot()`
calls directly, without calling `plt.show()`, and still
`matplotlib.pyplot.ion()` or by running an interactive python
shell like `python` or `ipython`, non-empty figures are drawn on
construction where possible. This allows you to use pandas'
`plot()` calls directly, without calling `plt.show()`, and still
enables you to manually construct and `plt.show()`.

If your matplotlib is in non-interactive mode,
you can construct your figures as usual, and then call
`plt.show()` to render them to your terminal. This
works from both a repl and when running scripts.
works both from scripts and from a repl.

Figures are resized to the size of your terminal by default.
If you'd rather control the sizing of figures manually,
Expand Down
2 changes: 1 addition & 1 deletion matplotlib-backend-kitty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


# XXX heuristic for interactive repl
if sys.flags.interactive:
if hasattr(sys, 'ps1') or sys.flags.interactive:
interactive(True)


Expand Down

0 comments on commit cbe4fe2

Please sign in to comment.