gonbui

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: MIT, MIT Imports: 14 Imported by: 4

README

GoNBUI

GoNB is a Jupyter notebook kernel able to run Go code.

gonbui package allows any go code ran in GoNB to easily display various type of rich content in the notebook. Currently supported:

  • HTML: An arbitrary HTML block, and it also allows updates to a block (e.g.: updates to some ongoing processing).
  • Images: Any given Go image (automatically rendered as PNG); a PNG file content; SVG.
  • Javascript: To be run in the Notebook.
  • Input request from the notebook.

More (sound, video, etc.) can be quite easily added as well, expect the list to grow.

Documentation

Overview

Package gonbui provides tools to interact with the front-end (the notebook) using HTML and other rich-data.

In its simplest form, simply use `DisplayHtml` to display HTML content. But there is much more nuance and powerful types of interactions (including support for widgets, see the `widget` sup-package). Check out the [tutorial]() for details.

If using the rich data content, consider adding the following to your `main()` function:

defer gonbui.Sync()

This guarantees that no in-transit display content get left behind when a program exits.

Index

Constants

This section is empty.

Variables

View Source
var Debug bool
View Source
var (
	// IsNotebook indicates whether the execution was started by a GoNB kernel.
	IsNotebook bool
)
View Source
var OnCommValueUpdate func(valueMsg *protocol.CommValue)

OnCommValueUpdate handler and dispatcher of value updates.

Internal use only -- used by `gonb/gonbui/comms`.

Functions

func DisplayHTML

func DisplayHTML(html string)

DisplayHTML is an alias to DisplayHtml.

func DisplayHtml added in v0.9.0

func DisplayHtml(html string)

DisplayHtml will display the given HTML in the notebook, as the output of the cell being executed.

func DisplayHtmlf added in v0.9.0

func DisplayHtmlf(htmlFormat string, args ...any)

DisplayHtmlf is similar to DisplayHtml, but it takes a format string and its args which are passed to fmt.Sprintf.

func DisplayImage

func DisplayImage(image image.Image) error

DisplayImage displays the given image, by converting it to PNG first. It returns an error if it fails to encode to the image to PNG.

func DisplayMarkdown added in v0.7.7

func DisplayMarkdown(markdown string)

DisplayMarkdown will display the given markdown content in the notebook, as the output of the cell being executed. This also renders math formulas using latex, use `$x^2$` for formulas inlined in text, or double "$" for formulas in a separate line -- e.g.: `$$f(x) = \int_{-\infty}^{\infty} e^{-x^2} dx$$`.

func DisplayPNG

func DisplayPNG(png []byte)

DisplayPNG is an alias for DisplayPng. Deprecated: use DisplayPng instead.

func DisplayPng added in v0.9.0

func DisplayPng(png []byte)

DisplayPng displays the given PNG, given as raw bytes.

func DisplaySVG

func DisplaySVG(svg string)

DisplaySVG is an alias for DisplaySvg. Deprecated: use DisplaySvg instead.

func DisplaySvg added in v0.9.0

func DisplaySvg(svg string)

func EmbedImageAsPNGSrc added in v0.4.0

func EmbedImageAsPNGSrc(img image.Image) (string, error)

