Skip to content

Add support to load notebooks from URL or from local file #12

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

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions _layouts/iocamljs.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
<ul class="dropdown-menu">
<li id="download_ipynb"><a href="#">Download Notebook (.ipynb)</a></li>
<li id="open_notebook_url"><a href="#">Open from URL</a></li>
<li id="open_notebook_file"><a href="#">Open from file</a></li>
</ul>
</li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Edit</a>
Expand Down
5 changes: 4 additions & 1 deletion static/notebook/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ function (marked) {

$([IPython.events]).on('notebook_loaded.Notebook', first_load);
$([IPython.events]).trigger('app_initialized.NotebookApp');
IPython.notebook.load_notebook($('body').data('notebookId'));
var notebook_id = $('body').data('notebookId');
IPython.notebook.notebook_id = notebook_id
var url = IPython.notebook.baseProjectUrl() + 'notebooks/' + notebook_id
IPython.notebook.load_notebook(url);

if (marked) {
marked.setOptions({
Expand Down
30 changes: 28 additions & 2 deletions static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,34 @@ var IPython = (function (IPython) {
this.element.find('#new_notebook').click(function () {
window.open(that.baseProjectUrl()+'new');
});
this.element.find('#open_notebook').click(function () {
window.open(that.baseProjectUrl());
this.element.find('#open_notebook_url').click(function () {
var notebook_id = IPython.notebook.get_notebook_id();
var url = that.baseProjectUrl() + 'notebooks/' +
notebook_id
url = prompt('Enter URL:', url);
IPython.notebook.load_notebook(url)
});
this.element.find('#open_notebook_file').click(function () {
var uploadForm = document.createElement('form');
var fileInput = uploadForm.appendChild(document.createElement('input'));
function readFile(e){
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
data = JSON.parse(contents);
IPython.notebook.load_notebook_success(data, 42, 42);
};
reader.readAsText(file);
}
fileInput.type = 'file';
fileInput.name = 'notebook_file';
fileInput.multiple = false;
fileInput.addEventListener('change', readFile, false);
fileInput.click();
});
this.element.find('#rename_notebook').click(function () {
IPython.save_widget.rename_notebook();
Expand Down
4 changes: 1 addition & 3 deletions static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -1711,9 +1711,8 @@ var IPython = (function (IPython) {
* @method load_notebook
* @param {String} notebook_id A notebook to load
*/
Notebook.prototype.load_notebook = function (notebook_id) {
Notebook.prototype.load_notebook = function (url) {
var that = this;
this.notebook_id = notebook_id;
// We do the call with settings so we can set cache to false.
var settings = {
processData : false,
Expand All @@ -1724,7 +1723,6 @@ var IPython = (function (IPython) {
error : $.proxy(this.load_notebook_error,this),
};
$([IPython.events]).trigger('notebook_loading.Notebook');
var url = this.baseProjectUrl() + 'notebooks/' + this.notebook_id;
$.ajax(url, settings);
};

Expand Down