Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fferegrino committed Aug 16, 2024
1 parent a828832 commit 1378d5c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
13 changes: 9 additions & 4 deletions presentpy_jupyter/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
60 changes: 39 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,16 +37,6 @@ const plugin: JupyterFrontEndPlugin<void> = {
});
}

requestAPI<any>('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;

Expand All @@ -48,20 +46,40 @@ const plugin: JupyterFrontEndPlugin<void> = {
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<any>('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);
}

}
}
Expand Down

0 comments on commit 1378d5c

Please sign in to comment.