notebook

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CellTypeMarkdown     = "markdown"
	CellTypeRaw          = "raw"
	CellTypeCode         = "code"
	CellTypeUnrecognized = "unrecognized"
)
View Source
const (
	OutputTypeStream        = "stream"
	OutputTypeExecuteResult = "execute_result"
	OutputTypeDisplayData   = "display_data"
	OutputTypeError         = "error"
)
View Source
const SVGMime = "image/svg+xml"

Variables

View Source
var ErrNullBoolNotAllowed = errors.New("null bool not allowed")
View Source
var ErrNullIntNotAllowed = errors.New("null integer not allowed")
View Source
var ErrNullStringListNotAllowed = errors.New("null string list not allowed")
View Source
var ErrNullStringNotAllowed = errors.New("null string not allowed")

Functions

func LoadFixture

func LoadFixture(t *testing.T, relPath ...string) []byte

func PassesNBConvert

func PassesNBConvert(nbPath, jupyterConvertPath string) (string, bool)

func PathRelativeToTest

func PathRelativeToTest(t *testing.T, relPath ...string) string

func ProduceNotebookPaths

func ProduceNotebookPaths(ctx context.Context, rootDir string, bufSize int) <-chan string

I wrote this to analyze a large collection of Jupyter notebooks cloned from github as repositories.

func TempDir

func TempDir(t *testing.T) (string, func())

Types

type Author

type Author struct {
	Name   string            `json:"name"`
	Extras map[string]string `json:"-"`
}

func (Author) MarshalJSON

func (a Author) MarshalJSON() ([]byte, error)

type Base64Figure added in v0.0.6

type Base64Figure struct {
	Id          string
	ContentType string
	Url         string
	Data        []byte
	Alt         string
	Caption     string
	IsPrimary   bool
	FullSize    bool
	Href        string
	Resource    *Resource
}

func NewBase64Figure added in v0.0.6

func NewBase64Figure(contentType string, data []byte) (*Base64Figure, error)

func NewSVG added in v0.0.6

func NewSVG(contentType string, data []byte) *Base64Figure

type Cell

type Cell interface {
	GetType() string
	GetInput() []string
	GetOutputs() []Output
	GetMetadata() CellMetadata

	RenderInput(r Renderer) error
	RenderOutput(r Renderer) error
	GetRaw() []byte
}

type CellMetadata

type CellMetadata struct {
	Name      string   `json:"name,omitempty"`
	Tags      []string `json:"tags,omitempty"`
	Collapsed bool     `json:"collapsed,omitempty"`
	Editable  bool     `json:"editable,omitempty"`
	Deletable bool     `json:"deletable,omitempty"`
	Format    string   `json:"format,omitempty"`

	Jupyter JupyterMetaData `json:"jupyter,omitempty"`
}

See: https://nbformat.readthedocs.io/en/latest/format_description.html#cell-metadata

type CodeCell

type CodeCell struct {
	CellType string       `json:"cell_type"`
	Metadata CellMetadata `json:"metadata"`
	Source   []string     `json:"source"`
	Outputs  []Output     `json:"outputs"`
	Raw      []byte       `json:"-"`
}

func (CodeCell) GetInput

func (c CodeCell) GetInput() []string

func (CodeCell) GetMetadata

func (c CodeCell) GetMetadata() CellMetadata

func (CodeCell) GetOutputs

func (c CodeCell) GetOutputs() []Output

func (CodeCell) GetRaw

func (c CodeCell) GetRaw() []byte

func (CodeCell) GetType

func (c CodeCell) GetType() string

func (CodeCell) RenderInput

func (c CodeCell) RenderInput(r Renderer) error

func (CodeCell) RenderOutput

func (c CodeCell) RenderOutput(r Renderer) error

type CodeMirrorMode

type CodeMirrorMode struct {
	Name    string `json:"name,omitempty"`
	Version int    `json:"version,omitempty"`
}

type DisplayData

type DisplayData struct {
	OutputType string         `json:"output_type"`
	Data       MimeBundle     `json:"data"`
	Metadata   OutputMetadata `json:"metadata"`
}

func (DisplayData) Render

func (s DisplayData) Render(r Renderer) error

func (DisplayData) Type

func (s DisplayData) Type() string

type ErrorOutput

type ErrorOutput struct {
	OutputType string   `json:"output_type"`
	EName      string   `json:"ename"`
	EValue     string   `json:"evalue"`
	Traceback  []string `json:"traceback"`
}

func (ErrorOutput) Render

func (s ErrorOutput) Render(r Renderer) error

func (ErrorOutput) Type

func (s ErrorOutput) Type() string

type ExecuteResult

type ExecuteResult struct {
	OutputType   string         `json:"output_type"`
	ExecuteCount int            `json:"execution_count"`
	Data         MimeBundle     `json:"data"`
	Metadata     OutputMetadata `json:"metadata"`
}

func (ExecuteResult) Render

func (s ExecuteResult) Render(r Renderer) error

func (ExecuteResult) Type

func (s ExecuteResult) Type() string

