From c1d6fd5c4cf7829c85d70cf6b454f52f47920043 Mon Sep 17 00:00:00 2001 From: Rafael Cunha de Almeida Date: Wed, 3 Aug 2022 17:56:27 +0100 Subject: [PATCH 1/2] Support notebook upload from the UI --- notebooker/web/routes/core.py | 19 +++++++++++++- notebooker/web/static/notebooker/header.js | 30 ++++++++++++++++++++++ notebooker/web/templates/header.html | 20 ++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/notebooker/web/routes/core.py b/notebooker/web/routes/core.py index a38c1ac0..ea8528d3 100644 --- a/notebooker/web/routes/core.py +++ b/notebooker/web/routes/core.py @@ -1,9 +1,10 @@ +from pathlib import Path from flask import Blueprint, jsonify, request import notebooker.version from notebooker.constants import DEFAULT_RESULT_LIMIT from notebooker.utils.results import get_all_available_results_json, get_count_and_latest_time_per_report -from notebooker.web.utils import get_serializer, get_all_possible_templates, all_templates_flattened +from notebooker.web.utils import _get_python_template_dir, get_serializer, get_all_possible_templates, all_templates_flattened core_bp = Blueprint("core_bp", __name__) @@ -76,3 +77,19 @@ def get_version_no(): :returns: A JSON mapping from "version" to the string repr of the version number. """ return jsonify({"version": notebooker.version.__version__}) + + +@core_bp.route("/core/notebook/upload", methods=["POST"]) +def upload_notebook(): + """ + Stores a notebook in git + """ + templates = Path(_get_python_template_dir()) + web = templates / "web" + web.mkdir(exist_ok=True) + notebook_name = request.values.get("name") + if not notebook_name or not notebook_name.endswith(".ipynb"): + return jsonify({"status": "Invalid notebook name"}), 400 + with open(web / request.values.get("name"), "w") as fp: + fp.write(request.values.get("notebook", "")) + return jsonify({"status": "Notebook uploaded"}) diff --git a/notebooker/web/static/notebooker/header.js b/notebooker/web/static/notebooker/header.js index 1a667899..64a54647 100644 --- a/notebooker/web/static/notebooker/header.js +++ b/notebooker/web/static/notebooker/header.js @@ -1,4 +1,34 @@ $(document).ready(() => { + $('#showUploadNotebookButton').click(() => + $('#uploadNotebookModal').modal({ + closable: true, + onHidden: function() { + $('#notebook').val(''); + }, + onApprove: function() { + let notebook = $('#notebook').prop('files')[0]; + let reader = new FileReader(); + reader.addEventListener('load', (e) => { + $.ajax({ + url: '/core/notebook/upload', + cache: false, + type: 'POST', + data: { + name: notebook.name, + notebook: e.target.result + }, + success: () => { + $('body').toast({class: 'info', message: 'Upload successful! Refresh the page in order to see it.'}); + }, + error: (result) => { + $('body').toast({class: 'error', message: `Error uploading notebook: ${result.responseJSON.status} (${result.status})`}); + }, + }); + }); + reader.readAsText(notebook); + } + }).modal('show') + ); $.ajax({ url: '/core/version', success: (result) => { diff --git a/notebooker/web/templates/header.html b/notebooker/web/templates/header.html index 218716b7..79743966 100644 --- a/notebooker/web/templates/header.html +++ b/notebooker/web/templates/header.html @@ -88,11 +88,29 @@

Execute a notebook:

{% endif %} {% endfor %} - + Add a new notebook + + From 257a4151485e5bc29c081658b30ad0614e0d0a24 Mon Sep 17 00:00:00 2001 From: Rafael Cunha de Almeida Date: Wed, 3 Aug 2022 18:00:50 +0100 Subject: [PATCH 2/2] Fix indentation --- notebooker/web/templates/header.html | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/notebooker/web/templates/header.html b/notebooker/web/templates/header.html index 79743966..d708115d 100644 --- a/notebooker/web/templates/header.html +++ b/notebooker/web/templates/header.html @@ -96,11 +96,12 @@

Execute a notebook: