Skip to content

Commit

Permalink
Working with jupytercad app
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Oct 23, 2024
1 parent 1de03b0 commit 64e5c86
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 11 deletions.
1 change: 1 addition & 0 deletions python/jupytercad_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@jupyterlab/filebrowser": "^4.0.0",
"@jupyterlab/filebrowser-extension": "^4.0.0",
"@jupyterlab/fileeditor": "^4.2.0",
"@jupyterlab/json-extension": "^4.0.0",
"@jupyterlab/launcher": "^4.0.0",
"@jupyterlab/launcher-extension": "^4.0.0",
"@jupyterlab/logconsole": "^4.0.0",
Expand Down
28 changes: 25 additions & 3 deletions python/jupytercad_app/src/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
createRendermimePlugins,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

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

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

/**
* App is the main application class. It is instantiated once and shared.
Expand All @@ -16,10 +18,16 @@ export class App extends JupyterFrontEnd<IShell> {
*
* @param options The instantiation options for an application.
*/
constructor(options: App.IOptions = { shell: new Shell() }) {
constructor(options: App.IOptions) {
super({
shell: options.shell
...options,
shell: options.shell ?? new Shell()
});
if (options.mimeExtensions) {
for (const plugin of createRendermimePlugins(options.mimeExtensions)) {
this.registerPlugin(plugin);
}
}
}

/**
Expand Down Expand Up @@ -112,7 +120,21 @@ export namespace App {
/**
* The instantiation options for an App application.
*/
export type IOptions = JupyterFrontEnd.IOptions<IShell>;
export interface IOptions
extends JupyterFrontEnd.IOptions<IShell>,
Partial<IInfo> {
paths?: Partial<JupyterFrontEnd.IPaths>;
}

/**
* The information about a application.
*/
export interface IInfo {
/**
* The mime renderer extensions.
*/
readonly mimeExtensions: IRenderMime.IExtensionModule[];
}

/**
* The interface for a module that exports a plugin or plugins as
Expand Down
26 changes: 23 additions & 3 deletions python/jupytercad_app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import '@jupyterlab/console/style/index.js';
import '@jupyterlab/completer/style/index.js';
import '../style/index.css';
import './sharedscope';
import { Shell } from './app/shell';

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

const app = new App();
// populate the list of disabled extensions
const disabled: any[] = [
'jupytercad:serverInfoPlugin',
Expand Down Expand Up @@ -142,8 +142,12 @@ async function main(): Promise<void> {
require('./app/plugins/browser'),
require('./app/plugins/launcher')
];
const mimeExtensions = [
require('@jupyterlab/json-extension'),
];

const federatedExtensionPromises: Promise<any>[] = [];
const federatedMimeExtensionPromises: Promise<any>[] = [];
const federatedStylePromises: Promise<any>[] = [];

const extension_data = JSON.parse(
Expand Down Expand Up @@ -176,8 +180,9 @@ async function main(): Promise<void> {
federatedExtensionPromises.push(createModule(data.name, data.extension));
}
if (data.mimeExtension) {
// TODO Do we need mime extensions?
return;
federatedMimeExtensionPromises.push(
createModule(data.name, data.mimeExtension)
);
}
if (data.style && !PageConfig.Extension.isDisabled(data.name)) {
federatedStylePromises.push(createModule(data.name, data.style));
Expand All @@ -200,13 +205,28 @@ async function main(): Promise<void> {
}
});

// Add the federated mime extensions.
const federatedMimeExtensions = await Promise.allSettled(
federatedMimeExtensionPromises
);
federatedMimeExtensions.forEach((p) => {
if (p.status === 'fulfilled') {
for (const plugin of activePlugins(p.value)) {
mimeExtensions.push(plugin);
}
} else {
console.error(p.reason);
}
});

// Load all federated component styles and log errors for any that do not
(await Promise.allSettled(federatedStylePromises))
.filter(({ status }) => status === 'rejected')
.forEach(e => {
console.error((e as any).reason);
});

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

await app.start();
Expand Down
Loading

0 comments on commit 64e5c86

Please sign in to comment.