EmbedImageAsPNGSrc returns a string that can be used as in an HTML <img> tag, as its source (it's `src` field). This simplifies embedding an image in HTML without requiring separate files. It embeds it as a PNG file base64 encoded.

func Error

func Error() error

Error returns the error that triggered failure on the communication with GoNB. It returns nil if there were no errors.

It can be tested as a health check.

func Logf added in v0.9.0

func Logf(format string, args ...any)

Logf is used to log debugging messages for the gonbui library. It is enabled by setting the global Debug to true.

Usually only useful for those developing new widgets and the like.

func Open

func Open() error

Open pipes used to communicate to GoNB (and through it, to the front-end). This can be called every time, if connections are already opened, it does nothing.

Users don't need to use this directly, since this is called every time by all other functions.

Returns nil if succeeded (or if connections were already opened).

func RequestInput added in v0.7.4

func RequestInput(prompt string, password bool)

RequestInput from the Jupyter notebook. It triggers the opening of a small text field in the cell output area where the user can type something. Whatever the user writes is written to the stdin of cell program -- and can be read, for instance, with `fmt.Scanf`.

Args:

  • prompt: string displayed in front of the field to be entered. Leave empty ("") if not needed.
  • password: if whatever the user is typing is not to be displayed.

func ScriptJavascript added in v0.9.0

func ScriptJavascript(js string)

ScriptJavascript executes the given Javascript script in the Notebook.

Errors in javascript parsing are sent by Jupyter Server to the stderr -- as opposed to showing to the browser console, which may be harder to debug.

Also, like with DisplayHtml, each execution creates a new `<div>` block in the output area. Even if empty, it uses up a bit of vertical space (Jupyter Notebook thing).

If these are an issue, consider using TransientJavascript, which uses a transient area to execute the Javascript, which is re-used for every execution.

Note: `text/javascript` mime-type (protocol.MIMETextJavascript) is not supported by VSCode, it's displayed as text. So using this won't work in VSCode. Consider instead using DisplayHtml, and wrapping the `js` string with `("<scrip>%s</script>", js)`.

func SendData added in v0.9.0

func SendData(data *protocol.DisplayData)

SendData to be displayed in the connected Notebook.

This is a lower level function, that most end users won't need to use, instead look for the other functions DisplayHtml, DisplayMarkdown, etc.

But if you are testing new types of MIME types, this is the way to result messages ("execute_result" message type) directly to the front-end.

func Sync added in v0.9.0

func Sync()

Sync synchronizes with GoNB, and can be used to make sure all pending output has been sent.

This can be used at the end of a program to make sure that everything that is in the pipe to be displayed is fully displayed (flushed) before a program exits.

func UniqueID deprecated added in v0.4.0

func UniqueID() string

UniqueID returns a newly created unique id. UniqueID is an alias for UniqueId.

Deprecated: Use UniqueId instead.

func UniqueId added in v0.9.0

func UniqueId() string

UniqueId returns newly created unique id. It can be used for instance with UpdateHtml.

func UpdateHTML added in v0.4.0

func UpdateHTML(id, html string)

UpdateHTML is an alias for UpdateHtml. Deprecated: use UpdateHtml instead, it's the same.

func UpdateHtml added in v0.9.0

func UpdateHtml(id, html string)

UpdateHtml displays the given HTML in the notebook on an output block with the given `id`: the block identified by 'id' is created automatically the first time this function is called, and simply updated thereafter.

The contents of these output blocks are considered transient, and intended to live only during a kernel session.

Usage example:

counterDisplayId := "counter_"+gonbui.UniqueId()
for ii := 0; ii < 10; ii++ {
	gonbui.UpdateHtml(counterDisplayId, fmt.Sprintf("Count: <b>%d</b>\n", ii))
}
gonbui.UpdateHtml(counterDisplayId, "")  // Erase transient block.
gonbui.DisplayHtml(fmt.Sprintf("Count: <b>%d</b>\n", ii))  // Show on final block.

Notice that the value of `counterDisplayId` is not a DOM element id -- unfortunately. If you want a `<div>` that you can manipulate with the [dom] package, create an empty `<div id=%q></div>` with another unique id (see gonbui.UniqueID) and use that instead.

func UpdateMarkdown added in v0.7.7

func UpdateMarkdown(id, markdown string)

UpdateMarkdown updates the contents of the output identified by id: the block identified by 'id' is created automatically the first time this function is called, and simply updated thereafter.

The contents of these output blocks are considered transient, and intended to live only during a kernel session.

See example in UpdateHtml, just instead this used Markdown content.

Types

This section is empty.

Directories

Path Synopsis
Package comms implements a protocol of communication wih the front-ent (notebook), and it's used to implement widgets.
Package comms implements a protocol of communication wih the front-ent (notebook), and it's used to implement widgets.
Package plotly adds barebones support to Plotly (https://plotly.com/javascript/getting-started/) library.
Package plotly adds barebones support to Plotly (https://plotly.com/javascript/getting-started/) library.
Package protocol contains the definition of the objects that are serialized and communicated to the kernel, using the standard Go `encoding/gob` package.
Package protocol contains the definition of the objects that are serialized and communicated to the kernel, using the standard Go `encoding/gob` package.
Package wasm defines several utilities to facilitate the writing of small WASM widgets in GoNB (or elsewhere).
Package wasm defines several utilities to facilitate the writing of small WASM widgets in GoNB (or elsewhere).
Package widgets implement several simple widgets that can be used to make your Go programs interact with front-end widgets in a Jupyter Notebook, using GoNB kernel.
Package widgets implement several simple widgets that can be used to make your Go programs interact with front-end widgets in a Jupyter Notebook, using GoNB kernel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL