1. common-lisp-jupyter
  2. examples
Notebook

common-lisp-jupyter

A Common Lisp kernel for Jupyter.

All stream output is captured and displayed in the notebook interface.

In [1]:
(formatt"Hello, World")
(format*error-output*"Goodbye, cruel World.")
Out[1]:
NIL
Out[1]:
NIL
Hello, World
Goodbye, cruel World.
In [2]:
(formatj:*markdown-output*"# Title
This is *markdown*!")
Out[2]:
NIL

Title

This is markdown!

Evaluation results are displayed directory in the notebook.

In [3]:
(+2345)
Out[3]:
14

All Lisp code is value, including calls to quicklisp.

In [4]:
(ql:quickload:shasht)
To load "shasht":
 Load 1 ASDF system:
 shasht
Out[4]:
(:SHASHT)
; Loading "shasht"

The serialized JSON will represented as a Lisp string.

In [5]:
(shasht:write-json`(:object-plist"foo""bar""quux"1.23)t)
Out[5]:
(:OBJECT-PLIST "foo" "bar" "quux" 1.23)
{
 "foo": "bar",
 "quux": 1.23
}

JSON can also be displayed with open/close expanders using json or json-file

In [6]:
(jupyter:json`(:object-plist"foo""bar""quux"(:object-plist"a"1"b"2)):expandedt:displayt:id"a")

If you use display_data and assign an id then you can update the result later on.

In [7]:
(jupyter:json`(:object-plist"foo""bar""quux"(:object-plist"a"1"b"2"c"3)):expandedt:displayt:updatet:id"a")

Error conditions will be captured and a backtrace will be sent to *error-output*

In [8]:
(/10)
3: ((FLET "H1" :IN COMMON-LISP-JUPYTER::MY-EVAL) arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ 1 0).)
4: (SB-KERNEL::%SIGNAL arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ 1 0).)
5: (ERROR DIVISION-BY-ZERO OPERATION / OPERANDS (1 0))
6: ("DIVISION-BY-ZERO-ERROR" 1 0)
7: (SB-KERNEL:INTERNAL-ERROR #.(SB-SYS:INT-SAP #X7F7B8362DC80) #<unused argument>)
8: ("foreign function: call_into_lisp")
9: ("foreign function: funcall2")
10: ("foreign function: interrupt_internal_error")
11: ("foreign function: #x558902F62993")
12: (SB-KERNEL::INTEGER-/-INTEGER 1 0)
13: (/ 1 0)
14: (SB-INT:SIMPLE-EVAL-IN-LEXENV (/ 1 0) #<NULL-LEXENV>)
15: (EVAL (/ 1 0))
16: (COMMON-LISP-JUPYTER::MY-EVAL (/ 1 0))
17: ((:METHOD JUPYTER:EVALUATE-CODE (COMMON-LISP-JUPYTER:KERNEL T)) #<unused argument> (/ 1 0))
18: (JUPYTER::HANDLE-EXECUTE-REQUEST)
19: (JUPYTER::RUN-SHELL #<KERNEL {1003A22F83}>)
20: ((LAMBDA NIL :IN BORDEAUX-THREADS::BINDING-DEFAULT-SPECIALS))
21: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
22: ((FLET "WITHOUT-INTERRUPTS-BODY-11" :IN SB-THREAD::RUN))
23: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
24: ((FLET "WITHOUT-INTERRUPTS-BODY-4" :IN SB-THREAD::RUN))
25: (SB-THREAD::RUN)
26: ("foreign function: call_into_lisp")
27: ("foreign function: funcall1")
#<ENVIRONMENT {1003A05853}>
 [Environment of thread #<THREAD "SHELL Thread" RUNNING {10051C8C13}>]
arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ 1 0).
 [Condition of type DIVISION-BY-ZERO]
Backtrace:
 3: ((FLET "H1" :IN COMMON-LISP-JUPYTER::MY-EVAL) arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ 1 0).)
 4: (SB-KERNEL::%SIGNAL arithmetic error DIVISION-BY-ZERO signalled
Operation was (/ 1 0).)
 5: (ERROR DIVISION-BY-ZERO OPERATION / OPERANDS (1 0))
 6: ("DIVISION-BY-ZERO-ERROR" 1 0)
 7: (SB-KERNEL:INTERNAL-ERROR #.(SB-SYS:INT-SAP #X7F7B8362DC80) #<unused argument>)
 8: ("foreign function: call_into_lisp")
 9: ("foreign function: funcall2")
 10: ("foreign function: interrupt_internal_error")
 11: ("foreign function: #x558902F62993")
 12: (SB-KERNEL::INTEGER-/-INTEGER 1 0)
 13: (/ 1 0)
 14: (SB-INT:SIMPLE-EVAL-IN-LEXENV (/ 1 0) #<NULL-LEXENV>)
 15: (EVAL (/ 1 0))
 16: (COMMON-LISP-JUPYTER::MY-EVAL (/ 1 0))
 17: ((:METHOD JUPYTER:EVALUATE-CODE (COMMON-LISP-JUPYTER:KERNEL T)) #<unused argument> (/ 1 0))
 18: (JUPYTER::HANDLE-EXECUTE-REQUEST)
 19: (JUPYTER::RUN-SHELL #<KERNEL {1003A22F83}>)
 20: ((LAMBDA NIL :IN BORDEAUX-THREADS::BINDING-DEFAULT-SPECIALS))
 21: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
 22: ((FLET "WITHOUT-INTERRUPTS-BODY-11" :IN SB-THREAD::RUN))
 23: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
 24: ((FLET "WITHOUT-INTERRUPTS-BODY-4" :IN SB-THREAD::RUN))
 25: (SB-THREAD::RUN)
 26: ("foreign function: call_into_lisp")
 27: ("foreign function: funcall1")

New functions can be defined. The default namespace is COMMON-LISP-USER.

In [8]:
(defunfibonacci(n)
(if(<=n1)
1
(+(fibonacci(-n2))(fibonacci(-n1)))))
Out[8]:
FIBONACCI

The seventh element of everybody's favorite sequence.

In [9]:
(fibonacci7)
Out[9]:
21

S-Expressions will be displayed using pprint.

In [10]:
(function-lambda-expression#'fibonacci)
Out[10]:
(LAMBDA (N)
 (BLOCK FIBONACCI
 (IF (<= N 1)
 1
 (+ (FIBONACCI (- N 2)) (FIBONACCI (- N 1))))))
Out[10]:
T
Out[10]:
FIBONACCI

Rich text and images can be displayed using inline values using the inline-result, html, jpeg, latex, markdown, png, svg or text functions.

In [11]:
(jupyter:markdown"## wibble
foo `quux`")
Out[11]:

wibble

foo quux

In [12]:
(jupyter:latex"$$R_{\\mu \\nu} - \\tfrac{1}{2}R \,円 g_{\\mu \\nu} + \\Lambda g_{\\mu \\nu} =
 8 \\pi G c^{-4} T_{\\mu \\nu}$$")
Out[12]:
$$R_{\mu \nu} - \tfrac{1}{2}R ,円 g_{\mu \nu} + \Lambda g_{\mu \nu} = 8 \pi G c^{-4} T_{\mu \nu}$$

External files can be rendered using the file, gif-file, jpeg-file, png-file, ps-file, svg-file functions.

The MIME type will be automatically determined in the case of a call to file.

In [13]:
(jupyter:file"lisplogo_alien.svg":displayt)
<sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.64776242" inkscape:cx="102.78147" inkscape:cy="-89.134833" inkscape:document-units="px" inkscape:current-layer="layer1" inkscape:window-width="1841" inkscape:window-height="1025" inkscape:window-x="79" inkscape:window-y="27" showgrid="false" fit-margin-top="25" fit-margin-left="25" fit-margin-right="25" fit-margin-bottom="25" inkscape:window-maximized="1" /> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF>

Calls to yes-or-no-p will result in a input_request to the user.

In [14]:
(defparameterlisp-rocks(yes-or-no-p"LISP rocks?"))
Out[14]:
LISP-ROCKS
In [15]:
(jupyter:markdown(formatnil"For the record Lisp ~A"(iflisp-rocks"**rocks**!""**does not** rock.")))
Out[15]:

For the record Lisp rocks!

Output send to *query-io* will result also result in an input_request to the user.

In [16]:
(defunask(prompt)
(format*query-io*prompt)
(finish-output*query-io*)
(read-line*query-io*))
Out[16]:
ASK
In [17]:
(defvarquest(ask"What is your quest? "))
Out[17]:
QUEST
In [18]:
(formatt"Your quest is: ~A"quest)
Out[18]:
NIL
Your quest is: wibble

jupyter:clear will clear the output of the current.

In [19]:
(loop
forifrom1to10
do(sleep0.25)
do(jupyter:cleart)
do(printi)
do(finish-output*standard-output*)
finally(return(values)))
10 

(values) can be used to suppress the output. Defining a reader macro can make this easier.

In [20]:
(defunno-output-reader(streamchar)
(declare(ignorechar))
(list(quoteprogn)(readstreamtnilt)(values)))
(set-macro-character#\~#'no-output-reader)
Out[20]:
NO-OUTPUT-READER
Out[20]:
T
In [21]:
~(formatt"No output returned!")
Out[21]:
NIL
No output returned!

Multiple value returns function correctly and previous result/form are set.

In [22]:
(values'a1'a2)
'b
(values'c1'c2'c3)
(list//////)
Out[22]:
A1
Out[22]:
A2
Out[22]:
B
Out[22]:
C1
Out[22]:
C2
Out[22]:
C3
Out[22]:
((C1 C2 C3) (B) (A1 A2))
In [23]:
(values'a1'a2)
'b
(values'c1'c2'c3)
(list******)
Out[23]:
A1
Out[23]:
A2
Out[23]:
B
Out[23]:
C1
Out[23]:
C2
Out[23]:
C3
Out[23]:
(C1 B A1)
In [24]:
(+01)
(-42)
(/93)
(list++++++)
Out[24]:
1
Out[24]:
2
Out[24]:
3
Out[24]:
((/ 9 3) (- 4 2) (+ 0 1))
In [ ]:
In [ ]:
In [ ]:
In [ ]:

AltStyle によって変換されたページ (->オリジナル) /