Repository navigation
Show diagrams from a terminal with draw() - #85
Merged
Merged
Conversation
draw() with no filename raised TypeError: it passed format to graphviz.Digraph.view, which takes none. It now shows the diagram inline in terminals that support the kitty graphics protocol (kitty, Ghostty; not inside tmux or screen), at a resolution that matches the terminal's text and fits the window, and otherwise opens a temporary PDF in the system viewer. draw(filename, view=True) rendered a second time through view(), which overwrote the file it had just written with the DOT source and opened <filename>.svg.pdf. It now opens the file it wrote.
Autoplectic
marked this pull request as ready for review
November 29, 2026 02:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
model.draw()with no filename has raisedTypeErrorsince the first commit: it passedformattographviz.Digraph.view(), which takes none. It now shows the diagram, which also works from a terminal (IPython or the plain REPL):TERM,TERM_PROGRAMandKITTY_WINDOW_ID, and not inside tmux or screen, which drop the escape sequences unless configured to pass them through. The PNG's dpi comes from the terminal's cell height (TIOCGWINSZ), so that the 11pt node labels come out about as tall as the terminal's text, and a Graphvizsizefits the diagram to the window, less two rows for the prompt;.svgcan be any app (LaTeXiT on one Mac).draw(filename, view=True)rendered a second time throughview(), which overwrote the file it had just written with the DOT source and opened<filename>.svg.pdf. It now opens the file it wrote.sofic/viz/_terminal.py.docs/viz.rstand the CHANGELOG describe the behavior.Tests
tests/test_viz.py: the viewer path (a real PDF, nothing on stdout), the inline path (escape sequences that decode to the PNG, terminal attributes applied),view=True(opens the SVG it wrote, no stray files), detection for kitty, Ghostty, Terminal.app, tmux, screen and a non-terminal stream, chunking (4096-character chunks, keys only on the first), and reading the window size through a real pty. Onmainthe threedrawtests fail for the reasons above.TERM=xterm-ghostty, 120 × 40 cells of 14 × 34 pixels) draws the mixed-state presentation of the butterfly process (7 states, long belief labels) as 65 well-formed chunks. The PNG is 1623 × 1004 pixels, inside the 1680 × 1292 left for it.ty check(exit 0) andsphinx-build -Wpass, as do the test files that touch visualization (320 passed, 2 skipped).