gonbui

package
v0.7.8 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT, MIT Imports: 12 Imported by: 6

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// IsNotebook indicates whether the execution was started by a GoNB kernel.
	IsNotebook bool
)

Functions

func DisplayHTML

func DisplayHTML(html string)

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

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.

func DisplayPNG

func DisplayPNG(png []byte)

DisplayPNG displays the given PNG, given as raw bytes.

func DisplaySVG

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 first error that may have happened in communication to the kernel. Nil if there has been no errors.

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 UniqueID added in v0.4.0

func UniqueID() string

UniqueID returns a unique id that can be used for UpdateHTML. It should be generated once per display block the program wants to update.

func UpdateHTML added in v0.4.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:

```go

counterDisplayId := 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.

```

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 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