jupyterpack 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +2 -0
  2. package/lib/document/commands.d.ts +8 -0
  3. package/lib/document/commands.js +76 -0
  4. package/lib/document/iframePanel.d.ts +4 -1
  5. package/lib/document/plugin.d.ts +2 -1
  6. package/lib/document/plugin.js +22 -4
  7. package/lib/document/toolbar.d.ts +9 -0
  8. package/lib/document/toolbar.js +20 -0
  9. package/lib/document/widgetFactory.d.ts +2 -1
  10. package/lib/document/widgetFactory.js +15 -6
  11. package/lib/index.d.ts +1 -1
  12. package/lib/pythonServer/dash/dashServer.d.ts +22 -0
  13. package/lib/pythonServer/dash/dashServer.js +47 -0
  14. package/lib/pythonServer/index.d.ts +5 -0
  15. package/lib/pythonServer/index.js +9 -0
  16. package/lib/pythonServer/kernelExecutor.d.ts +71 -0
  17. package/lib/pythonServer/kernelExecutor.js +140 -0
  18. package/lib/pythonServer/streamlit/streamlitServer.d.ts +14 -0
  19. package/lib/pythonServer/streamlit/streamlitServer.js +51 -0
  20. package/lib/pythonServer/tornado/tornadoServer.d.ts +34 -0
  21. package/lib/pythonServer/tornado/tornadoServer.js +68 -0
  22. package/lib/pythonWidget/comm.d.ts +11 -0
  23. package/lib/pythonWidget/comm.js +52 -0
  24. package/lib/pythonWidget/pythonWidget.d.ts +6 -0
  25. package/lib/pythonWidget/pythonWidget.js +28 -3
  26. package/lib/pythonWidget/pythonWidgetModel.d.ts +27 -6
  27. package/lib/pythonWidget/pythonWidgetModel.js +101 -15
  28. package/lib/sandpackWidget/sandpackFilesModel.d.ts +6 -2
  29. package/lib/sandpackWidget/sandpackFilesModel.js +13 -2
  30. package/lib/sandpackWidget/sandpackPanel.d.ts +10 -1
  31. package/lib/sandpackWidget/sandpackPanel.js +38 -3
  32. package/lib/swConnection/index.js +2 -2
  33. package/lib/{pythonWidget/connectionManager.d.ts → swConnection/mainConnectionManager.d.ts} +10 -0
  34. package/lib/swConnection/mainConnectionManager.js +93 -0
  35. package/lib/swConnection/sw.js +1 -1
  36. package/lib/swConnection/swCommManager.d.ts +11 -0
  37. package/lib/swConnection/{comm_manager.js → swCommManager.js} +5 -0
  38. package/lib/token.d.ts +2 -1
  39. package/lib/token.js +1 -0
  40. package/lib/tools.d.ts +9 -0
  41. package/lib/tools.js +75 -0
  42. package/lib/type.d.ts +64 -3
  43. package/lib/type.js +2 -0
  44. package/lib/websocket/websocket.d.ts +0 -0
  45. package/lib/websocket/websocket.js +152 -0
  46. package/package.json +8 -5
  47. package/src/document/commands.ts +91 -0
  48. package/src/document/iframePanel.ts +4 -1
  49. package/src/document/plugin.ts +28 -7
  50. package/src/document/toolbar.ts +39 -0
  51. package/src/document/widgetFactory.ts +17 -6
  52. package/src/global.d.ts +9 -0
  53. package/src/pythonServer/dash/dashServer.ts +66 -0
  54. package/src/pythonServer/index.ts +18 -0
  55. package/src/pythonServer/kernelExecutor.ts +243 -0
  56. package/src/pythonServer/streamlit/streamlitServer.ts +67 -0
  57. package/src/pythonServer/tornado/tornadoServer.ts +97 -0
  58. package/src/pythonWidget/comm.ts +65 -0
  59. package/src/pythonWidget/pythonWidget.ts +38 -3
  60. package/src/pythonWidget/pythonWidgetModel.ts +155 -34
  61. package/src/sandpackWidget/sandpackFilesModel.ts +17 -3
  62. package/src/sandpackWidget/sandpackPanel.ts +45 -4
  63. package/src/swConnection/index.ts +5 -2
  64. package/src/swConnection/mainConnectionManager.ts +121 -0
  65. package/src/swConnection/sw.ts +1 -1
  66. package/src/swConnection/{comm_manager.ts → swCommManager.ts} +6 -0
  67. package/src/token.ts +5 -1
  68. package/src/tools.ts +91 -0
  69. package/src/type.ts +76 -4
  70. package/src/websocket/websocket.ts +216 -0
  71. package/style/base.css +7 -0
  72. package/style/icons/autoreload.svg +16 -0
  73. package/style/icons/box.svg +12 -0
  74. package/style/icons/externallink.svg +10 -0
  75. package/lib/pythonWidget/connectionManager.js +0 -27
  76. package/lib/pythonWidget/kernelExecutor.d.ts +0 -27
  77. package/lib/pythonWidget/kernelExecutor.js +0 -104
  78. package/lib/swConnection/comm_manager.d.ts +0 -6
  79. package/lib/swConnection/connection_manager.d.ts +0 -18
  80. package/lib/swConnection/connection_manager.js +0 -27
  81. package/src/pythonWidget/connectionManager.ts +0 -43
  82. package/src/pythonWidget/kernelExecutor.ts +0 -140
  83. package/src/swConnection/connection_manager.ts +0 -43
