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

guix-kernel: Add syntax for 'guix environment' magic command.

* guix-jupyter-container.scm (kernel-info->html): Add warning at
  envrionment creation.
  (reply-execute-request): Update list ref.
* guix-jupyter-kernel.scm: Update list ref.
* guix-kernel/magic.scm (magic-env?): Test separator.
* tests/magic.scm (magic-no-code): Update string.
* README.org (How to use it): Update 'environment'.
* guix-kernel-demo.ipynb: Update with new syntax.
parent 7a019ac3
......@@ -47,7 +47,7 @@ The =environment= command must be use to create new container with
specific software environment.
#+BEGIN_SRC scheme
;;guix environment <env-name> <packages ...>
;;guix environment <env-name> <- <packages ...>
#+END_SRC
/This command can take few minutes./
......@@ -56,7 +56,7 @@ ENV-NAME is the container id. PACKAGES is list of guix packages to put
in software environment.
#+BEGIN_SRC scheme
;;guix environment my-env guile@2.2.2 jupyter-guile-kernel guile-commonmark
;;guix environment my-env <- guile@2.2.2 jupyter-guile-kernel guile-commonmark
#+END_SRC
*** Run
......
......@@ -187,19 +187,29 @@
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)))
(if (magic-env? code)
(let* ((magic (get-magic-line code))
(list (string-split magic #\ ))
(name (list-ref list 2))
(sep (list-ref list 3))
(env (list-cdr-ref list 4))
(title (string-append "<h3 " (green->html) ">"
"Environment '" name "' is ready !</h3>"))
(err (if (not (equal? sep %magic-separator))
(string-append "<strong><p " (red->html) ">"
"Invalid separator '" sep
"'</p></strong>")
""))
(text (string-append "<p>With package"
(if (> (length env) 1)
"s" "")
":</p>"))
(enum (list->html env)))
(string-append title err text enum))
(string-append "<h3" (red->html) ">"
"Invalid magic environment"
"</h3>")))
;;
;; Handler.
......
......@@ -218,7 +218,7 @@
((magic-env? code) ;Guix environment magic command
(let* ((list (string-split magic #\ ))
(env-name (list-ref list 2))
(env (list-cdr-ref list 3)))
(env (list-cdr-ref list 4)))
(if (container-by-name containers env-name)
(values containers
(proxy-exec-container containers env-name
......
......@@ -72,14 +72,14 @@
"Use *magic* cells to specify package version, just like this:\n",
"\n",
"```scheme\n",
";;guix environment <name> <packages...>\n",
";;guix environment <name> <- <packages...>\n",
"<code>\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {
"collapsed": false
},
......@@ -90,13 +90,13 @@
"<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,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
";;guix environment env-guile guile@2.2.2 guile-commonmark"
";;guix environment env-guile <- guile@2.2.2 guile-commonmark"
]
},
{
......@@ -272,7 +272,7 @@
}
],
"source": [
";;guix environment with-kernel-container jupyter-guile-kernel"
";;guix environment with-kernel-container <- jupyter-guile-kernel"
]
},
{
......
%% 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** 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...>
;;guix environment <name> <- <packages...>
<code>
```
%% Cell type:code id: tags:
``` scheme
;;guix environment env-guile guile@2.2.2 guile-commonmark
;;guix environment env-guile <- guile@2.2.2 guile-commonmark
```
%%%% Output: execute_result
%% 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:
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/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
;;guix environment with-kernel-container <- jupyter-guile-kernel
```
%%%% Output: execute_result
%% 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>
%% Cell type:markdown id: tags:
---
`guix-kernel-demo.ipynb` for guix-kernel.
_Version 0.0.1_
......
......@@ -15,7 +15,9 @@
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(define-module (guix-kernel magic)
#:export (magic?
#:export (%magic-separator
magic?
get-magic-line
delete-magic
......@@ -28,6 +30,8 @@
magic-get-kernel-name))
(define %magic-separator "<-")
(define (magic? code)
"Return true if code begin by magic command like ';;guix ...', else
return false"
......@@ -43,8 +47,7 @@ return false"
(define (delete-magic code)
"Delete the magic !"
(if (magic? code)
(string-join (cdr (string-split code #\newline))
"\n")
(string-join (cdr (string-split code #\newline)) "\n")
code))
(define (magic-attr? code attr)
......@@ -54,8 +57,16 @@ return false"
attr))
#f))
(define (magic-env? code) ;;;guix environement ...
(magic-attr? code "environment"))
(define (magic-env? code) ;;;guix environment ...
(if (magic-attr? code "environment")
(let* ((magic (get-magic-line code))
(list (string-split magic #\ )))
(unless (equal? (list-ref list 3) %magic-separator)
(display (string-append "WARNING: Separator '"
%magic-separator
"' not found !\n")))
#t)
#f))
(define (magic-run? code) ;;;guix run ...
(magic-attr? code "run"))
......
......@@ -18,7 +18,7 @@
#:use-module (guix-kernel magic)
#:use-module (srfi srfi-64))
(define magic-no-code ";;guix environment test-magic jupyter-guile-kernel guile@2.2.3")
(define magic-no-code ";;guix environment test-magic <- jupyter-guile-kernel guile@2.2.3")
(define no-magic-code "(define hello \"hello world\")")
(define no-magic ";; this is not magic command")
(define no-magic-2 ";; guix is not 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