Skip to content

Commit

Permalink
Added cache entry in tutorial. Improved docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
janpfeifer committed May 7, 2023
1 parent 8fec00f commit d253793
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ it in a REPL (Read-Eval-Print-Loop) fashion, by inserting a "Compile" step in th
of the loop -- so it's a Read-Compile-Run-Print-Loop -- while still feeling very interactive.

**GoNB** leverages that compilation speed to implement a full-featured (at least it's getting there)
[Jupyter notebook](https://jupyter.org/) kernel.
[Jupyter notebook](https://jupyter.org/) kernel. As a side benefit it works with packages that use CGO -- although it
won't parse C code in the cells, so it can't be used as a C kernel.

It already includes many goodies: contextual help and auto-complete (with
It already includes many goodies: cache between cell of results, contextual help and auto-complete (with
[`gopls`](https://github.com/golang/tools/tree/master/gopls)), compilation error context (by
mousing over), bash command execution, images, html, etc. See the [tutorial](examples/tutorial.ipynb).

Expand Down Expand Up @@ -101,9 +102,6 @@ Contributions are welcome!
* Windows version:
* Installation.
* Named-pipe implementation in `kernel/pipeexec.go`.
* Tracking of lines on generated Go files back to cell, so reported errors are easy to
follow. In the meantime the errors can be moused over and will display the lines
surrounding them.
* Controllable (per package or file) logging in GoNB code.
* Run `goimports` before calling `gopls` to inspect a variable, or auto-complete. This
would be handy, but complicates tracking the position of the cursor on the changed
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GoNB Changelog

## Next version
## v0.6.0

* Issue #16: Added package `cache`: implements a convenient cache of values for things that
are expensive or slow to regenerate at each execution.
Expand Down
58 changes: 57 additions & 1 deletion examples/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
}
],
"source": [
"!*go mod edit --replace github.com/janpfeifer/gonb=/home/janpf/Projects/gonb\n",
"func main() {\n",
" fmt.Printf(\"Hello World!\")\n",
"}"
Expand Down Expand Up @@ -186,6 +187,61 @@
"fmt.Printf(\"current startValue=%f\\n\", startValue)"
]
},
{
"cell_type": "markdown",
"id": "f4fc0797-df7f-4652-8a22-e8b489580e1d",
"metadata": {},
"source": [
"If one wants to save results calculated from one cell to another, GoNB includes the `github.com/janpfeifer/gonb/cache` package that makes it trivial to save and load previously generated results.\n",
"\n",
"**Example**: Below `VeryExpensive` is only called once for `CachedValue`, so you will notice that if you run the cell multiple times, it will display always the same number, while `NonCachedValue` will always call `VeryExpensive` again, and display another number. So the string \"...calculating...\" is printed twice only the first time."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "d59e0cbd-0d2a-42e5-8f94-29936130f437",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\t...calculating...\n",
"NonCachedValue=575\n",
" CachedValue=36\n"
]
}
],
"source": [
"import (\n",
" \"math/rand\"\n",
" \"github.com/janpfeifer/gonb/cache\"\n",
")\n",
"\n",
"func VeryExpensive() int {\n",
" fmt.Println(\"\\t...calculating...\")\n",
" return rand.Intn(1000)\n",
"}\n",
"\n",
"var (\n",
" CachedValue = cache.Cache(\"expensive\", VeryExpensive)\n",
" NonCachedValue = VeryExpensive()\n",
")\n",
" \n",
"%%\n",
"fmt.Printf(\"NonCachedValue=%d\\n\", NonCachedValue)\n",
"fmt.Printf(\" CachedValue=%d\\n\", CachedValue)"
]
},
{
"cell_type": "markdown",
"id": "5a5470b4-43ea-47ae-a111-63ab77376bd0",
"metadata": {},
"source": [
"The `cache` package has many more features, check out "
]
},
{
"cell_type": "markdown",
"id": "83c85334-6323-4b75-b9a2-184c0d83ed1c",
Expand Down Expand Up @@ -1325,7 +1381,7 @@
"name": "go",
"nbconvert_exporter": "",
"pygments_lexer": "",
"version": "go1.20.1"
"version": "go1.20.3"
}
},
"nbformat": 4,
Expand Down

0 comments on commit d253793

Please sign in to comment.