Skip to content

Commit 0fc7fbc

Browse files
authored
Merge pull request #144 from janpfeifer/capture
Adding `%capture [-a] <file>`, feature request #142
2 parents bed2f86 + ccbb91f commit 0fc7fbc

File tree

17 files changed

+456
-91
lines changed

17 files changed

+456
-91
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ FROM ${BASE_IMAGE}:${BASE_TAG}
2424
USER root
2525
RUN apt update --yes
2626
RUN apt install --yes --no-install-recommends \
27-
sudo wget git openssh-client rsync curl
27+
sudo tzdata wget git openssh-client rsync curl
2828

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

6363
# Install GoNB (https://github.com/janpfeifer/gonb) in the user account
64-
ARG GONB_VERSION="v0.10.5"
64+
ARG GONB_VERSION="v0.10.6"
6565
USER $NB_USER
6666
WORKDIR ${HOME}
6767
RUN export GOPROXY=direct && \

cmd/nbexec/nbexec.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func main() {
103103
jupyterStop.Trigger()
104104
if e != nil {
105105
// Wait for jupyter notebook to get killed, before re-throwing the panic.
106-
klog.Errorf("%v", e)
106+
klog.Errorf("Panic: %+v", e)
107107
jupyterDone.Wait()
108108
panic(e)
109109
}
@@ -271,14 +271,24 @@ func executeNotebook(url string, inputBoxes []string) {
271271
// Use system's Google Chrome is available, for sandboxing:
272272
var controlURL string
273273
chromePath, err := exec.LookPath("google-chrome")
274+
var l *launcher.Launcher
274275
if err == nil {
275-
controlURL = launcher.New().Bin(chromePath).MustLaunch()
276+
klog.V(1).Infof("Using system's Google Chrome")
277+
l = launcher.New().Bin(chromePath)
276278
} else {
277279
klog.Warningf("Using rod downloaded chromium, with --no-sandbox")
278-
controlURL = launcher.New().NoSandbox(true).MustLaunch()
280+
l = launcher.New().NoSandbox(true)
279281
}
280-
page := rod.New().ControlURL(controlURL).MustConnect().MustPage(url)
281-
klog.V(1).Infof("Waiting for opening of page %q", url)
282+
controlURL = l.
283+
Set("disable-gpu", "true").
284+
Set("disable-software-rasterizer", "true").
285+
Logger(os.Stderr).
286+
MustLaunch()
287+
klog.V(1).Infof("Using controlURL=%q", controlURL)
288+
browser := rod.New().ControlURL(controlURL).MustConnect()
289+
klog.V(1).Info("Connected to browser.")
290+
page := browser.MustPage(url)
291+
klog.V(1).Infof("Connected to page in browser, waiting for opening of page %q", url)
282292
page.MustWaitStable()
283293

284294
if *flagConsoleLog {

docs/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# GoNB Changelog
22

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

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

1214
## v0.10.5, Added SendAsDownload
1315

docs/coverage.txt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ github.com/janpfeifer/gonb/cache/cache.go CacheWith 91.7%
2828
github.com/janpfeifer/gonb/cache/cache.go Cache 0.0%
2929
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go main 74.3%
3030
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go startJupyterNotebook 73.2%
31-
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go executeNotebook 78.6%
31+
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go executeNotebook 82.4%
3232
github.com/janpfeifer/gonb/cmd/nbexec/nbexec.go checkForInputBoxes 95.7%
3333
github.com/janpfeifer/gonb/common/common.go Panicf 0.0%
3434
github.com/janpfeifer/gonb/common/common.go Pause 0.0%
@@ -68,12 +68,13 @@ github.com/janpfeifer/gonb/gonbui/gonbui.go pollReaderPipe 66.7%
6868
github.com/janpfeifer/gonb/gonbui/gonbui.go Sync 92.9%
6969
github.com/janpfeifer/gonb/gonbui/gonbui.go UniqueId 100.0%
7070
github.com/janpfeifer/gonb/gonbui/gonbui.go UniqueID 100.0%
71-
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtml 66.7%
72-
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTML 100.0%
71+
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtml 100.0%
72+
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTML 66.7%
73+
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHTMLF 0.0%
7374
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayHtmlf 0.0%
7475
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayMarkdown 66.7%
75-
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHtml 66.7%
76-
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHTML 0.0%
76+
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHTML 66.7%
77+
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateHtml 100.0%
7778
github.com/janpfeifer/gonb/gonbui/gonbui.go UpdateMarkdown 0.0%
7879
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayPng 0.0%
7980
github.com/janpfeifer/gonb/gonbui/gonbui.go DisplayPNG 0.0%
@@ -198,12 +199,12 @@ github.com/janpfeifer/gonb/internal/goexec/errorpublish.go JupyterErrorSplit
198199
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.ExecuteCell 100.0%
199200
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.serializeExecuteCell 100.0%
200201
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.executeCellImpl 68.0%
201-
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.PostExecuteCell 88.9%
202+
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.PostExecuteCell 85.7%
202203
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.BinaryPath 100.0%
203204
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.CodePath 100.0%
204205
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.RemoveCode 83.3%
205206
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.AlternativeDefinitionsPath 0.0%
206-
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Execute 77.8%
207+
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Execute 85.7%
207208
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.Compile 70.0%
208209
github.com/janpfeifer/gonb/internal/goexec/execcode.go *State.GoImports 70.6%
209210
github.com/janpfeifer/gonb/internal/goexec/execcode.go newJupyterStackTraceMapperWriter 75.0%
@@ -321,10 +322,11 @@ github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.UseNamedPipes
321322
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.ExecutionCount 100.0%
322323
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.InDir 100.0%
323324
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStderr 100.0%
324-
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStdout 0.0%
325+
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStdout 100.0%
325326
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithInputs 100.0%
326327
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithPassword 100.0%
327328
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.WithStaticInput 100.0%
329+
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.CaptureDisplayDataOutput 100.0%
328330
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.Exec 68.8%
329331
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.done 92.3%
330332
github.com/janpfeifer/gonb/internal/jpyexec/jpyexec.go *Executor.handleJupyterInput 91.3%
@@ -335,7 +337,7 @@ github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.createTmpF
335337
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.openPipeReader 89.3%
336338
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.pollNamedPipeReader 73.1%
337339
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.reportCellError 0.0%
338-
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchDisplayData 78.6%
340+
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchDisplayData 80.0%
339341
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.dispatchInputRequest 83.3%
340342
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.openPipeWriter 86.7%
341343
github.com/janpfeifer/gonb/internal/jpyexec/namedpipes.go *Executor.pollPipeWriterFifo 66.7%
@@ -409,11 +411,11 @@ github.com/janpfeifer/gonb/internal/specialcmd/definitions.go removeDefinitionI
409411
github.com/janpfeifer/gonb/internal/specialcmd/definitions.go removeDefinitions 91.7%
410412
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go Parse 84.6%
411413
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go joinLine 87.5%
412-
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execSpecialConfig 55.3%
414+
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execSpecialConfig 58.2%
413415
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go execShell 100.0%
414416
github.com/janpfeifer/gonb/internal/specialcmd/specialcmd.go splitCmd 97.0%
415417
github.com/janpfeifer/gonb/internal/specialcmd/track.go execTrack 27.3%
416418
github.com/janpfeifer/gonb/internal/specialcmd/track.go execUntrack 54.5%
417419
github.com/janpfeifer/gonb/internal/specialcmd/track.go showTrackedList 91.7%
418420
github.com/janpfeifer/gonb/internal/websocket/websocket.go Javascript 83.3%
419-
total (statements) 64.4%
421+
total (statements) 64.6%

examples/tests/capture.ipynb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "fe105ef6-5264-4cb4-b467-60d9b0a337fd",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"Ping\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"%capture ${TEST_DIR}/pingpong.txt\n",
19+
"%%\n",
20+
"fmt.Println(\"Ping\")"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"id": "ee9acf4f-2488-4f92-bfae-6998d8351175",
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"Ping\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"!cat ${TEST_DIR}/pingpong.txt"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 3,
44+
"id": "004e9622-e3a4-4780-816a-e5d1c77055b4",
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"name": "stdout",
49+
"output_type": "stream",
50+
"text": [
51+
"Pong\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"%capture -a ${TEST_DIR}/pingpong.txt\n",
57+
"%%\n",
58+
"fmt.Println(\"Pong\")"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 4,
64+
"id": "aee500e4-5728-4224-9746-f61d58909851",
65+
"metadata": {},
66+
"outputs": [
67+
{
68+
"name": "stdout",
69+
"output_type": "stream",
70+
"text": [
71+
"Ping\n",
72+
"Pong\n"
73+
]
74+
}
75+
],
76+
"source": [
77+
"!cat ${TEST_DIR}/pingpong.txt"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 5,
83+
"id": "c8214b58-5946-4db5-a329-9e2f2c2fc215",
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"data": {
88+
"text/html": [
89+
"Ping\n",
90+
"Pong\n"
91+
]
92+
},
93+
"metadata": {},
94+
"output_type": "display_data"
95+
}
96+
],
97+
"source": [
98+
"%capture ${TEST_DIR}/pingpong.txt\n",
99+
"import \"github.com/janpfeifer/gonb/gonbui\"\n",
100+
"%%\n",
101+
"gonbui.DisplayHTML(\"Ping\\nPong\\n\")"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 6,
107+
"id": "f67d51c5-dbcb-4e96-aba5-380bc55dbf53",
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"Ping\n",
115+
"Pong\n"
116+
]
117+
}
118+
],
119+
"source": [
120+
"!cat ${TEST_DIR}/pingpong.txt"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 7,
126+
"id": "9b88a712-deae-4d48-80a1-c4795db31c22",
127+
"metadata": {},
128+
"outputs": [
129+
{
130+
"data": {
131+
"text/markdown": [
132+
"# Ping\n",
133+
"# Pong\n"
134+
]
135+
},
136+
"metadata": {},
137+
"output_type": "display_data"
138+
}
139+
],
140+
"source": [
141+
"%capture ${TEST_DIR}/pingpong.txt\n",
142+
"import \"github.com/janpfeifer/gonb/gonbui\"\n",
143+
"%%\n",
144+
"gonbui.DisplayMarkdown(\"# Ping\\n# Pong\\n\")"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 8,
150+
"id": "e884811a-e7c9-425b-aa0b-394b1628ccd8",
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"# Ping\n",
158+
"# Pong\n"
159+
]
160+
}
161+
],
162+
"source": [
163+
"!cat ${TEST_DIR}/pingpong.txt"
164+
]
165+
}
166+
],
167+
"metadata": {
168+
"kernelspec": {
169+
"display_name": "Go (gonb)",
170+
"language": "go",
171+
"name": "gonb"
172+
},
173+
"language_info": {
174+
"codemirror_mode": "",
175+
"file_extension": ".go",
176+
"mimetype": "",
177+
"name": "go",
178+
"nbconvert_exporter": "",
179+
"pygments_lexer": "",
180+
"version": "go1.23.2"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 5
185+
}

0 commit comments

Comments
 (0)