gopherdata / gophernotes

The Go kernel for Jupyter notebooks and nteract.
MIT License
3.85k stars 263 forks source link

MIME rendering #110

Closed SpencerPark closed 6 years ago

SpencerPark commented 6 years ago

Initial support for rendering richer displays of data in the notebook. The approach was designed to follow IPython's API as far as it applies in Go. Any code that needs access to the kernel should be injected right before evaluation but generic user space stuff should go in the runtime package.

An example I've been playing around with is below. There is still a bug I can't quite track down yet with the zmq message being somehow corrupted during Publish and so in random cases the results may or may not show up due to the message being rejected (which means if it doesn't work try it a second time until this is fixed). (Fixed in 710233fed632bf3f38d7dc1d25cb2db59f6d7ccc)

import "fmt"
import r "github.com/gopherdata/gophernotes/runtime"

type Test struct {
    Color string
}

func (t Test) RenderAsSVG() string {
    return "<svg><rect x=\"0\" y=\"0\" width=\"100\" height=\"100\" fill=\"" + t.Color + "\"/></svg>"
}

func (t Test) RenderAsMarkdown() string {
    return "**Color:** " + t.Color
}

t := Test{"blue"}

fmt.Println(t)
Display(r.SVG(t))
Display(r.Markdown(t))

Still TODO:

SpencerPark commented 6 years ago

Ran into trouble with cosmos72/gomacro#16. A lot of the rendering logic requires type assertions and unfortunately many of those are interfaces.

cosmos72 commented 6 years ago

If you don't mind, I tried to simplify your code, see issue #17 and pull request https://github.com/gopherdata/gophernotes/pull/105

SpencerPark commented 6 years ago

If you don't mind, I tried to simplify your code

Not at all! You know gomacro's capabilities much better than I do :) I'll move the discussion over there.

cosmos72 commented 6 years ago

as per discussion in #105, I merged that pull request