Skip to content

Commit

Permalink
Upload notebook form
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <steve.cassidy@mq.edu.au>
  • Loading branch information
stevecassidy committed Sep 22, 2023
1 parent 33f43b2 commit 7e2bcd7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ api.post('/notebooks/', requireAuthenticationAPI, async (req, res) => {
try {
const projectID = await createNotebook(projectName, uiSpec, metadata);
res.json({notebook: projectID});
} catch {
} catch (err) {
res.json({error: 'there was an error creating the notebook'});
console.log('Error creating notebook', err);
res.status(500).end();
}
} else {
Expand Down
124 changes: 106 additions & 18 deletions views/notebooks.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,57 @@
</ol>
</nav>

<h1>Notebooks</h1>

<table class="table">
{{#each notebooks}}
<tr>
<td>
<a href="/notebooks/{{this.non_unique_project_id}}">{{this.name}}</a>
</td>
{{#if ../developer}}
<td>
<button class="btn btn-danger btn-sm" onclick="deleteNotebook('{{this.non_unique_project_id}}')">Delete</button>
</td>
{{/if}}
</tr>
{{/each}}
</table>


<div class="accordion" id="uploadFormContainer">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" data-bs-target="#collapseOne"
aria-expanded="false" aria-controls="collapseOne">
Upload a Notebook
</button>
</h2>

<div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#uploadFormContainer">
<div class="accordion-body">
<form id="upload-notebook-form">
<div class="mb-3">
<label for="notebookName">Notebook Name</label>
<input id="notebookName" name="name" class="form-control" placeholder="Notebook Name">
<div class="form-text">Optional, if not provided the name property from the file will be used.</div>
</div>

<div class="mb-3">
<input type="file" class="form-control" name="notebook" id="notebook">
<div class="form-text">Select a notebook file in JSON format.</div>
</div>

<div class="mb-3">
<input type="submit" class="form-control" value="Upload Notebook">
</div>
</form>
</div>
</div>
</div>
</div>


<script>
const deleteNotebook = (project_id) => {
fetch(`/api/notebooks/${project_id}/delete`, {
Expand All @@ -15,23 +66,60 @@
window.location.reload();
});
}
</script>
<h1>Notebooks</h1>
<table class="table">
{{#each notebooks}}
<tr>
<td>
<a href="/notebooks/{{this.non_unique_project_id}}">{{this.name}}</a>
</td>
{{#if ../developer}}
<td>
<button class="btn btn-danger btn-sm" onclick="deleteNotebook('{{this.non_unique_project_id}}')">Delete</button>
</td>
{{/if}}
</tr>
{{/each}}
</table>

</div>
function uploadNotebookHandler(event) {
event.preventDefault();
const url = '/api/notebooks/';
const form = event.target;
const name = form.querySelector('[name="name"]').value;
const fileInput = form.querySelector('[name="notebook"]');
if (fileInput) {
// parse the contents of the uploaded file as JSON
const reader = new FileReader();
const filename = fileInput.files[0];
reader.readAsText(filename);
reader.onload = (evt) => {
try {
const data = JSON.parse(evt.target.result);
// a little bit of validation
if (!data.metadata || !data['ui-specification']) {
alert('Invalid notebook file');
return;
}
if (!data.metadata.name) {
alert('Invalid notebook file');
return;
}
const body = {
metadata: data.metadata,
'ui-specification': data['ui-specification'],
name: name || data.metadata.name,
}
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
.then(response => response.json())
.then(data => {
if (data.notebook) {
window.location.reload();
} else {
alert('Error uploading notebook');
}
});
} catch (e) {
alert('Invalid notebook file');
return;
}
}
}
}
document.getElementById('upload-notebook-form').onsubmit = uploadNotebookHandler;
</script>

0 comments on commit 7e2bcd7

Please sign in to comment.