Skip to content

Commit

Permalink
Update mimerenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Aug 29, 2023
1 parent 0066a9b commit 17acf05
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
19 changes: 6 additions & 13 deletions packages/voici/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import {
OutputArea,
SimplifiedOutputArea,
} from '@jupyterlab/outputarea';
import { IRenderMime } from '@jupyterlab/rendermime';
import { IRenderMime, IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { NotebookModel } from '@jupyterlab/notebook';
import { ServiceManager } from '@jupyterlab/services';
import {
RenderMimeRegistry,
standardRendererFactories,
} from '@jupyterlab/rendermime';
import { IShell, VoilaShell } from '@voila-dashboards/voila';
import { IKernelConnection } from '@jupyterlab/services/lib/kernel/kernel';
import { IKernelSpecs } from '@jupyterlite/kernel';
Expand Down Expand Up @@ -215,10 +211,9 @@ export class VoiciApp extends JupyterFrontEnd<IShell> {
kernel.connectionStatusChanged.connect(async (_, status) => {
if (status === 'connected') {
window.update_loading_text(0, 0, 'Starting up kernel...');
const rendermime = new RenderMimeRegistry({
initialFactories: standardRendererFactories,
});

const rendermime = await this.resolveRequiredService(
IRenderMimeRegistry
);
// Create Voila widget manager
const widgetManager = new KernelWidgetManager(kernel, rendermime);
rendermime.removeMimeType(WIDGET_MIMETYPE);
Expand Down Expand Up @@ -305,11 +300,10 @@ export namespace App {

export async function executeCells(options: {
source: NotebookModel;
rendermime: RenderMimeRegistry;
rendermime: IRenderMimeRegistry;
kernel: IKernelConnection;
}): Promise<void> {
const { source, rendermime, kernel } = options;

const cellCount = source.cells.length;
for (let idx = 0; idx < cellCount; idx++) {
const cell = source.cells.get(idx);
Expand All @@ -331,11 +325,10 @@ export namespace App {
rendermime,
});
}
console.log('executing', cell.sharedModel.getSource());

area.future = kernel.requestExecute({
code: cell.sharedModel.getSource(),
});

await area.future.done;
const element = document.querySelector(`[cell-index="${idx + 1}"]`);
if (element && PageConfig.getOption('include_output')) {
Expand Down
7 changes: 7 additions & 0 deletions packages/voici/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const disabled = [
async function main() {
const mods = [
// @jupyterlab plugins
require('@jupyterlab/codemirror-extension').default.filter(
(p: any) => p.id === '@jupyterlab/codemirror-extension:languages'
),
require('@jupyterlab/markedparser-extension'),
require('@jupyterlab/markdownviewer-extension'),
require('@jupyterlab/mathjax2-extension'),
require('@jupyterlab/rendermime-extension'),
Expand All @@ -46,6 +50,8 @@ async function main() {
const mimeExtensions = [
require('@jupyterlite/iframe-extension'),
require('@jupyterlab/json-extension'),
require('@jupyterlab/javascript-extension'),
require('@jupyterlab/vega5-extension'),
];

const extensionData = JSON.parse(
Expand Down Expand Up @@ -165,6 +171,7 @@ async function main() {
);

const serviceManager = jupyterLiteServer.serviceManager;

const app = new VoiciApp({
serviceManager: serviceManager,
kernelspecs,
Expand Down
6 changes: 6 additions & 0 deletions packages/voici/src/plugins/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const treeWidgetPlugin: JupyterFrontEndPlugin<void> = {
const fbModel = new FilterFileBrowserModel({
manager: docManager,
refreshInterval: 2147483646,
filter: (value) => {
if (value.type === 'notebook' || value.type === 'directory') {
return {};
}
return null;
},
});
const urlFactory = (path: string) => {
const dir = PathExt.dirname(path);
Expand Down
9 changes: 4 additions & 5 deletions voici/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from voila.utils import include_lab_theme
from jupyter_server.services.contents.largefilemanager import LargeFileManager

from nbconvert.exporters import TemplateExporter
from nbconvert.exporters.templateexporter import TemplateExporter
from nbconvert.filters.highlight import Highlight2HTML
from nbconvert.preprocessors import ClearOutputPreprocessor
from nbconvert.preprocessors.clearoutput import ClearOutputPreprocessor

from traitlets import default

Expand All @@ -27,8 +27,8 @@ class VoiciExporter(VoilaExporter):
def __init__(self, *args, **kwargs):
kwargs.setdefault("contents_manager", LargeFileManager())

self.voici_configuration = kwargs.get("voici_config")
self.page_config = kwargs.get("page_config", {})
self.voici_configuration = kwargs.pop("voici_config")
self.page_config = kwargs.pop("page_config", {})
self.theme = self.voici_configuration.theme
self.template_name = self.voici_configuration.template

Expand Down Expand Up @@ -87,7 +87,6 @@ def notebook_execute(nb, kernel_id):
# TODO: investigate whether there is something to do in Voila to avoid this
base_url = page_config["baseUrl"]
resources["base_url"] = base_url

html = []
for html_snippet in self.template.generate(
nb=nb_copy,
Expand Down
14 changes: 11 additions & 3 deletions voici/tree_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ def patch_page_config(
else:
themeName = config.theme
page_config_copy["jpThemeName"] = themeName

page_config_copy[
"fullMathjaxUrl"
] = f'{page_config_copy["baseUrl"]}{page_config_copy["fullMathjaxUrl"]}'
return page_config_copy


def resources_include_css(env, name):
code = """<style type="text/css">\n%s</style>""" % (env.loader.get_source(env, name)[0])
code = """<style type="text/css">\n%s</style>""" % (
env.loader.get_source(env, name)[0]
)
return markupsafe.Markup(code)


class VoiciTreeExporter(HTMLExporter):
def __init__(
self,
Expand Down Expand Up @@ -140,7 +148,7 @@ def render_tree(page_config) -> StringIO:
base_url=page_config["baseUrl"],
include_css=partial(resources_include_css, self.jinja2_env),
include_lab_theme=partial(include_lab_theme, None),
theme=self.validate_theme(self.theme, False)
theme=self.validate_theme(self.theme, False),
)
)

Expand Down Expand Up @@ -225,4 +233,4 @@ def validate_theme(self, theme: str, classic_tree: bool) -> str:
return "dark"
if theme == "JupyterLab Light":
return "light"
return theme
return theme

0 comments on commit 17acf05

Please sign in to comment.