Skip to content

Commit 427b97f

Browse files
committed
Working with jupytercad app
1 parent 1de03b0 commit 427b97f

File tree

5 files changed

+198
-12
lines changed

5 files changed

+198
-12
lines changed

python/jupytercad_app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@jupyterlab/filebrowser": "^4.0.0",
7676
"@jupyterlab/filebrowser-extension": "^4.0.0",
7777
"@jupyterlab/fileeditor": "^4.2.0",
78+
"@jupyterlab/json-extension": "^4.0.0",
7879
"@jupyterlab/launcher": "^4.0.0",
7980
"@jupyterlab/launcher-extension": "^4.0.0",
8081
"@jupyterlab/logconsole": "^4.0.0",

python/jupytercad_app/src/app/app.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2+
createRendermimePlugins,
23
JupyterFrontEnd,
34
JupyterFrontEndPlugin
45
} from '@jupyterlab/application';
56

67
import { PageConfig } from '@jupyterlab/coreutils';
78

89
import { IShell, Shell } from './shell';
10+
import { IRenderMime } from '@jupyterlab/rendermime';
911

1012
/**
1113
* App is the main application class. It is instantiated once and shared.
@@ -16,10 +18,16 @@ export class App extends JupyterFrontEnd<IShell> {
1618
*
1719
* @param options The instantiation options for an application.
1820
*/
19-
constructor(options: App.IOptions = { shell: new Shell() }) {
21+
constructor(options: App.IOptions) {
2022
super({
21-
shell: options.shell
23+
...options,
24+
shell: options.shell ?? new Shell()
2225
});
26+
if (options.mimeExtensions) {
27+
for (const plugin of createRendermimePlugins(options.mimeExtensions)) {
28+
this.registerPlugin(plugin);
29+
}
30+
}
2331
}
2432

2533
/**
@@ -112,7 +120,21 @@ export namespace App {
112120
/**
113121
* The instantiation options for an App application.
114122
*/
115-
export type IOptions = JupyterFrontEnd.IOptions<IShell>;
123+
export interface IOptions
124+
extends JupyterFrontEnd.IOptions<IShell>,
125+
Partial<IInfo> {
126+
paths?: Partial<JupyterFrontEnd.IPaths>;
127+
}
128+
129+
/**
130+
* The information about a application.
131+
*/
132+
export interface IInfo {
133+
/**
134+
* The mime renderer extensions.
135+
*/
136+
readonly mimeExtensions: IRenderMime.IExtensionModule[];
137+
}
116138

117139
/**
118140
* The interface for a module that exports a plugin or plugins as

python/jupytercad_app/src/main.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '@jupyterlab/console/style/index.js';
1515
import '@jupyterlab/completer/style/index.js';
1616
import '../style/index.css';
1717
import './sharedscope';
18+
import { Shell } from './app/shell';
1819

1920
function loadScript(url: string) {
2021
return new Promise((resolve, reject) => {
@@ -61,7 +62,6 @@ async function createModule(scope: string, module: string) {
6162
async function main(): Promise<void> {
6263
// Inject some packages in the shared scope
6364

64-
const app = new App();
6565
// populate the list of disabled extensions
6666
const disabled: any[] = [
6767
'jupytercad:serverInfoPlugin',
@@ -142,8 +142,10 @@ async function main(): Promise<void> {
142142
require('./app/plugins/browser'),
143143
require('./app/plugins/launcher')
144144
];
145+
const mimeExtensions = [require('@jupyterlab/json-extension')];
145146

146147
const federatedExtensionPromises: Promise<any>[] = [];
148+
const federatedMimeExtensionPromises: Promise<any>[] = [];
147149
const federatedStylePromises: Promise<any>[] = [];
148150

149151
const extension_data = JSON.parse(
@@ -176,8 +178,9 @@ async function main(): Promise<void> {
176178
federatedExtensionPromises.push(createModule(data.name, data.extension));
177179
}
178180
if (data.mimeExtension) {
179-
// TODO Do we need mime extensions?
180-
return;
181+
federatedMimeExtensionPromises.push(
182+
createModule(data.name, data.mimeExtension)
183+
);
181184
}
182185
if (data.style && !PageConfig.Extension.isDisabled(data.name)) {
183186
federatedStylePromises.push(createModule(data.name, data.style));
@@ -200,13 +203,28 @@ async function main(): Promise<void> {
200203
}
201204
});
202205

206+
// Add the federated mime extensions.
207+
const federatedMimeExtensions = await Promise.allSettled(
208+
federatedMimeExtensionPromises
209+
);
210+
federatedMimeExtensions.forEach(p => {
211+
if (p.status === 'fulfilled') {
212+
for (const plugin of activePlugins(p.value)) {
213+
mimeExtensions.push(plugin);
214+
}
215+
} else {
216+
console.error(p.reason);
217+
}
218+
});
219+
203220
// Load all federated component styles and log errors for any that do not
204221
(await Promise.allSettled(federatedStylePromises))
205222
.filter(({ status }) => status === 'rejected')
206223
.forEach(e => {
207224
console.error((e as any).reason);
208225
});
209226

227+
const app = new App({ mimeExtensions, shell: new Shell() });
210228
app.registerPluginModules(mods);
211229

212230
await app.start();

python/jupytercad_core/src/jcadplugin/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const activate = (
8181
modelName: 'jupytercad-jcadmodel',
8282
name: 'JSON Editor',
8383
primaryFileType: app.docRegistry.getFileType('json'),
84-
fileTypes: [FACTORY],
84+
fileTypes: [FACTORY]
8585
});
8686
app.docRegistry.addWidgetFactory(factory);
8787

0 commit comments

Comments
 (0)