Commit 7a019ac3 authored by ROUBY Pierre-Antoine's avatar ROUBY Pierre-Antoine
Browse files

guix-kernel: Reply html at environment creation.

* guix-kernel/magic.scm (magic-html?): New procedure.
* guix-jupyter-container.scm (reply-html-to-kernel,
  kernel-info->html): New procedures.
* guix-jupyter-kernel.scm: Minor change.
* guix-kernel/html.scm: New file.
* guix-kernel/jupyter-client.scm (reply-html): New procedure.
* Makefile.am: Add 'guix-kernel/html.scm'.
* guix-kernel-demo.ipynb: Add example for new features.
parent 10531b61
......@@ -24,6 +24,7 @@ SOURCES = \
guix-kernel/hmac.scm \
guix-kernel/tools.scm \
guix-kernel/magic.scm \
guix-kernel/html.scm \
guix-kernel/jupyter-client.scm \
guix-kernel/jupyter-server.scm \
guix-jupyter-kernel.scm \
......
......@@ -24,6 +24,7 @@
(guix-kernel tools)
(guix-kernel hmac)
(guix-kernel magic)
(guix-kernel html)
(guix-kernel jupyter-client)
(guix-kernel jupyter-server))
......@@ -176,6 +177,30 @@
("payload" . [])
("user_expressions" . ,empty-object))))
counter))))
(define (reply-html-to-kernel socket uuid header-
parent-header metadata
content html count)
(reply-html socket socket ;Use same socket.
uuid header- parent-header
metadata content html
notebook-key count))
(define (kernel-info->html code)
(let* ((magic (get-magic-line code))
(list (string-split magic #\ ))
(name (list-ref list 2))
(env (list-cdr-ref list 3))
(title (string-append "<h3 " (green->html) ">"
"Environment '" name "' is ready !</h3>"))
(text (string-append "<p>With package"
(if (> (length env) 1)
"s" "")
":</p>"))
(enum (list->html env)))
(string-append title text enum)))
;;
;; Handler.
;;
......@@ -211,8 +236,13 @@
(let ((code (hash-ref content "code"))
(uuid (cadr parts)))
(cond
((or (magic-run? code)
(magic-env? code))
((magic-env? code)
(values kernels
(reply-html-to-kernel socket uuid header-
parent-header metadata content
(kernel-info->html code)
count)))
((magic-run? code)
(values kernels
(local-eval socket uuid header- parent-header
metadata content count)))
......@@ -226,6 +256,12 @@
(new-kernels (register-kernel kernels name new-kernel)))
(values new-kernels
(proxy-exec-kernel new-kernels name parts count))))))
((magic-html? code)
(values kernels
(reply-html-to-kernel socket uuid header-
parent-header metadata content
(delete-magic code)
count)))
(else
(values kernels
(local-eval socket uuid header- parent-header
......
......@@ -219,7 +219,6 @@
(let* ((list (string-split magic #\ ))
(env-name (list-ref list 2))
(env (list-cdr-ref list 3)))
;; Create new environment if not exist
(if (container-by-name containers env-name)
(values containers
(proxy-exec-container containers env-name
......@@ -228,8 +227,9 @@
(container (new-container-connect env-name id))
(new (register-container containers env-name
container)))
(values new (proxy-exec-container new env-name
parts count))))))
(values new
(proxy-exec-container new env-name
parts count))))))
((or (magic-run? code)
(magic-kernel? code))
(let* ((list (string-split magic #\ ))
......
......@@ -23,8 +23,8 @@
"```\n",
"\n",
"Differente command are available:\n",
" - **environment** for multi container\n",
" - **run** eval code in specific container\n",
" - **environment** to create new environment with container\n",
" - **run** eval code in guix kernel container\n",
" - **kernel** eval code in specific kernel\n",
"\n",
"---"
......@@ -86,8 +86,8 @@
"outputs": [
{
"data": {
"text/plain": [
"#<unspecified>"
"text/html": [
"<h3 style=color:green;>Environment 'env-guile' is ready !</h3><p>With packages:</p><ul><li>guile@2.2.2</li><li>guile-commonmark</li></ul>"
]
},
"execution_count": 1,
......@@ -96,7 +96,7 @@
}
],
"source": [
";;guix environment env-guile guile@2.2.2"
";;guix environment env-guile guile@2.2.2 guile-commonmark"
]
},
{
......@@ -135,7 +135,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Specifie default container:"
"Specify default container:"
]
},
{
......@@ -222,15 +222,15 @@
"SHELL=/gnu/store/y9ikcs0lcsip95p1914jv355034yjc6i-bash-4.4.12/bin/bash\n",
"PS1='Jupyter Guix Kernel [env-guile] -> '\n",
"JUPYTER_ENV=env-guile\n",
"PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/bin\n",
"CPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/include\n",
"INFOPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/info\n",
"MANPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/man\n",
"LIBRARY_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/lib\n",
"ALOCAL_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/shaer/alocal\n",
"JUPYTER_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/jupyter\n",
"GUILE_LOAD_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/guile/site/2.2\n",
"GUILE_LOAD_COMPILED_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/lin/guile/2.2/site-ccache\n",
"PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/bin\n",
"CPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/include\n",
"INFOPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/info\n",
"MANPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/man\n",
"LIBRARY_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/lib\n",
"ALOCAL_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/alocal\n",
"JUPYTER_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/jupyter\n",
"GUILE_LOAD_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/guile/site/2.2\n",
"GUILE_LOAD_COMPILED_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/lin/guile/2.2/site-ccache\n",
"#t"
]
},
......@@ -262,8 +262,8 @@
"outputs": [
{
"data": {
"text/plain": [
"#<unspecified>"
"text/html": [
"<h3 style=color:green;>Environment 'with-kernel-container' is ready !</h3><p>With package:</p><ul><li>jupyter-guile-kernel</li></ul>"
]
},
"execution_count": 1,
......@@ -284,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 1,
"metadata": {
"collapsed": false
},
......@@ -295,7 +295,7 @@
"#<unspecified>"
]
},
"execution_count": 7,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
......@@ -322,7 +322,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 2,
"metadata": {
"collapsed": false
},
......@@ -330,10 +330,10 @@
{
"data": {
"text/plain": [
"..........................................................+.++.......................+..............+..+.+.......+........+..................................................................++...........+..........++...........+.....................+...+...+.....................+.............+....+.........+..............+..................+.+........+.+......................+.................+...........+.+....................+.+..........+....+....+...................++...+.........+....+...+...........................+..+.++.+....+..............+..+......................+..........+....+....................+..+..........+...........+...................+........+.......+........................+...............+...+...............+.........++.....+........+..+..++................+...........+...........+..........+...+............................+...................+....+..+...+...................+.........+..+...++.++.........+................+..+...................................+.....+..................+....+++...............+....+..+...+.....+...............+...+............+..........+..+.+..............+...............+.........................++............+.+..................+.+..+............++.......................................+.++.............................+..............++..........+..............+......+........................+.....+....+........+.+......+...+..++.....+........+.+.......+..........+............+..............+.+......+....................+.............+..................++........+...+.+.............++....+......+..+..............+....+..............+..................................+...........+............................+....+......+....+.....................+.....+......+....+......+..+...............+............++......+.....+.......++....++........++.......+......+..............+........+..........+......+........+.+.....+.....+.............+...+...........+..............+.......+...+....+...............+..+.........................+...+.+...+......................+............................+.....+.+..........+.........................+.......+..+...........+.+.....................................+++..............++.....+......+....+...........++..............+............+..+...+......+....+.......................+......+.............................+.....++....+.....+.....................+.+............+...........+...+.....+....+...........+.......................+....+...+................++......+..................+...+.....+.........++..............+......+..........+...........+.....+.........................+..................+.......................................++...+.....................+.....+..+................................+.....+.......+......++.........................+................+..................+..+.............+........+...............................+..+......+.......+.......................+........................+.....+.......+...................+..+.+........+.+...#<unspecified>"
"..+...+...............+.............+....+............++.....+...................+..........+.........................++..........................+...............+.......+....................+............................+......+.....+..........+..+..++...........................++.......+.+..+..........+..+......+...........+.+.....+.....................+.............+...+.+......................++.......................+......+............+...+......++...+....+++..+.........................+++...................................+.+.........................+.......+......................++.............+..+.........+.......+..+......+..+...+........................+..........+...............................+......+.........+...............+..+....+....................+........+...........................+...............+..................++...............................+...........+...................+..............++......+......+....+.+........................+.................+.+....+........+.........+...+.......................+........................+.........................................+...+..........+....+..++......+.....................+..........+.................+.+..................+...........+....+....+......................+.+.............................+...........................+.+.........+..............++....+......................+..........+...+.........+...+......+..................................+.+...+.....+................+................+.....+.............+....+..............................................+............+.....+...+...........+....................+........................+......+......+..............++....+.............................+........+......+........++..+...++.......+..+....+......++....+...........+.......+........+....+......+...............+...............++...+..................................+.................+..............++......................+.+...+.....+.+.......+....................+............+..........+..........................+........+...+..............+...+.....++..+.........................+......+...+......+...+..................+.+.............++...+..+.....+.....++.............+.....+....+++..............+.........................+...........+....+........+.+..........+................++++.++..+.......+........+...............+...............++..................+................................+....+.........+.+......++.+....+...............++.+............................++.........+...........................++....................+.+.....................................+........+....+.+....+.......+...........+.....+.....+........+........+..................+.............+..+.....+.+....+..........+......+...........+...+.......+...+...++.+.+...........+................................+.............+............................................+...........+..+.+........++.............+.+....+........................+..............+.....+........................#<unspecified>"
]
},
"execution_count": 8,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
......
%% Cell type:markdown id: tags:
# Demo Guix Jupyter kernel
Project source code: [Guix-kernel](https://gitlab.inria.fr/guix-hpc/guix-kernel).
![Guix-Jupyter](logo.png)
---
## Features
### Magic command
This kernel use magic cells for kernel command.
```scheme
;;guix <command> <args>
<code>
```
Differente command are available:
- **environment** for multi container
- **run** eval code in specific container
- **environment** to create new environment with container
- **run** eval code in guix kernel container
- **kernel** eval code in specific kernel
---
%% Cell type:markdown id: tags:
## Usage
### Environment: Multi environment
#### Default environment
%% Cell type:code id: tags:
``` scheme
(version)
```
%%%% Output: execute_result
"2.2.3"
%% Cell type:markdown id: tags:
#### Create new environment
Use *magic* cells to specify package version, just like this:
```scheme
;;guix environment <name> <packages...>
<code>
```
%% Cell type:code id: tags:
``` scheme
;;guix environment env-guile guile@2.2.2
;;guix environment env-guile guile@2.2.2 guile-commonmark
```
%%%% Output: execute_result
#<unspecified>
%% Cell type:markdown id: tags:
### Run: Run code in existing environment
Check current guile-version:
%% Cell type:code id: tags:
``` scheme
;;guix run env-guile
(version)
```
%%%% Output: execute_result
"2.2.2"
%% Cell type:markdown id: tags:
Specifie default container:
Specify default container:
%% Cell type:code id: tags:
``` scheme
;;guix run default
(version)
```
%%%% Output: execute_result
"2.2.3"
%% Cell type:markdown id: tags:
Define function:
%% Cell type:code id: tags:
``` scheme
;;guix run env-guile
(define (list-environ env)
(cond
((null? env) #t)
(else
(display (car env)) (newline)
(list-environ (cdr env)))))
```
%%%% Output: execute_result
#<unspecified>
%% Cell type:markdown id: tags:
Use function:
%% Cell type:code id: tags:
``` scheme
;;guix run env-guile
(list-environ (environ))
```
%%%% Output: execute_result
HOME=/home/prouby
USER=prouby
LOGNAME=prouby
TERM=xterm-256color
PWD=/home/prouby/src/jupyter/guix/guix-kernel
PAGER=
SHELL=/gnu/store/y9ikcs0lcsip95p1914jv355034yjc6i-bash-4.4.12/bin/bash
PS1='Jupyter Guix Kernel [env-guile] -> '
JUPYTER_ENV=env-guile
PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/bin
CPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/include
INFOPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/info
MANPATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/man
LIBRARY_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/lib
ALOCAL_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/shaer/alocal
JUPYTER_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/jupyter
GUILE_LOAD_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/share/guile/site/2.2
GUILE_LOAD_COMPILED_PATH=/gnu/store/9xdiyjk77gykbdnqibfq1wzm1yax081r-profile/lin/guile/2.2/site-ccache
PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/bin
CPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/include
INFOPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/info
MANPATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/man
LIBRARY_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/lib
ALOCAL_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/alocal
JUPYTER_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/jupyter
GUILE_LOAD_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/share/guile/site/2.2
GUILE_LOAD_COMPILED_PATH=/gnu/store/nwpr2r4hlhbl6wyx6bzrp9yc20qnjny9-profile/lin/guile/2.2/site-ccache
#t
%% Cell type:markdown id: tags:
### Kernel: Eval code in other jupyter kernel
Create environment 'guile-kernel' with guile kernel for jupyter:
%% Cell type:code id: tags:
``` scheme
;;guix environment with-kernel-container jupyter-guile-kernel
```
%%%% Output: execute_result
#<unspecified>
%% Cell type:markdown id: tags:
Run code in guile kernel:
%% Cell type:code id: tags:
``` scheme
;;guix kernel with-kernel-container guile
(define (random-art num)
(define (new-char)
(if (eq? (random 10) 5)
(display "+")
(display "."))
(random-art (- num 1)))
(if (not (zero? num))
(new-char)))
```
%%%% Output: execute_result
#<unspecified>
%% Cell type:markdown id: tags:
Use previous function in guile kernel:
%% Cell type:code id: tags:
``` scheme
;;guix kernel with-kernel-container guile
(random-art 3000)
```
%%%% Output: execute_result
..........................................................+.++.......................+..............+..+.+.......+........+..................................................................++...........+..........++...........+.....................+...+...+.....................+.............+....+.........+..............+..................+.+........+.+......................+.................+...........+.+....................+.+..........+....+....+...................++...+.........+....+...+...........................+..+.++.+....+..............+..+......................+..........+....+....................+..+..........+...........+...................+........+.......+........................+...............+...+...............+.........++.....+........+..+..++................+...........+...........+..........+...+............................+...................+....+..+...+...................+.........+..+...++.++.........+................+..+...................................+.....+..................+....+++...............+....+..+...+.....+...............+...+............+..........+..+.+..............+...............+.........................++............+.+..................+.+..+............++.......................................+.++.............................+..............++..........+..............+......+........................+.....+....+........+.+......+...+..++.....+........+.+.......+..........+............+..............+.+......+....................+.............+..................++........+...+.+.............++....+......+..+..............+....+..............+..................................+...........+............................+....+......+....+.....................+.....+......+....+......+..+...............+............++......+.....+.......++....++........++.......+......+..............+........+..........+......+........+.+.....+.....+.............+...+...........+..............+.......+...+....+...............+..+.........................+...+.+...+......................+............................+.....+.+..........+.........................+.......+..+...........+.+.....................................+++..............++.....+......+....+...........++..............+............+..+...+......+....+.......................+......+.............................+.....++....+.....+.....................+.+............+...........+...+.....+....+...........+.......................+....+...+................++......+..................+...+.....+.........++..............+......+..........+...........+.....+.........................+..................+.......................................++...+.....................+.....+..+................................+.....+.......+......++.........................+................+..................+..+.............+........+...............................+..+......+.......+.......................+........................+.....+.......+...................+..+.+........+.+...#<unspecified>
..+...+...............+.............+....+............++.....+...................+..........+.........................++..........................+...............+.......+....................+............................+......+.....+..........+..+..++...........................++.......+.+..+..........+..+......+...........+.+.....+.....................+.............+...+.+......................++.......................+......+............+...+......++...+....+++..+.........................+++...................................+.+.........................+.......+......................++.............+..+.........+.......+..+......+..+...+........................+..........+...............................+......+.........+...............+..+....+....................+........+...........................+...............+..................++...............................+...........+...................+..............++......+......+....+.+........................+.................+.+....+........+.........+...+.......................+........................+.........................................+...+..........+....+..++......+.....................+..........+.................+.+..................+...........+....+....+......................+.+.............................+...........................+.+.........+..............++....+......................+..........+...+.........+...+......+..................................+.+...+.....+................+................+.....+.............+....+..............................................+............+.....+...+...........+....................+........................+......+......+..............++....+.............................+........+......+........++..+...++.......+..+....+......++....+...........+.......+........+....+......+...............+...............++...+..................................+.................+..............++......................+.+...+.....+.+.......+....................+............+..........+..........................+........+...+..............+...+.....++..+.........................+......+...+......+...+..................+.+.............++...+..+.....+.....++.............+.....+....+++..............+.........................+...........+....+........+.+..........+................++++.++..+.......+........+...............+...............++..................+................................+....+.........+.+......++.+....+...............++.+............................++.........+...........................++....................+.+.....................................+........+....+.+....+.......+...........+.....+.....+........+........+..................+.............+..+.....+.+....+..........+......+...........+...+.......+...+...++.+.+...........+................................+.............+............................................+...........+..+.+........++.............+.+....+........................+..............+.....+........................#<unspecified>
%% Cell type:markdown id: tags:
---
`guix-kernel-demo.ipynb` for guix-kernel.
_Version 0.0.1_
......
;;; Guix-kernel -- Guix kernel for Jupyter
;;; Copyright (C) 2018 Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (guix-kernel html)
#:export (list->html
inline-color
red->html
grey->html
blue->html
green->html))
;;
;; Text color.
;;
(define (inline-color color)
(string-append "style=color:" color ";"))
(define (red->html)
(inline-color "red"))
(define (grey->html)
(inline-color "grey"))
(define (blue->html)
(inline-color "blue"))
(define (green->html)
(inline-color "green"))
;;
;; Tools.
;;
(define (list->html list)
"(a b c d) -> <ul><li>a</li><li>b</li>...</ul>"
(define (loop lst)
(cond
((null? lst) "")
(else
(string-append "<li>" (car lst) "</li>"
(loop (cdr lst))))))
(string-append "<ul>" (loop list) "</ul>"))
......@@ -24,7 +24,9 @@
send-to-jupyter
pub-busy
pub-idle))
pub-idle
reply-html))
;;
;; Kernel information.
......@@ -76,3 +78,36 @@
(define (pub-idle socket header- parent-header key)
(pub socket header- "idle" parent-header key))
;;
;; Reply.
;;
(define (reply-html socket-shell socket-iopub
uuid header- parent-header
metadata content html
notebook-key count)
(let ((code (hash-ref content "code"))
(empty-object (make-hash-table 1))
(counter (+ count 1)) ;Execution counter
(send- (lambda (socket msg-type content)
(send-to-jupyter socket uuid
(header- msg-type)
parent-header metadata
(scm->json-string content)
notebook-key))))
(send- socket-iopub
"execute_input" `(("code" . ,code)
("execution_count" . ,counter)))
(send- socket-iopub
"execute_result" `(("data" . (("text/html" . ,html)))
("metadata" . ,empty-object)
("execution_count" . ,counter)))
(send- socket-shell
"execute_reply" `(("status" . "ok")
("execution_count" . ,counter)
("payload" . [])
("user_expressions" . ,empty-object)))
counter))
......@@ -24,6 +24,7 @@
magic-run?
magic-exec?
magic-kernel?
magic-html?
magic-get-kernel-name))
......@@ -65,6 +66,9 @@ return false"
(define (magic-kernel? code) ;;;guix kernel ...
(magic-attr? code "kernel"))
(define (magic-html? code) ;;;guix html ...
(magic-attr? code "html"))
;;
;; Kernel magic command.
;;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment