Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add save as menu option#3289

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
takluyver merged 7 commits intojupyter:masterfromMadhu94:add-save-as-menu-option
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes fromall 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
4 changes: 4 additions & 0 deletionsnotebook/static/notebook/js/menubar.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,6 +170,10 @@ define([
that.notebook.copy_notebook();
return false;
});
this.element.find('#save_notebook_as').click(function() {
that.notebook.save_notebook_as();
return false;
});
this.element.find('#download_ipynb').click(function () {
var base_url = that.notebook.base_url;
var notebook_path = utils.encode_uri_components(that.notebook.notebook_path);
Expand Down
93 changes: 91 additions & 2 deletionsnotebook/static/notebook/js/notebook.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2846,7 +2846,96 @@ define([
this._checkpoint_after_save = false;
}
};


Notebook.prototype.save_notebook_as = function() {
var that = this;
var current_dir = $('body').attr('data-notebook-path').split('/').slice(0, -1).join("/");
current_dir = current_dir? current_dir + "/": "";
var dialog_body = $('<div/>').append(
$('<p/>').addClass('save-message')
.text(i18n.msg._('Enter a notebook path relative to notebook dir'))
).append(
$('<br/>')
).append(
$('<input/>').attr('type','text').attr('size','25')
.attr('data-testid', 'save-as')
.addClass('form-control')
);

var d = dialog.modal({
title: 'Save As',
body: dialog_body,
keyboard_manager: this.keyboard_manager,
notebook: this,
buttons: {
Cancel: {},
Save: {
class: 'btn-primary',
click: function() {
var nb_path = d.find('input').val();
var nb_name = nb_path.split('/').slice(-1).pop();
// If notebook name does not contain extension '.ipynb' add it
var ext = utils.splitext(nb_name)[1];
if (ext === '') {
nb_name = nb_name + '.ipynb';
nb_path = nb_path + '.ipynb';
}
var save_thunk = function() {
var model = {
'type': 'notebook',
'content': that.toJSON(),
'name': nb_name
};
return that.contents.save(nb_path, model)
.then(function(data) {
d.modal('hide');
that.notebook_name = data.name;
that.notebook_path = data.path;
that.session.rename_notebook(data.path);
that.events.trigger('notebook_renamed.Notebook', data);
}, function(error) {
var msg = i18n.msg._(error.message || 'Unknown error saving notebook');
$(".save-message").html(
$("<span>")
.attr("style", "color:red;")
.text(msg)
);
});
};
that.contents.get(nb_path, {type: 'notebook', content: false}).then(function(data) {
var warning_body = $('<div/>').append(
$("<p/>").text(i18n.msg._('Notebook with that name exists.')));
dialog.modal({
title: 'Save As',
body: warning_body,
buttons: {Cancel: {},
Overwrite: {
class: 'btn-warning',
click: function() {
return save_thunk();
}
}
}
});
}, function(err) {
return save_thunk();
});
return false;
}
},
},
open : function () {
d.find('input[type="text"]').keydown(function (event) {
if (event.which === keyboard.keycodes.enter) {
d.find('.btn-primary').first().click();
return false;
}
});
d.find('input[type="text"]').val(current_dir).focus();
}
});
};

/**
* Update the autosave interval based on the duration of the last save.
*
Expand DownExpand Up@@ -3387,4 +3476,4 @@ define([
};

return {Notebook: Notebook};
});
});
3 changes: 3 additions & 0 deletionsnotebook/templates/notebook.html
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -89,6 +89,9 @@
<li id="copy_notebook"
title="{% trans %}Open a copy of this notebook's contents and start a new kernel{% endtrans %}">
<a href="#">{% trans %}Make a Copy...{% endtrans %}</a></li>
<li id="save_notebook_as"
title="{% trans %}Save a copy of the notebook's contents and start a new kernel{% endtrans %}">
<a href="#">{% trans %}Save as...{% endtrans %}</a></li>
<li id="rename_notebook"><a href="#">{% trans %}Rename...{% endtrans %}</a></li>
<li id="save_checkpoint"><a href="#">{% trans %}Save and Checkpoint{% endtrans %}</a></li>
<!-- <hr/> -->
Expand Down
40 changes: 40 additions & 0 deletionsnotebook/tests/selenium/test_save_as_notebook.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
from notebook.tests.selenium.utils import wait_for_selector
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait

def wait_for_rename(browser, nbname, timeout=10):
wait = WebDriverWait(browser, timeout)
def notebook_renamed(browser):
elem = browser.find_element_by_id('notebook_name')
current_name = browser.execute_script('return arguments[0].innerText', elem)
return current_name == nbname
return wait.until(notebook_renamed)

def save_as(nb):
JS = 'Jupyter.notebook.save_notebook_as()'
return nb.browser.execute_script(JS)

def get_notebook_name(nb):
JS = 'return Jupyter.notebook.notebook_name'
return nb.browser.execute_script(JS)

def set_notebook_name(nb, name):
JS = 'Jupyter.notebook.rename("{}")'.format(name)
nb.browser.execute_script(JS)

def test_save_notebook_as(notebook):
# Set a name for comparison later
set_notebook_name(notebook, name="nb1.ipynb")
wait_for_rename(notebook.browser, "nb1")
assert get_notebook_name(notebook) == "nb1.ipynb"
# Wait for Save As modal, save
save_as(notebook)
wait_for_selector(notebook.browser, '.save-message')
inp = notebook.browser.find_element_by_xpath('//input[@data-testid="save-as"]')
inp.send_keys('new_notebook.ipynb')
inp.send_keys(Keys.RETURN)
wait_for_rename(notebook.browser, "new_notebook")
# Test that the name changed
assert get_notebook_name(notebook) == "new_notebook.ipynb"
# Test that address bar was updated (TODO: get the base url)
assert "new_notebook.ipynb" in notebook.browser.current_url

[8]ページ先頭

©2009-2025 Movatter.jp