gonbui

package
v0.0.0-...-dc643fe Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT, MIT Imports: 11 Imported by: 0

README

GoNBUI

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

GoNBUI is a library that 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 DisplayPNG

func DisplayPNG(png []byte)

DisplayPNG displays the given PNG, given as raw bytes.

func DisplaySVG

func DisplaySVG(svg string)

func EmbedImageAsPNGSrc

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 UniqueID

func UniqueID() string

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

func UpdateHTML

func UpdateHTML(id, html string)

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

The contents of these cells 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.

```

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