Skip to content

Update notebook routes and services to include delete functionality #240

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

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
8 changes: 6 additions & 2 deletions server/app/routes/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ def notebook():
def get_all_notebooks():
return Notebook.get_all_notebooks()

@notebook_blueprint.route('/notebook/create', methods=['POST'])
@notebook_blueprint.route('/notebook', methods=['POST'])
def create_notebook():
data = request.get_json()
notebook_name = data.get('notebookName', None)
return Notebook.create_notebook_with_init_cells(notebook_name=notebook_name)
return Notebook.create_notebook_with_init_cells(notebook_name=notebook_name)

@notebook_blueprint.route('/notebook/<path:notebook_path>', methods=['DELETE'])
def delete_notebook(notebook_path):
return Notebook.delete_notebook_by_path(notebook_path=notebook_path)
24 changes: 24 additions & 0 deletions server/app/services/notebook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from app.models.notebook import NotebookModel
from flask import jsonify
from datetime import datetime
import requests
from database import db
Expand Down Expand Up @@ -107,4 +108,27 @@ def create_notebook_with_init_cells(notebook_name: str = None) -> None:

return response.json()

@staticmethod
def delete_notebook_by_path(notebook_path: str = None):
jupyter_server_path = os.environ.get("JUPYTER_SERVER_PATH", "http://localhost:8888")

path = f"{jupyter_server_path}/api/contents/{notebook_path}"
response = requests.delete(path)

if response.status_code != 204:
return jsonify({'message': 'Notebook not found in jupyter server'}), 404

notebook = NotebookModel.query.filter_by(path=notebook_path).first()

if notebook is None:
# If no notebook was found with the given path, return a 404 error
return jsonify({'message': 'Notebook not found in DB'}), 404

# Delete the notebook
db.session.delete(notebook)

# Commit the transaction
db.session.commit()

return jsonify({'message': 'Notebook deleted'}), 200

4 changes: 2 additions & 2 deletions webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const App = () => {

const handleNewNotebookClick = () => {
if (handleUnsavedChanges()) {
NotebookModel.createNotebook(`${baseUrl}work`).then((data) => {
NotebookModel.createNotebook(`${baseUrl}work`, '').then((data) => {
const notebookPath = `${baseUrl}${data.path}`
NotebookModel.fetchNotebook(notebookPath).then((data) => {
setNotebook(data);
Expand Down Expand Up @@ -106,7 +106,7 @@ const App = () => {

const handleDeleteNotebook = () => {
if (window.confirm('Are you sure you want to delete this notebook?')) {
NotebookModel.deleteNotebook(baseUrl + notebook.path).then((data) => {
NotebookModel.deleteNotebook(notebook.path).then((data) => {
setNotebookState({}); // Clear notebook content
console.log('Notebook deleted:', notebookState);
}).catch((error) => {
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/components/notebook/Notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ function Notebook({
}

{
contentType === ContentType.CODE ?
notebookState.name &&
(contentType === ContentType.CODE ?
<Code
notebook={notebook}
notebookState={notebookState}
Expand Down Expand Up @@ -312,7 +313,7 @@ function Notebook({
saveNotebook={handleUpdateNotebook}
deleteNotebook={handleDeleteNotebook}
/> :
<Runs />
<Runs />)
}

</Box>
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/models/DirectoryModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DirectoryModel {
static async deleteItem(basePath = '', item = '') {
const itemPath = basePath + item.path;
if (item.type === 'notebook') {
NotebookModel.deleteNotebook(itemPath);
NotebookModel.deleteNotebook(item.path);
} else {
let folderItems = [];
await DirectoryModel.getFiles(itemPath)
Expand Down
33 changes: 18 additions & 15 deletions webapp/src/models/NotebookModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class NotebookModel {
return data;
}

static async createNotebook(path = '', notebookName='') {
const response = await fetch("http://localhost:5002/notebook/create", {
static async createNotebook(path = '', notebookName = '') {
const response = await fetch("http://localhost:5002/notebook", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -146,6 +146,22 @@ class NotebookModel {
return data;
}
};

static async deleteNotebook(path = '') {
const response = await fetch("http://localhost:5002/notebook/" + path, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
}
});

if (!response.ok) {
throw new Error('Failed to create notebook');
} else {
const data = await response.json();
return data;
}
};

static async updateNotebook(path = '', content = {}) {
const updatedContent = { ...content };
Expand Down Expand Up @@ -178,19 +194,6 @@ class NotebookModel {
return data;
};

static async deleteNotebook(path = '') {
console.log("Deleting notebook at path:", path);
const response = await fetch(path, {
method: 'DELETE'
});

if (!response.ok) {
throw new Error('Failed to delete notebook');
}
const data = response.status !== 204 ? await response.json() : {};
return data;
};

static async renameNotebook(basePath = '', path = '', newName = '') {
const newPath = path.substring(0, path.lastIndexOf("/") + 1) + newName;
console.log("Renaming notebook at path:", path, "to:", newPath);
Expand Down
Loading