Here are some ideas for using the Python (Python3) language with DomTerm. I am not a Python expert, so recommendations for better ways to do these things are welcome.
In your shell startup file (e.g
export PYTHONSTARTUP=$HOME/.config/python/python-startup.py export PYTHONPATH=$HOME/.config/python
Copy the following to
import os import sys is_domterm = os.getenv("DOMTERM", "")!="" session_key = 'Py%d' % os.getpid() sys.ps1 = '>>> ' sys.ps2 = '... ' if is_domterm : sys.ps1 = '\001\033]133;N;aid=' + session_key + ';cl=m\007\002'+ sys.ps1 + '\001\033]133;I\007\002' sys.ps2 = '\001\033]133;P;k=c\007\002' + sys.ps2 + '\001\033]133;I\007\002' import domterm.utils as dtutils dtutils.set_notebook_mode()
sys.ps2 wraps the prompts
with DomTerm-specific escape sequences that provide some benefits:
Logical and visual separation between commands, with different colors for prompt, input, and output.
A “fold” (hide/show) mini-button (in the right column):
Clicking on the downward triangle hides all but the first line of the command.
You can see that for the command starting
Input lines beyond the first plus all the output is hidden,
and the button changed to a right arrow.
Clicking with the mouse within the current input line causes the editing position in the line to be changed.
Installing domterm/utils.py defines the following useful functions.
html_stringto the terminal, and display it as would be done in a browser.
print_html, but supports replace-in-place of existing output: If
False, it wraps the
htmlin another element (a
<div>otherwise), and attaches a key
nameto that wrapper element. (This key does not use the
idattribute, and need not be unique.)
True, DomTerm searches for the most recent element that has a key matching
name. If found, it replaces the contents (child nodes) of that wrapper element with
Controls how auto-display (the “print” part of the Python REPL) handles objects that have a
enableis True, then
_repr_html_is called, and the result is printed as if by
dtutils.print_html. Otherwise, the default display is done. The
python-startup.pyabove starts out with
domterm.utils module overrides Python’s
as show in the screenshot. The error message is wrapped in
<span std="err"> elements, which is by default styled with red.
Lines of the form
are converted to clickable links (visible when you hover over it).
(Note the effective URL in the blue box.)
Right-clicking on the link brings up an
Open Link menu entry,
which normally brings up a text editor at the specified line;
you can customize this action.
domterm.utils module overrides the
uses for printing expression results.
It replaces the default
repr with one that checks if an
object has a
if so it uses that instead of the
Lists, tuples, and dictionaries are pretty-printed. The width used for line-breaking is the current terminal line-width; if the terminal width is changed, previously printed lines are automatically re-formatted. The screenshot shows the same terminal session in two panes with different widths.
Here is an example using Pandas, the Pandas Data Analysis Library.
Note how a
DataFrame by default is displayed as an HTML table
dtutils.set_notebook_mode(True) has been called).
Matplotlib is popular plotting library. If you do:
it will “patch” matplotlib so graphical output is displayed inline in a DomTerm terminal. (Note that domterm.matplot is very experimental, so the API may change.)
Display the current image inline. By default, the image is presented using SVG (Structured Vector Graphics), but you can also specify the
overwriteargument specifies whether to create a new display area (
overwrite=False) or re-use the most recent previous one (if (
overwrite=True); the default is
Falsefor the first call to
Truefor subsequent calls.