Commit 018e507b authored by Romain Casati's avatar Romain Casati
Browse files

Support for Python module loading.

parent f42f5b42
......@@ -209,6 +209,48 @@ Un lien vers la page de Basthon avec le contenu actuel du script a été créé.
});
};
/**
* Load the content of a Python script in first cell.
*/
that.loadPythonInCell = function(file) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = async function (event) {
const new_cell = that.notebook.insert_cell_above('code', 0);
new_cell.set_text(event.target.result);
// notification seems useless here.
resolve();
};
reader.onerror = reject;
});
};
/**
* Open *.py file by asking user what to do:
* load in notebook cell or put on (emulated) local filesystem.
*/
that.openPythonFile = async function (file) {
const msg = $("<div>").html(
"Que faire de " + file.name + " ?");
that.dialog.modal({
notebook: that.notebook,
keyboard_manager: that.notebook.keyboard_manager,
title : "Que faire du fichier ?",
body : msg,
buttons : {
"Charger dans le notebook": {
"class": "btn-primary",
"click": () => { that.loadPythonInCell(file); },
},
"Installer le module": {
"class": "btn-primary",
"click": () => { that.putFSRessource(file); },
},
}
});
};
/**
* Loading file in the (emulated) local filesystem (async).
*/
......@@ -253,6 +295,8 @@ Un lien vers la page de Basthon avec le contenu actuel du script a été créé.
const ext = file.name.split('.').pop();
if(ext === 'ipynb') {
await that.openNotebook(file);
} else if(ext === 'py') {
await that.openPythonFile(file);
} else {
await that.putFSRessource(file);
}
......
......@@ -214,7 +214,7 @@ dir="ltr">
<ul class="dropdown-menu" id="menu-new-notebook-submenu"></ul>
</li>
<li id="open_notebook" role="none"
title="Ouvre un notebook ou charge un fichier">
title="Ouvre un notebook, charge un module ou un fichier">
<a href="#" role="menuitem">Ouvrir...</a></li>
<!-- <hr/> -->
<li class="divider" role="none"></li>
......
......@@ -886,8 +886,8 @@ define([
}
},
'open-notebook':{
cmd: i18n.msg._('open notebook or load file'),
help: i18n.msg._("Open a notebook or load a file"),
cmd: i18n.msg._('open notebook, module or file'),
help: i18n.msg._("Open a notebook, load a module or a file"),
help_index: 'fd',
icon: 'fa-folder-open-o',
handler : function (env, event) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment