Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trust new notebooks and notebooks with new cells #275

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion nbclassic/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ define([

this.cell_id = utils.uuid();

// Similar to nbformat, which uses `uuid.uuid4().hex[:8]`
// as recommended by JEP 62.
// We do not check the nbformat version here, as the notebook will report
// the incorrect (4, 1) nbformat version while calling
// insert_cell_below(...) in load_notebook_success (in notebook.js).
// To not break with nbformat < 4.5, we do not include this field in toJSON.
this.id = utils.uuid().slice(0, 8);

// For JS VM engines optimization, attributes should be all set (even
// to null) in the constructor, and if possible, if different subclass
// have new attributes with same name, they should be created in the
Expand Down Expand Up @@ -494,7 +502,10 @@ define([
var data = {};
// deepcopy the metadata so copied cells don't share the same object
data.metadata = JSON.parse(JSON.stringify(this.metadata));
if (this.id !== undefined) {
// id's are only introduced in 4.5 and should only be added to 'raw', 'code' and 'markdown' cells
var nbformatSupportsIds = (Jupyter.notebook.nbformat == 4 && Jupyter.notebook.nbformat_minor >= 5) || (Jupyter.notebook.nbformat > 4);
var cellTypeCanIncludeId = this.cell_type == 'raw' || this.cell_type == 'code' || this.cell_type == 'markdown';
if (nbformatSupportsIds && cellTypeCanIncludeId) {
data.id = this.id;
}
if (data.metadata.deletable) {
Expand Down
30 changes: 30 additions & 0 deletions nbclassic/tests/end_to_end/test_trust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from .utils import EDITOR_PAGE

trust_test_code = """
import IPython.display
IPython.display.Javascript("window.javascriptExecuted = true")
"""


def test_trust(notebook_frontend):
def save_notebook():
notebook_frontend.evaluate("() => Jupyter.notebook.save_notebook()", page=EDITOR_PAGE)

# Add a cell that executes javascript
notebook_frontend.add_cell(index=0, content=trust_test_code)
notebook_frontend.execute_cell(0)
notebook_frontend.wait_for_cell_output(0)
# Make sure the JavaScript executed
assert notebook_frontend.evaluate("() => window.javascriptExecuted", page=EDITOR_PAGE) == True
# Check that we see the 'Trusted' text on the page
trusted = notebook_frontend.locate("#notification_trusted", page=EDITOR_PAGE)
assert trusted.get_inner_text() == "Trusted"
save_notebook()

# refresh the page, should be trusted
notebook_frontend.reload(EDITOR_PAGE)
notebook_frontend.wait_for_kernel_ready()
trusted = notebook_frontend.locate("#notification_trusted", page=EDITOR_PAGE)
assert trusted.get_inner_text() == "Trusted"
notebook_frontend.wait_for_cell_output(0)
assert notebook_frontend.evaluate("() => window.javascriptExecuted", page=EDITOR_PAGE) == True
Loading