From 1378d5cb7c0b2f74f6e0bc75d13a765d8004e5b5 Mon Sep 17 00:00:00 2001 From: Antonio Feregrino Date: Fri, 16 Aug 2024 08:10:43 +0100 Subject: [PATCH] wip --- presentpy_jupyter/handlers.py | 13 +++++--- src/index.ts | 60 +++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/presentpy_jupyter/handlers.py b/presentpy_jupyter/handlers.py index fe80883..f158b60 100644 --- a/presentpy_jupyter/handlers.py +++ b/presentpy_jupyter/handlers.py @@ -9,16 +9,21 @@ class RouteHandler(APIHandler): # patch, put, delete, options) to ensure only authorized user can request the # Jupyter server @tornado.web.authenticated - def get(self): + def post(self): + # data = json.loads(self.request.body.decode('utf-8')) + # content = data['content'] + # filename = data['filename'] + self.finish(json.dumps({ - "data": "This is /presentpy-jupyter/get-example endpoint!" + "data": "This is /presentpy-jupyter/get-example endpoint!", + "content": self.request.body.decode('utf-8'), })) - + def setup_handlers(web_app): host_pattern = ".*$" base_url = web_app.settings["base_url"] - route_pattern = url_path_join(base_url, "presentpy-jupyter", "get-example") + route_pattern = url_path_join(base_url, "presentpy-jupyter", "download") handlers = [(route_pattern, RouteHandler)] web_app.add_handlers(host_pattern, handlers) diff --git a/src/index.ts b/src/index.ts index 945df52..a6f4ca0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,15 @@ import { import { ISettingRegistry } from '@jupyterlab/settingregistry'; -import { requestAPI } from './handler'; +// import { requestAPI } from './handler'; + +// import { INotebookTracker } from '@jupyterlab/notebook'; + +// import { ToolbarButton } from '@jupyterlab/apputils'; + +// import { downloadIcon } from '@jupyterlab/ui-components'; + +import { ServerConnection } from '@jupyterlab/services'; /** * Initialization data for the presentpy_jupyter extension. @@ -29,16 +37,6 @@ const plugin: JupyterFrontEndPlugin = { }); } - requestAPI('get-example') - .then(data => { - console.log(data); - }) - .catch(reason => { - console.error( - `The presentpy_jupyter server extension appears to be missing.\n${reason}` - ); - }); - const { commands } = app; @@ -48,20 +46,40 @@ const plugin: JupyterFrontEndPlugin = { commands.addCommand(command, { label: 'ODP', caption: 'Export to ODP', - execute: (args: any) => { + execute: async (args: any) => { + // const notebook = notebooks.currentWidget; + // const notebookModel = notebook.model; + // const content = notebookModel.toString(); const orig = args['origin']; console.log(`presentpy_jupyter:command has been called from ${orig}.`); if (orig !== 'init') { + const settings = ServerConnection.makeSettings(); + const requestUrl = `${settings.baseUrl}presentpy-jupyter/download`; + console.log(requestUrl); + try { + const response = await ServerConnection.makeRequest(requestUrl, { + method: 'POST', + body: JSON.stringify({ "content": "Hello, world!" }), + }, settings); + + if (response.status !== 200) { + const data = await response.json(); + throw new Error(data.message || 'Unknown error'); + } - requestAPI('get-example') - .then(data => { - console.log(data); - }) - .catch(reason => { - console.error( - `The presentpy_jupyter server extension appears to be missing.\n${reason}` - ); - }); + console.log(response); + // const blob = await response.blob(); + // const url = URL.createObjectURL(blob); + // const a = document.createElement('a'); + // a.href = url; + // a.download = notebook.title.label || 'notebook.ipynb'; + // document.body.appendChild(a); + // a.click(); + // document.body.removeChild(a); + // URL.revokeObjectURL(url); + } catch (error) { + console.error('Failed to download notebook:', error); + } } }