from fastcore.test import *
from basedpl import Array, bpl
from IPython.utils.capture import capture_output
import numpy as npNotebook magics
BPL adds %bpl and %%bpl magics to IPython. Both magics evaluate code in basedpl.bpl, the workspace also used by Python calls through bpl.
%bpl returns a basedpl.Array or a basedpl.Function. %%bpl displays what the BPL prompt would show, including implicit expression results. Registering the magics gives basedpl.bpl the BPL prompt’s display settings: matrices, nested arrays and strings are shown in boxes, and functions as trees. The notebook’s rich display of Python values uses the same settings, because it reads the same workspace.
Rendering output
AplOut._repr_html_ HTML-escapes the text and wraps it in <pre class="bpl_out">, which preserves BPL spacing.
out = AplOut('1 < 2')
assert '<' in out._repr_html_()
out1 < 2
The magics
Each call to %bpl or %%bpl displays a JavaScript snippet that calls the page’s loadBplInput function when the page has one, and otherwise imports the BPL input method from a CDN. The browser keeps one symbol bar, including when a Solveit dialog is reopened on the same kernel.
%bpl displays any explicit output as BPL produces it, and returns the expression’s native value. Ending a %%bpl cell with ; suppresses all of its display while still evaluating the cell. Output produced before an error is displayed before the error is raised.
Press Tab to complete user and system names in %bpl and %%bpl. Tab completion reads the workspace without evaluating anything.
%%bpl displays the result of each expression. Assignments are shy and display nothing.
%%bpl
m←2 3ρι6
m×10┌→───────┐ ↓ 0 10 20│ │30 40 50│ └~───────┘
%bpl returns a basedpl.Array. Use .np to convert it to a NumPy array or .py to convert it to a Python value. BPL parses comments itself. A ⍝ inside a quoted string doesn’t start a comment.
z = %bpl m ⍝ a matrix
test_is(type(z), Array)
np.testing.assert_array_equal(z.np, [[0, 1, 2], [3, 4, 5]])
test_eq(magic.bpl('"a⍝b" ⍝ comment').py, 'a⍝b')
z┌→────┐
↓0 1 2│
│3 4 5│
└~────┘
Python values bound through bpl are visible to the magics. Integers from Python stay exact integers in BPL, and the result converts back to a Python int.
bpl(x=np.arange(1, 4))
value = %bpl +/x
test_eq(value.py, 6)
test_is(type(value.py), int)
value6ₓ₈
BPL’s display commands also work in %%bpl.
%%bpl
]Display [[1 2] "ab"]┌→───────────┐ │ ┌→──┐ "ab" │ │ │1 2│ │ │ └~──┘ │ └∊───────────┘
A %%bpl cell ending in ; still updates the workspace.
%%bpl
quiet←7;SVG elements and custom MIME renderers are displayed in %%bpl cells and when returned as Python values.
%%bpl
circle←•element "circle"
•svg circle ["cx":50 "cy":50 "r":30 "fill":"orange"]Names and help
Use ]help name for help and ]help name -source for source, in either magic. Leading comments in a dfn supply its help text. ]help inspects a function without calling it.
%%bpl
double←{⍝ Double the argument
⎕←"called" ◇ ω×2}with capture_output() as cap: magic.bpl(']help double')
assert 'Double the argument' in cap.outputs[-1].data['text/markdown']
with capture_output() as cap: magic.bpl('', ']help double -source')
assert '⎕←"called"' in cap.outputs[-1].data['text/markdown']
test_eq(len(cap.outputs), 2)Because tab completion reads the basedpl.bpl workspace, it includes names defined after the magics were registered. Tab does not complete BPL names inside quoted text or comments.
from IPython.core.completer import provisionalcompletershell = get_ipython()
for text, expected in [('%bpl dou', 'double'), ('v = %bpl dou', 'double'), ('%%bpl\n1+•sr', '•src'), ("%%bpl\n''' dou", 'double')]:
with provisionalcompleter(): matches = list(shell.Completer.completions(text, len(text)))
assert expected in [m.text for m in matches]
for text in ['%%bpl\n"dou', '%%bpl\n⍝ dou', 'ordinary_python']:
with provisionalcompleter(): matches = list(shell.Completer.completions(text, len(text)))
assert not any(m.type == 'BPL name' for m in matches)Errors and workspace state
An error reports its source location, and assignments completed before the error remain in the workspace. Incomplete input raises a syntax error and leaves the workspace unchanged.
with capture_output() as cap: test_fail(lambda: magic.bpl('', '⎕←42 ◇ 1÷"a"'), contains='DOMAIN ERROR')
assert '42' in cap.outputs[-1].data['text/html']
test_fail(lambda: magic.bpl('', '{ω+1'), contains='SYNTAX ERROR')
test_eq(magic.bpl('quiet').py, 9)Set bpl.timeout to the time limit in seconds for each evaluation. Press Ctrl-C to interrupt the evaluation. Output produced before an evaluation is cancelled is still displayed. The workspace keeps its names after cancellation. Native-library calls and individual BigInt operations can delay cancellation.
Installation
%load_ext basedpl.notebooks registers the magics in the current kernel. bpl-install-magic adds basedpl.notebooks to the default IPython profile, so that IPython loads it at startup. Registering the magics does not start a bpl --worker subprocess.
Symbol input
%bpl and %%bpl load BPL’s browser input method for typing glyphs. Using BPL notebooks explains the input method.