type Falsifiable

type Falsifiable struct {
	Title     string   `json:"title,omitempty"`
	Subtitle  string   `json:"subtitle,omitempty"`
	CreatedAt string   `json:"created_at,omitempty"`
	UpdatedAt string   `json:"updated_at,omitempty"`
	Authors   []string `json:"authors,omitempty"`

	User string `json:"user,omitempty"`
	Slug string `json:"slug,omitempty"`
	Repo string `json:"repo,omitempty"`

	PrimaryImageCH string `json:"-"`
}

type JupyterMetaData

type JupyterMetaData struct {
	SourceHidden  bool `json:"source_hidden,omitempty"`
	OutputsHidden bool `json:"outputs_hidden,omitempty"`
}

type KernelSpec

type KernelSpec struct {
	Name        string `json:"name"`
	DisplayName string `json:"display_name"`
	Language    string `json:"language"`
}

type LanguageInfo

type LanguageInfo struct {
	CodeMirrorMode    CodeMirrorMode `json:"codemirror_mode,omitempty"`
	FileExtension     string         `json:"file_extension,omitempty"`
	MimeType          string         `json:"mimetype,omitempty"`
	Name              string         `json:"name,omitempty"`
	NBConvertExporter string         `json:"nbconvert_exporter,omitempty"`
	PygmentsLexer     string         `json:"pygments_lexer,omitempty"`
	Version           string         `json:"version,omitempty"`
}

type MarkdownCell

type MarkdownCell struct {
	CellType string       `json:"cell_type"`
	Metadata CellMetadata `json:"metadata"`
	Source   []string     `json:"source"`
	Raw      []byte       `json:"-"`
}

func (MarkdownCell) GetInput

func (c MarkdownCell) GetInput() []string

func (MarkdownCell) GetMetadata

func (c MarkdownCell) GetMetadata() CellMetadata

func (MarkdownCell) GetOutputs

func (c MarkdownCell) GetOutputs() []Output

func (MarkdownCell) GetRaw

func (c MarkdownCell) GetRaw() []byte

func (MarkdownCell) GetType

func (c MarkdownCell) GetType() string

func (MarkdownCell) RenderInput

func (c MarkdownCell) RenderInput(r Renderer) error

func (MarkdownCell) RenderOutput

func (c MarkdownCell) RenderOutput(r Renderer) error

type MetaData

type MetaData struct {
	LanguageInfo LanguageInfo `json:"language_info"`
	KernelSpec   KernelSpec   `json:"kernelspec"`
	Falsifiable  Falsifiable  `json:"falsifiable"`
	OrigFmt      int          `json:"orig_nbformat,omitempty"` // TODO
	Title        string       `json:"title,omitempty"`         // TODO
	Authors      []Author     `json:"authors,omitempty"`
	// TODO: SPDX license keys
	Raw []byte `json:"-"`
}

type MimeBundle

type MimeBundle = map[string][]string

type Notebook

type Notebook struct {
	VersionMajor int      `json:"nbformat"`
	VersionMinor int      `json:"nbformat_minor"`
	MetaData     MetaData `json:"metadata"`
	Cells        []Cell   `json:"cells"`

	Raw []byte `json:"-"`
}

func Parse added in v0.0.7

func Parse(data []byte, opts ...Opt) (*Notebook, error)

func (*Notebook) Marshal

func (nb *Notebook) Marshal() ([]byte, error)

type Opt

type Opt func(p *Parser)

func RetainRaw added in v0.0.4

func RetainRaw() Opt

func Strict added in v0.0.4

func Strict() Opt

type Output

type Output interface {
	Type() string
	Render(r Renderer) error
}

type OutputFigureMetadata added in v0.0.6

type OutputFigureMetadata struct {
	Alt       string `json:"alt,omitempty"`
	Caption   string `json:"caption,omitempty"`
	Href      string `json:"href,omitempty"`
	FullSize  bool   `json:"fullsize,omitempty"`
	IsPrimary bool   `json:"primary,omitempty"`
}

type OutputMetadata

type OutputMetadata struct {
	NeedsBackground string `json:"needs_background,omitempty"`
	FigureMeta      map[string]OutputFigureMetadata
}

type ParseError

type ParseError struct {
	// contains filtered or unexported fields
}

func NewParseError

func NewParseError(offset int, path string, msg string, args ...interface{}) *ParseError

func (*ParseError) Error

