diff --git a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/isValidNBJson..st b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/isValidNBJson..st new file mode 100644 index 000000000..a902079f7 --- /dev/null +++ b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/isValidNBJson..st @@ -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" \ No newline at end of file diff --git a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/offerToValidateOutput..st b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/offerToValidateOutput..st new file mode 100644 index 000000000..2db6710ca --- /dev/null +++ b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/offerToValidateOutput..st @@ -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!' ] ] \ No newline at end of file diff --git a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/instance/save.st b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/instance/save.st index dc7b39dc7..634f9ae18 100644 --- a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/instance/save.st +++ b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/instance/save.st @@ -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 \ No newline at end of file + json jsonWriteOn: stream \ No newline at end of file diff --git a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/methodProperties.json b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/methodProperties.json index d31c87eac..773cefd01 100644 --- a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/methodProperties.json +++ b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/methodProperties.json @@ -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", @@ -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" } } diff --git a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/properties.json b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/properties.json index da800810f..0b35f873e 100644 --- a/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/properties.json +++ b/src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/properties.json @@ -1,7 +1,7 @@ { "category" : "TruffleSqueak-Tools-Notebook-Core", "classinstvars" : [ - ], + "validateNBJson" ], "classvars" : [ ], "commentStamp" : "",