Skip to content

Commit

Permalink
Add support for notebook file validation through nbformat
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Jun 30, 2021
1 parent f84c161 commit cfc48a3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
nbformat
isValidNBJson: aJsonString
validateNBJson ifNil: [
[ {[ validateNBJson := (Polyglot eval: #python string: 'import nbformat
def is_valid_nb_json(nb_json):
nb = nbformat.reads(nb_json, 4)
try:
nbformat.validate(nb)
return True
except nbformat.ValidationError:
return False
is_valid_nb_json') ]} do: [ :block | block value ]
displayingProgress: [ :block | 'Initializing nbformat in Python...' ]
every: 100 ] on: Error do: [ :e |
self error: 'nbformat does not seem to be installed (`pip install nbformat`).'.
^ false ] ].
^ validateNBJson value: aJsonString interopAsString "interopAsString hack: this might not be needed after 21.2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nbformat
offerToValidateOutput: aJsonString
(self confirm: 'Would you like to validate the output with nbformat?') ifTrue: [
(self isValidNBJson: aJsonString)
ifTrue: [ self inform: 'Validation with nbformat succeeded!' ]
ifFalse: [ self error: 'Validation with nbformat failed!' ] ]
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
actions
save
| stream fName |
| stream fName json |
json := self asJson.
fName := UIManager default saveFilenameRequest:'Please enter the name' translated initialAnswer: 'polyglot-notebook.ipynb'.
(Polyglot isPermitted: #python) ifTrue: [ self class offerToValidateOutput: json asJsonString ].
stream := FileDirectory default forceNewFileNamed: fName.
self asJson jsonWriteOn: stream
json jsonWriteOn: stream
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"class" : {
"for:" : "fn 3/30/2019 00:31" },
"for:" : "fn 3/30/2019 00:31",
"isValidNBJson:" : "fn 6/30/2021 12:37",
"offerToValidateOutput:" : "fn 6/30/2021 12:38" },
"instance" : {
"addCodeCell" : "fn 4/13/2019 23:24",
"addCodeCellFor:" : "fn 4/13/2019 23:24",
Expand All @@ -20,6 +22,6 @@
"resetCells" : "fn 3/27/2019 11:15",
"resetExecutionCounter" : "fn 3/25/2019 18:28",
"runAll" : "fn 3/27/2019 11:19",
"save" : "fn 3/25/2019 18:18",
"save" : "fn 6/30/2021 12:37",
"step" : "fn 3/26/2019 11:51",
"stepTime" : "fn 3/26/2019 11:53" } }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"category" : "TruffleSqueak-Tools-Notebook-Core",
"classinstvars" : [
],
"validateNBJson" ],
"classvars" : [
],
"commentStamp" : "",
Expand Down

0 comments on commit cfc48a3

Please sign in to comment.