Skip to content

Commit

Permalink
Fix Google Colab Jupyter Notebooks rendering by excluding widgets (#7271
Browse files Browse the repository at this point in the history
)

Co-authored-by: Donny Wong <donnywong@cs.toronto.edu>
  • Loading branch information
donny-wong and Donny Wong authored Oct 28, 2024
1 parent 1f0e1f8 commit 81545d1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Disable editable fields in assignment criterion when criterion is released (#7264)
- Fix Marks Spreadsheet csv bug of showing incorrect marks (#7257)
- Fix incorrect inclusion of course parameter in LtiSyncJob (#7258)
- Fix Google Colab Jupyter Notebooks rendering by excluding widgets (#7271)

### 🔧 Internal changes

Expand Down
7 changes: 7 additions & 0 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'json'

class SubmissionsController < ApplicationController
include SubmissionsHelper
include RepositoryHelper
Expand Down Expand Up @@ -948,6 +950,11 @@ def notebook_to_html(file_contents, unique_path, type)
'--template', 'markus-html-template'
]
end
file_contents = JSON.parse(file_contents)
if file_contents['metadata'].key?('widgets')
file_contents['metadata'].delete('widgets')
end
file_contents = JSON.generate(file_contents)
_stdout, stderr, status = Open3.capture3(*args, stdin_data: file_contents)
return "#{I18n.t('submissions.cannot_display')}<br/><br/>#{stderr.lines.last}" unless status.exitstatus.zero?

Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/submissions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,19 @@
it_behaves_like 'notebook content'
end

context 'a jupyter-notebook file with widgets',
skip: Rails.application.config.nbconvert_enabled ? false : 'nbconvert dependencies not installed' do
render_views
let(:filename) { 'example_widgets.ipynb' }

it 'renders without widgets' do
subject
expect(response.body).not_to include("KeyError: 'state'")
end

it_behaves_like 'notebook content'
end

context 'an rmarkdown file' do
let(:filename) { 'example.Rmd' }

Expand Down
52 changes: 52 additions & 0 deletions spec/fixtures/files/example_widgets.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
}
}
},
"cells": [
{
"cell_type": "code",
"source": [
],
"metadata": {
"id": "wekaZO6DYWz6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "990f2494-5ca0-442f-ba8d-f7a31bd463e4"
},
"execution_count": 17,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"Training: 0%| | 0/1000 [00:00<?, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "92b45d943b9e4c83b50bb2c83505e80d"
}
},
"metadata": {}
}
]
}
]
}

0 comments on commit 81545d1

Please sign in to comment.