Skip to content

Commit

Permalink
Merge pull request #144 from janpfeifer/capture
Browse files Browse the repository at this point in the history
Adding `%capture [-a] <file>`, feature request #142
  • Loading branch information
janpfeifer authored Oct 16, 2024
2 parents bed2f86 + ccbb91f commit 0fc7fbc
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ FROM ${BASE_IMAGE}:${BASE_TAG}
USER root
RUN apt update --yes
RUN apt install --yes --no-install-recommends \
sudo wget git openssh-client rsync curl
sudo tzdata wget git openssh-client rsync curl

# Give NB_USER sudo power for "/usr/bin/apt-get install/update" or "/usr/bin/apt install/update".
USER root
Expand Down Expand Up @@ -61,7 +61,7 @@ RUN wget --quiet --output-document=- "https://go.dev/dl/go${GO_VERSION}.linux-am
&& go version

# Install GoNB (https://github.com/janpfeifer/gonb) in the user account
ARG GONB_VERSION="v0.10.5"
ARG GONB_VERSION="v0.10.6"
USER $NB_USER
WORKDIR ${HOME}
RUN export GOPROXY=direct && \
Expand Down
20 changes: 15 additions & 5 deletions cmd/nbexec/nbexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {
jupyterStop.Trigger()
if e != nil {
// Wait for jupyter notebook to get killed, before re-throwing the panic.
klog.Errorf("%v", e)
klog.Errorf("Panic: %+v", e)
jupyterDone.Wait()
panic(e)
}
Expand Down Expand Up @@ -271,14 +271,24 @@ func executeNotebook(url string, inputBoxes []string) {
// Use system's Google Chrome is available, for sandboxing:
var controlURL string
chromePath, err := exec.LookPath("google-chrome")
var l *launcher.Launcher
if err == nil {
controlURL = launcher.New().Bin(chromePath).MustLaunch()
klog.V(1).Infof("Using system's Google Chrome")
l = launcher.New().Bin(chromePath)
} else {
klog.Warningf("Using rod downloaded chromium, with --no-sandbox")
controlURL = launcher.New().NoSandbox(true).MustLaunch()
l = launcher.New().NoSandbox(true)
}
page := rod.New().ControlURL(controlURL).MustConnect().MustPage(url)
klog.V(1).Infof("Waiting for opening of page %q", url)
controlURL = l.
Set("disable-gpu", "true").
Set("disable-software-rasterizer", "true").
Logger(os.Stderr).
MustLaunch()
klog.V(1).Infof("Using controlURL=%q", controlURL)
browser := rod.New().ControlURL(controlURL).MustConnect()
klog.V(1).Info("Connected to browser.")
page := browser.MustPage(url)
klog.V(1).Infof("Connected to page in browser, waiting for opening of page %q", url)
page.MustWaitStable()

if *flagConsoleLog {
Expand Down
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# GoNB Changelog

## Next
## v0.10.6, 2024/10/16, Improved Docker, added `%capture`

* Feature request #138
* Added openssh-client, rsync and curl, to allow users to install other dependencies.
* Added sudo for apt install and apt update.
* Added support for `autostart.sh` that if present in the mounted container `/notebooks` directory, and if root owned
and set as executable.
* Updated Dockerfile to latest version to JupyterLab -- now the base docker is served `quay.io/jupyter/base-notebook`
* Added `%capture [-a] <file>` to capture the output of a cell (#142)
* Fixed `nbexec`: added `--disable-gpu` and `--disable-software-rasterizer` when executing "headless" chrome for tests.

## v0.10.5, Added SendAsDownload

Expand Down
24 changes: 13 additions & 11 deletions docs/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ github.com/janpfeifer/gonb/cache/cache.go CacheWith 91.7%
github.com/janpfeifer/gonb/cache/cache.go Cache 0.0%
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go main 74.3%
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go startJupyterNotebook 73.2%
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go executeNotebook 78.6%
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go executeNotebook 82.4%
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go checkForInputBoxes 95.7%
github.com/janpfeifer/gonb/common/common.go Panicf 0.0%
github.com/janpfeifer/gonb/common/common.go Pause 0.0%
Expand Down Expand Up @@ -68,12 +68,13 @@ github.com/janpfeifer/gonb/gonbui/gonbui.go pollReaderPipe 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go Sync 92.9%
github.com/janpfeifer/gonb/gonbui/gonbui.go UniqueId 100.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go UniqueID 100.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtml 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTML 100.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtml 100.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTML 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTMLF 0.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtmlf 0.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayMarkdown 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHtml 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHTML 0.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHTML 66.7%
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHtml 100.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateMarkdown 0.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayPng 0.0%
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayPNG 0.0%
Expand Down Expand Up @@ -198,12 +199,12 @@ github.com/janpfeifer/gonb/internal/goexec/errorpublish.go JupyterErrorSplit
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.ExecuteCell 100.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.serializeExecuteCell 100.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.executeCellImpl 68.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.PostExecuteCell 88.9%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.PostExecuteCell 85.7%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.BinaryPath 100.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.CodePath 100.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.RemoveCode 83.3%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.AlternativeDefinitionsPath 0.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Execute 77.8%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Execute 85.7%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Compile 70.0%
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.GoImports 70.6%
github.com/janpfeifer/gonb/internal/goexec/execcode.go newJupyterStackTraceMapperWriter 75.0%
Expand Down Expand Up @@ -321,10 +322,11 @@ github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.UseNamedPipes
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.ExecutionCount 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.InDir 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStderr 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStdout 0.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStdout 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithInputs 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithPassword 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStaticInput 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.CaptureDisplayDataOutput 100.0%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.Exec 68.8%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.done 92.3%
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.handleJupyterInput 91.3%
Expand All @@ -335,7 +337,7 @@ github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.createTmpF
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.openPipeReader 89.3%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.pollNamedPipeReader 73.1%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.reportCellError 0.0%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchDisplayData 78.6%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchDisplayData 80.0%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchInputRequest 83.3%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.openPipeWriter 86.7%
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.pollPipeWriterFifo 66.7%
Expand Down Expand Up @@ -409,11 +411,11 @@ github.com/janpfeifer/gonb/internal/specialcmd/definitions.go removeDefinitionI
github.com/janpfeifer/gonb/internal/specialcmd/definitions.go removeDefinitions 91.7%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go Parse 84.6%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go joinLine 87.5%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execSpecialConfig 55.3%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execSpecialConfig 58.2%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execShell 100.0%
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go splitCmd 97.0%
github.com/janpfeifer/gonb/internal/specialcmd/track.go execTrack 27.3%
github.com/janpfeifer/gonb/internal/specialcmd/track.go execUntrack 54.5%
github.com/janpfeifer/gonb/internal/specialcmd/track.go showTrackedList 91.7%
github.com/janpfeifer/gonb/internal/websocket/websocket.go Javascript 83.3%
total (statements) 64.4%
total (statements) 64.6%
185 changes: 185 additions & 0 deletions examples/tests/capture.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fe105ef6-5264-4cb4-b467-60d9b0a337fd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ping\n"
]
}
],
"source": [
"%capture ${TEST_DIR}/pingpong.txt\n",
"%%\n",
"fmt.Println(\"Ping\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ee9acf4f-2488-4f92-bfae-6998d8351175",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ping\n"
]
}
],
"source": [
"!cat ${TEST_DIR}/pingpong.txt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "004e9622-e3a4-4780-816a-e5d1c77055b4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pong\n"
]
}
],
"source": [
"%capture -a ${TEST_DIR}/pingpong.txt\n",
"%%\n",
"fmt.Println(\"Pong\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "aee500e4-5728-4224-9746-f61d58909851",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ping\n",
"Pong\n"
]
}
],
"source": [
"!cat ${TEST_DIR}/pingpong.txt"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c8214b58-5946-4db5-a329-9e2f2c2fc215",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Ping\n",
"Pong\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%capture ${TEST_DIR}/pingpong.txt\n",
"import \"github.com/janpfeifer/gonb/gonbui\"\n",
"%%\n",
"gonbui.DisplayHTML(\"Ping\\nPong\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f67d51c5-dbcb-4e96-aba5-380bc55dbf53",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ping\n",
"Pong\n"
]
}
],
"source": [
"!cat ${TEST_DIR}/pingpong.txt"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9b88a712-deae-4d48-80a1-c4795db31c22",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"# Ping\n",
"# Pong\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%capture ${TEST_DIR}/pingpong.txt\n",
"import \"github.com/janpfeifer/gonb/gonbui\"\n",
"%%\n",
"gonbui.DisplayMarkdown(\"# Ping\\n# Pong\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e884811a-e7c9-425b-aa0b-394b1628ccd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Ping\n",
"# Pong\n"
]
}
],
"source": [
"!cat ${TEST_DIR}/pingpong.txt"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Go (gonb)",
"language": "go",
"name": "gonb"
},
"language_info": {
"codemirror_mode": "",
"file_extension": ".go",
"mimetype": "",
"name": "go",
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "go1.23.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 0fc7fbc

Please sign in to comment.