@@ -1,27 +0,0 @@
1
- import { UUID } from '@lumino/coreutils';
2
- export class ConnectionManager {
3
- constructor(instanceId) {
4
- this.instanceId = instanceId;
5
- this._kernelExecutors = new Map();
6
- }
7
- async registerConnection(kernelExecutor) {
8
- const uuid = UUID.uuid4();
9
- this._kernelExecutors.set(uuid, kernelExecutor);
10
- return { instanceId: this.instanceId, kernelClientId: uuid };
11
- }
12
- async generateResponse(options) {
13
- const { urlPath, kernelClientId, method, params, requestBody, headers } = options;
14
- const executor = this._kernelExecutors.get(kernelClientId);
15
- if (!executor) {
16
- return null;
17
- }
18
- const response = await executor.getResponse({
19
- urlPath,
20
- method,
21
- params,
22
- headers,
23
- requestBody
24
- });
25
- return response;
26
- }
27
- }
@@ -1,43 +0,0 @@
1
- import { IConnectionManager, IDict, IKernelExecutor } from '../type';
2
- import { UUID } from '@lumino/coreutils';
3
-
4
- export class ConnectionManager implements IConnectionManager {
5
- constructor(public instanceId: string) {}
6
-
7
- async registerConnection(
8
- kernelExecutor: IKernelExecutor
9
- ): Promise<{ instanceId: string; kernelClientId: string }> {
10
- const uuid = UUID.uuid4();
11
-
12
- this._kernelExecutors.set(uuid, kernelExecutor);
13
-
14
- return { instanceId: this.instanceId, kernelClientId: uuid };
15
- }
16
-
17
- async generateResponse(options: {
18
- kernelClientId: string;
19
- urlPath: string;
20
- method: string;
21
- headers: IDict;
22
- requestBody?: ArrayBuffer;
23
- params?: string;
24
- }): Promise<IDict | null> {
25
- const { urlPath, kernelClientId, method, params, requestBody, headers } =
26
- options;
27
- const executor = this._kernelExecutors.get(kernelClientId);
28
- if (!executor) {
29
- return null;
30
- }
31
-
32
- const response = await executor.getResponse({
33
- urlPath,
34
- method,
35
- params,
36
- headers,
37
- requestBody
38
- });
39
- return response;
40
- }
41
-
42
- private _kernelExecutors = new Map<string, IKernelExecutor>();
43
- }
@@ -1,140 +0,0 @@
1
- import { PageConfig, URLExt } from '@jupyterlab/coreutils';
2
- import { KernelMessage, Session } from '@jupyterlab/services';
3
-
4
- import { arrayBufferToBase64 } from '../tools';
5
- import { IDict, IKernelExecutor } from '../type';
6
-
7
- export class KernelExecutor implements IKernelExecutor {
8
- constructor(options: KernelExecutor.IOptions) {
9
- this._sessionConnection = options.sessionConnection;
10
- }
11
-
12
- get isDisposed(): boolean {
13
- return this._isDisposed;
14
- }
15
- async init(options: {
16
- initCode?: string;
17
- instanceId: string;
18
- kernelClientId: string;
19
- }) {
20
- const { initCode, instanceId, kernelClientId } = options;
21
- const labBaseUrl = PageConfig.getOption('baseUrl');
22
- const baseURL = URLExt.join(
23
- labBaseUrl,
24
- 'extensions/jupyterpack/static',
25
- instanceId,
26
- 'dash',
27
- kernelClientId,
28
- '/'
29
- );
30
-
31
- const osCode = `
32
- import os
33
- os.environ['DASH_URL_BASE_PATHNAME'] = '${baseURL}'
34
- `;
35
- await this.executeCode({ code: osCode });
36
- if (initCode) {
37
- await this.executeCode({ code: initCode });
38
- }
39
- const serverCode = `
40
- import httpx, json, base64
41
- __monstra_transport = httpx.WSGITransport(app=app.server)
42
- def __monstra_get_response(method, url, headers, content=None, params=None):
43
- decoded_content = None
44
- if content is not None:
45
- content = base64.b64decode(content)
46
- decoded_content = content.decode()
47
- with httpx.Client(transport=__monstra_transport, base_url="http://testserver") as client:
48
- r = client.request(method, url, headers=headers, content=content, params=params)
49
- response = {
50
- "headers": dict(r.headers),
51
- "content": r.text,
52
- "status_code": r.status_code,
53
- "original_request": {"method": method, "url": url, "content": decoded_content, "params": params, "headers": headers},
54
- }
55
- json_str = json.dumps(response)
56
- b64_str = base64.b64encode(json_str.encode("utf-8")).decode("utf-8")
57
- return b64_str
58
- `;
59
- await this.executeCode({ code: serverCode });
60
- }
61
- async getResponse(options: {
62
- method: string;
63
- urlPath: string;
64
- headers: IDict;
65
- requestBody?: ArrayBuffer;
66
- params?: string;
67
- }): Promise<IDict> {
68
- const { method, urlPath, requestBody, params, headers } = options;
69
- const content = requestBody ? arrayBufferToBase64(requestBody) : undefined;
70
- const code = `__monstra_get_response("${method}", "${urlPath}", headers=${JSON.stringify(headers)} , content=${content ? `"${content}"` : 'None'}, params=${params ? `"${params}"` : 'None'})`;
71
- const raw = await this.executeCode({ code });
72
- const jsonStr = atob(raw.slice(1, -1));
73
- const obj = JSON.parse(jsonStr);
74
-
75
- return obj;
76
- }
77
- async executeCode(
78
- code: KernelMessage.IExecuteRequestMsg['content']
79
- ): Promise<string> {
80
- const kernel = this._sessionConnection?.kernel;
81
- if (!kernel) {
82
- throw new Error('Session has no kernel.');
83
- }
84
- return new Promise<string>((resolve, reject) => {
85
- const future = kernel.requestExecute(code, false, undefined);
86
- const parentMsgid = future.msg.header.msg_id;
87
- let executeResult = '';
88
- future.onIOPub = (msg: KernelMessage.IIOPubMessage): void => {
89
- const msgType = msg.header.msg_type;
90
- switch (msgType) {
91
- case 'execute_result': {
92
- const content = (msg as KernelMessage.IExecuteResultMsg).content
93
- .data['text/plain'] as string;
94
- executeResult += content;
95
- break;
96
- }
97
- case 'status': {
98
- if (
99
- (msg as KernelMessage.IStatusMsg).content.execution_state ===
100
- 'idle' &&
101
- msg.parent_header.msg_id === parentMsgid
102
- ) {
103
- resolve(executeResult);
104
- }
105
- break;
106
- }
107
- case 'stream': {
108
- const content = (msg as KernelMessage.IStreamMsg).content.text;
109
- executeResult += content;
110
- break;
111
- }
112
- case 'error': {
113
- console.error('Kernel operation failed', msg.content);
114
- reject(msg.content);
115
- break;
116
- }
117
- default:
118
- break;
119
- }
120
- };
121
- });
122
- }
123
-
124
- dispose(): void {
125
- if (this._isDisposed) {
126
- return;
127
- }
128
- this._isDisposed = true;
129
- this._sessionConnection.dispose();
130
- }
131
-
132
- private _isDisposed: boolean = false;
133
- private _sessionConnection: Session.ISessionConnection;
134
- }
135
-
136
- export namespace KernelExecutor {
137
- export interface IOptions {
138
- sessionConnection: Session.ISessionConnection;
139
- }
140
- }
@@ -1,43 +0,0 @@
1
- import { IConnectionManager, IDict, IKernelExecutor } from '../type';
2
- import { UUID } from '@lumino/coreutils';
3
-
4
- export class ConnectionManager implements IConnectionManager {
5
- constructor(public instanceId: string) {}
6
-
7
- async registerConnection(
8
- kernelExecutor: IKernelExecutor
9
- ): Promise<{ instanceId: string; kernelClientId: string }> {
10
- const uuid = UUID.uuid4();
11
-
12
- this._kernelExecutors.set(uuid, kernelExecutor);
13
-
14
- return { instanceId: this.instanceId, kernelClientId: uuid };
15
- }
16
-
17
- async generateResponse(options: {
18
- kernelClientId: string;
19
- urlPath: string;
20
- method: string;
21
- headers: IDict;
22
- requestBody?: ArrayBuffer;
23
- params?: string;
24
- }): Promise<IDict | null> {
25
- const { urlPath, kernelClientId, method, params, requestBody, headers } =
26
- options;
27
- const executor = this._kernelExecutors.get(kernelClientId);
28
- if (!executor) {
29
- return null;
30
- }
31
-
32
- const response = await executor.getResponse({
33
- urlPath,
34
- method,
35
- params,
36
- headers,
37
- requestBody
38
- });
39
- return response;
40
- }
41
-
42
- private _kernelExecutors = new Map<string, IKernelExecutor>();
43
- }