func (p *ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser(opts ...Opt) *Parser

func (*Parser) Parse

func (p *Parser) Parse(data []byte) (*Notebook, error)

func (*Parser) ParseAuthor

func (p *Parser) ParseAuthor(authorIdx int, data []byte) (Author, error)

func (*Parser) ParseAuthors

func (p *Parser) ParseAuthors(data []byte, offset int) ([]Author, error)

func (*Parser) ParseCell

func (p *Parser) ParseCell(cellIdx int, data []byte, offset int) (Cell, error)

func (*Parser) ParseCellMetadata

func (p *Parser) ParseCellMetadata(cellIdx int, data []byte, offset int) (CellMetadata, error)

func (*Parser) ParseCells

func (p *Parser) ParseCells(data []byte, offset int) ([]Cell, error)

func (*Parser) ParseCodeMirrorMode

func (p *Parser) ParseCodeMirrorMode(data []byte, offset int) (CodeMirrorMode, error)

func (*Parser) ParseFalsifiable

func (p *Parser) ParseFalsifiable(data []byte, offset int) (Falsifiable, error)

func (*Parser) ParseJupyterMetadata

func (p *Parser) ParseJupyterMetadata(cellIdx int, data []byte, offset int) (JupyterMetaData, error)

func (*Parser) ParseKernelSpec

func (p *Parser) ParseKernelSpec(data []byte, offset int) (KernelSpec, error)

func (*Parser) ParseLanguageInfo

func (p *Parser) ParseLanguageInfo(data []byte, offset int) (LanguageInfo, error)

func (*Parser) ParseMetadata

func (p *Parser) ParseMetadata(data []byte, offset int) (MetaData, error)

func (*Parser) ParseOutput

func (p *Parser) ParseOutput(cellIdx, outputIdx int, data []byte, offset int) (Output, error)

func (*Parser) ParseOutputFigureMetadata added in v0.0.6

func (p *Parser) ParseOutputFigureMetadata(cellIdx, outputIdx int, data []byte, contentType string) (OutputFigureMetadata, error)

func (*Parser) ParseOutputMetadata

func (p *Parser) ParseOutputMetadata(cellIdx, outputIdx int, data []byte, offset int) (OutputMetadata, error)

func (*Parser) ParseOutputs

func (p *Parser) ParseOutputs(cellIdx int, data []byte, offset int) ([]Output, error)

func (*Parser) Read

func (p *Parser) Read(filePath string) (*Notebook, error)

type RawCell

type RawCell struct {
	CellType string       `json:"cell_type"`
	Metadata CellMetadata `json:"metadata"`
	Source   []string     `json:"source"`
	Raw      []byte       `json:"-"`
}

func (RawCell) GetInput

func (c RawCell) GetInput() []string

func (RawCell) GetMetadata

func (c RawCell) GetMetadata() CellMetadata

func (RawCell) GetOutputs

func (c RawCell) GetOutputs() []Output

func (RawCell) GetRaw

func (c RawCell) GetRaw() []byte

func (RawCell) GetType

func (c RawCell) GetType() string

func (RawCell) RenderInput

func (c RawCell) RenderInput(r Renderer) error

func (RawCell) RenderOutput

func (c RawCell) RenderOutput(r Renderer) error

type Renderer

type Renderer interface {
	SetMetadata(metadata MetaData)
	GetDefaultLang() string

	WriteOutputs(outputs []Output) error
	WriteHTMLUnsafely(lines []string) error
	GetResources() map[string]*Resource
	WriteB64Image(fig *Base64Figure) (string, error)
	WriteHTMLEscapedLines(lines []string) error
	WriteMarkdownLines(lines []string) error
	WriteMarkdownCodeFenced(lang string, lines []string) error
	GetResource(contentHash string) *Resource
	WritePlainText(class string, lines []string, clean bool) error
}

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

func NewResource

func NewResource(contentType string, data []byte) *Resource

func (*Resource) ContentHash

func (r *Resource) ContentHash() []byte

Lazily computes the Base58(SHA256(data))

func (*Resource) ContentType

func (r *Resource) ContentType() string

func (*Resource) Data

func (r *Resource) Data() []byte

func (*Resource) Md5 added in v0.0.4

func (r *Resource) Md5() []byte

todo: http://marcio.io/2015/07/calculating-multiple-file-hashes-in-a-single-pass/

func (*Resource) Size added in v0.0.4

func (r *Resource) Size() int

type StreamOutput

type StreamOutput struct {
	Name       string   `json:"name"`
	OutputType string   `json:"output_type"`
	Text       []string `json:"text"`
}

func (StreamOutput) Render

func (s StreamOutput) Render(r Renderer) error

func (StreamOutput) Type

func (s StreamOutput) Type() string

type UnrecognizedCell

type UnrecognizedCell struct {
	CellType string       `json:"cell_type"`
	Metadata CellMetadata `json:"metadata"`
	Raw      []byte       `json:"-"`
}

func (UnrecognizedCell) GetInput

func (c UnrecognizedCell) GetInput() []string

func (UnrecognizedCell) GetMetadata

func (c UnrecognizedCell) GetMetadata() CellMetadata

func (UnrecognizedCell) GetOutputs

func (c UnrecognizedCell) GetOutputs() []Output

func (UnrecognizedCell) GetRaw

func (c UnrecognizedCell) GetRaw() []byte

func (UnrecognizedCell) GetType

func (c UnrecognizedCell) GetType() string

func (UnrecognizedCell) RenderInput

func (c UnrecognizedCell) RenderInput(r Renderer) error

func (UnrecognizedCell) RenderOutput

func (c UnrecognizedCell) RenderOutput(r Renderer) error

Jump to

Keyboard shortcuts

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