Commit d981e1a5 authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

Add ";;guix pin" magic.

* guix-jupyter-kernel.scm (channels->shtml, reply-for-channels)
(reply-for-channel-failure): New procedures.
(reply-execute-request): Handle ";;guix pin".
(reply-complete-request): Complete ";;guix pin".
* guix-kernel-demo.ipynb: Add ";;guix pin" example.
parent aa57c901
......@@ -18,6 +18,7 @@
(use-modules (json)
(simple-zmq)
(git) ;for 'git-error-message'
(srfi srfi-1)
(srfi srfi-13)
(srfi srfi-19)
......@@ -33,6 +34,7 @@
(guix build utils)
(guix gexp)
(guix store)
(guix channels)
(guix derivations)
(guix inferior)
(guix monads)
......@@ -164,6 +166,34 @@ NAME with MANIFEST."
("execution_count" . ,count))))
#:kernel-socket kernel-iopub))
(define (channels->shtml channels)
"Return SHTML representing CHANNELS."
`(div
(bold "Switched to these Guix channels:")
(p (table
,@(map (lambda (channel)
`(tr (tc (a (@ (href ,(channel-url channel)))
(code ,(channel-name channel))))
(tc (code ,(or (channel-commit channel)
(channel-branch channel))))))
channels)))))
(define* (reply-for-channels kernel message channels
#:key (count 0))
"Reply to MESSAGE, which comes from KERNEL, that we successfully switch to
CHANNELS."
(reply-html kernel message
(sxml->html-string (channels->shtml channels))
count))
(define* (reply-for-channel-failure kernel message channels error
#:key (count 0))
"Send KERNEL a reply saying we failed to switch to CHANNELS."
(reply-html kernel message
(string-append "Failed to switch to channels: "
(git-error-message error) ".")
count))
(define* (create-environment name specs state
#:key kernel message)
"Spawn a new execution environment called NAME and containing SPECS, a list
......@@ -286,6 +316,29 @@ stripped."
(send-message proxy (execute-request-sans-magic message))
(let ((state (increment-execution-count state)))
(set-proxy-state-default-environment state name)))))
((";;guix" "pin" commit)
(let ((count (proxy-state-execution-count state))
(channels (map (lambda (ch)
(if (guix-channel? ch)
(channel (inherit ch)
(commit commit))
ch))
%default-channels)))
(format/log "pinning to these channels: ~s~%" channels)
;; TODO: Send a message showing that something's happening.
(catch 'git-error
(lambda ()
(let ((inferior (inferior-for-channels channels)))
;; TODO: Obtain the <channel-instance> records so the message
;; can provide more info.
(reply-for-channels kernel message channels
#:count count)
(set-proxy-state-inferior (increment-execution-count state)
inferior)))
(lambda (key error . rest)
(reply-for-channel-failure kernel message channels error
#:count count)
state))))
((";;guix" _ ...)
(reply-html kernel message
(sxml->html-string
......@@ -349,7 +402,7 @@ Return STATE."
;; This is a completion request on a ";;guix" magic.
(match (string-split (string-take first cursor) #\space)
((";;guix" command)
(send-completion-reply '("environment")
(send-completion-reply '("environment" "pin")
(- cursor (string-length command))
cursor)
state)
......
%% 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)
---
## Getting Started
The `;;guix environment` _magic command_ allows you to create an execution environment containing a Jupyter kernel, or to run code in a previously-created environment.
---
%% Cell type:markdown id: tags:
### Evaluating Code in a Jupyter Kernel
Create an environment called `guile-kernel` containing the Guile kernel for Jupyter:
%% Cell type:code id: tags:
``` scheme
;;guix pin 36cc160e721a764c16f53c6f7fbd9d09c581717e
```
%% Cell type:code id: tags:
``` scheme
;;guix environment guile-kernel-env <- jupyter-guile-kernel
```
%% Cell type:markdown id: tags:
Run code in guile kernel:
%% Cell type:code id: tags:
``` scheme
;;guix environment guile-kernel-env
(define (list-environ env)
(for-each (lambda (variable)
(display variable)
(newline))
(environ)))
```
%% Cell type:code id: tags:
``` scheme
;; Here we don't repeat "guix environment", so we use the
;; same environment as the previous cell.
(list-environ (environ))
```
%% Cell type:code id: tags:
``` scheme
(define (random-art num)
(define (new-char)
(if (eq? (random 10) 5)
(display "+")
(display "."))
(random-art (- num 1)))
(if (not (zero? num))
(new-char)))
```
%% Cell type:markdown id: tags:
Call the previously-defined function in the Guile kernel:
%% Cell type:code id: tags:
``` scheme
(random-art 3000)
```
%% Cell type:markdown id: tags:
---
## Using IPython
### Create an environment with IPython
%% Cell type:code id: tags:
``` scheme
;;guix environment my-ipython <- python-ipython python-ipykernel
```
%% Cell type:markdown id: tags:
---
### Run code in the IPython kernel
%% Cell type:code id: tags:
``` scheme
;;guix environment my-ipython
# This is python lang !
def hello ():
print ("Hello Jupyter !")
hello ()
```
%% Cell type:code id: tags:
``` scheme
;;guix environment my-ipython
import os
os.getcwd()
```
%% Cell type:code id: tags:
``` scheme
os.getuid()
```
%% Cell type:code id: tags:
``` scheme
os.getpid()
```
%% Cell type:code id: tags:
``` scheme
os.listdir('/')
```
%% Cell type:markdown id: tags:
## A Matplotlib environment
%% Cell type:code id: tags:
``` scheme
;;guix environment matplotlib-env <- python-ipython python-ipykernel python-ipywidgets python-matplotlib
```
%% Cell type:code id: tags:
``` scheme
;;guix environment matplotlib-env
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import style
import random
x = random.sample(range(1, 5000), 1000)
num_bins = 100
n, bins, patches = plt.hist(x, num_bins, facecolor='green', alpha=0.5)
plt.title('Histogram Example')
plt.xlabel('Values')
plt.xlabel('Counts')
plt.show()
```
%% Cell type:code id: tags:
``` scheme
;;guix environment R <- r r-irkernel
```
%% Cell type:code id: tags:
``` scheme
;;guix environment R
version
```
%% Cell type:markdown id: tags:
### Using the built-in kernel for [GNU Guile](https://gnu.org/s/guile)
%% Cell type:code id: tags:
``` scheme
;;guix environment guile <- guile
```
%% Cell type:code id: tags:
``` scheme
(version)
```
%% Cell type:markdown id: tags:
---
`guix-kernel-demo.ipynb` for guix-kernel.
_Version 0.0.1_
......
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