-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for notebook file validation through nbformat
- Loading branch information
Showing
5 changed files
with
32 additions
and
5 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...ffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/isValidNBJson..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
6 changes: 6 additions & 0 deletions
6
...ak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/class/offerToValidateOutput..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' ] ] |
6 changes: 4 additions & 2 deletions
6
src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/instance/save.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/TruffleSqueak-Tools-Notebook-Core.package/PNBCodeCellContainer.class/properties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters