Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Throw error on old nbformat versions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Mar 24, 2020
1 parent c19f5a6 commit 582a872
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func ConvertString(writer io.Writer, notebookString string) error {
return err
}

if notebook.NBFormat < 4 {
return fmt.Errorf(
"the provided Jupyter Notebook uses an old version of the Notebook file format (version 4 or higher is required)",
)
}

// Get format extension of Jupyter Kernel language (e.g. "py")
fileExtension := notebook.Metadata.LanguageInfo.FileExtension[1:]

Expand Down
7 changes: 4 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ type metadata struct {

// notebook represents the JSON data structure in which a Jupyter Notebook is stored.
type notebook struct {
Cells []cell `json:"cells"`
Metadata metadata `json:"metadata"`
// Omitted fields: "nbformat", "nbformat_minor"
Cells []cell `json:"cells"`
Metadata metadata `json:"metadata"`
NBFormat int `json:"nbformat"`
NBFormatMinor int `json:"nbformat_minor"`
}

// parseNotebook takes the provided Jupyter Notebook JSON string and parses it into the
Expand Down
4 changes: 3 additions & 1 deletion test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,7 @@ var testParsedNotebook = notebook{
testCodeCell,
testRawCell,
},
Metadata: testMetadata,
Metadata: testMetadata,
NBFormat: 4,
NBFormatMinor: 4,
}

0 comments on commit 582a872

Please sign in to comment.