Skip to content

Commit

Permalink
feat: decode v3.0 notebooks
Browse files Browse the repository at this point in the history
Prior to v4.0:
- top-level 'worksheets' contained multiple worksheets with the actual 'cells'
- execution_results was called pyout
- error output was called pyerr
- code cell 'source' was called 'input'; execution_count was called prompt_number
- mime-bundle explicitly defined keys for all mime-tyipes which it supported and
had to be decoded differently

BREAKING: decode.Decoder interface not inlcudes ExtractCells method to handle the
deprecation of top-level 'worksheets'
  • Loading branch information
bevzzz committed Feb 24, 2024
1 parent 96767ed commit 230f0ff
Show file tree
Hide file tree
Showing 6 changed files with 753 additions and 9 deletions.
18 changes: 16 additions & 2 deletions decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ func (n *notebook) UnmarshalJSON(data []byte) error {
return fmt.Errorf("%s: notebook metadata: %w", ver, err)
}

n.cells = make([]schema.Cell, len(n.Notebook.Cells))
for i, raw := range n.Notebook.Cells {
cells, err := d.ExtractCells(data)
if err != nil {
return fmt.Errorf("%s: extract cells: %w", ver, err)
}

n.cells = make([]schema.Cell, len(cells))
for i, raw := range cells {
c := cell{meta: meta, decoder: d}
if err := json.Unmarshal(raw, &c); err != nil {
return fmt.Errorf("%s: %w", ver, err)
Expand Down Expand Up @@ -78,7 +83,16 @@ func (c *cell) UnmarshalJSON(data []byte) error {
// Decoder implementations are version-aware and decode cell contents and metadata
// based on the respective JSON schema definition.
type Decoder interface {
// ExtractCells accesses the array of notebook cells.
//
// Prior to v4.0 cells were not a part of the top level structure,
// and were contained in "worksheets" instead.
ExtractCells(data []byte) ([]json.RawMessage, error)

// DecodeMeta decodes version-specific metadata.
DecodeMeta(data []byte) (schema.NotebookMetadata, error)

// DecodeCell decodes raw cell data to a version-specific implementation.
DecodeCell(v map[string]interface{}, data []byte, meta schema.NotebookMetadata) (schema.Cell, error)
}

Expand Down
Loading

0 comments on commit 230f0ff

Please sign in to comment.