Skip to content

Commit

Permalink
refactor: directly render vue tree if root view is a ipyvue widget
Browse files Browse the repository at this point in the history
Do not include a div in between temporarily. Since the widget view
is created before we try to render the the upyter-widget-mount-point
we do not need to get it asynchrounously via promises.
  • Loading branch information
maartenbreddels committed Jan 2, 2024
1 parent f887272 commit b763688
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions solara/server/static/main-vuetify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

const widgetViews = {};
var jupyterWidgetMountPoint = {
data() {
return {
Expand All @@ -8,25 +9,22 @@ var jupyterWidgetMountPoint = {
},
props: ['mount-id'],
created() {
requestWidget(this.mountId);
},
mounted() {
const vue3 = Vue.version.startsWith('3');
requestWidget(this.mountId)
.then(widgetView => {
if (['VuetifyView', 'VuetifyTemplateView'].includes(widgetView.model.get('_view_name'))) {
this.renderFn = createElement => widgetView.vueRender(createElement);
} else {
while (this.$el.firstChild) {
this.$el.removeChild(this.$el.firstChild);
}

requirejs(['@jupyter-widgets/base'], widgets =>
widgets.JupyterPhosphorWidget.attach(widgetView.pWidget, this.$el)
);
const maybeCreateRenderFn = (widgetView) => {
// in solara, this is always the case, but lets keep this code so we could potentially
// support any ipywidget as a root view
if (['VuetifyView', 'VuetifyTemplateView'].includes(widgetView.model.get('_view_name'))) {
this.renderFn = createElement => widgetView.vueRender(createElement);
} else {
while (this.$el.firstChild) {
this.$el.removeChild(this.$el.firstChild);
}

requirejs(['@jupyter-widgets/base'], widgets =>
widgets.JupyterPhosphorWidget.attach(widgetView.pWidget, this.$el)
);
}
);
}
maybeCreateRenderFn(widgetViews[this.mountId]);
},
render(createElement) {
// in vue3 we have Vue.h, otherwise fall back to createElement (vue2)
Expand All @@ -43,24 +41,6 @@ var jupyterWidgetMountPoint = {
}
};

const widgetResolveFns = {};
const widgetPromises = {};

function provideWidget(mountId, widgetView) {
if (widgetResolveFns[mountId]) {
widgetResolveFns[mountId](widgetView);
} else {
widgetPromises[mountId] = Promise.resolve(widgetView);
}
}

function requestWidget(mountId) {
if (!widgetPromises[mountId]) {
widgetPromises[mountId] = new Promise(resolve => widgetResolveFns[mountId] = resolve);
}
return widgetPromises[mountId];
}

function injectDebugMessageInterceptor(kernel) {
const _original_handle_message = kernel._handleMessage.bind(kernel)
kernel._handleMessage = ((msg) => {
Expand Down Expand Up @@ -242,7 +222,7 @@ async function solaraMount(widgetManager, mountId, modelId) {
const model = await modelPromise;
if (model.model_id == modelId) {
const view = await widgetManager.create_view(model);
provideWidget(mountId, view);
widgetViews[mountId] = view;
}
}));
app.$data.loadingPercentage = 0;
Expand Down

0 comments on commit b763688

Please sign in to comment.