Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mira model template preview for regnet/stockflow framework types #2802

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@change="if ($event.value) view = $event.value;"
:options="viewOptions"
/>
<div class="container">
<div class="container" v-if="model && model.header.schema_name === 'petrinet'">
<tera-model-diagram
v-if="view === ModelNodeView.Diagram"
:model="model"
Expand All @@ -21,6 +21,9 @@
:is-editable="false"
/>
</div>
<div class="container">
<img :src="templatePreview" alt="" style="max-height: 180px" />
</div>
<Button label="Open" @click="emit('open-drilldown')" severity="secondary" outlined />
</template>
<template v-else>
Expand Down Expand Up @@ -51,6 +54,7 @@ import TeraOperatorTitle from '@/components/operator/tera-operator-title.vue';
import { useProjects } from '@/composables/project';
import TeraOperatorPlaceholder from '@/components/operator/tera-operator-placeholder.vue';
import Button from 'primevue/button';
import { KernelSessionManager } from '@/services/jupyter';
import { ModelOperationState } from './model-operation';

const props = defineProps<{
Expand All @@ -68,6 +72,7 @@ enum ModelNodeView {
const model = ref<Model | null>();
const view = ref(ModelNodeView.Diagram);
const viewOptions = ref([ModelNodeView.Diagram, ModelNodeView.Equation]);
const templatePreview = ref('');

async function getModelById(modelId: string) {
model.value = await getModel(modelId);
Expand All @@ -88,11 +93,38 @@ async function onModelChange(chosenProjectModel: ProjectAsset) {
await getModelById(chosenProjectModel.assetId);
}

// Create a preview image based on MMT
const generateTemplatePreview = async () => {
if (!model.value) return;
try {
const kernelManager = new KernelSessionManager();
await kernelManager.init('beaker_kernel', 'Beaker Kernel', {
context: 'mira_model',
language: 'python3',
context_info: {
id: model.value.id
}
});

kernelManager
.sendMessage('reset_request', {})
.register('reset_response', () => null) // noop
.register('model_preview', (data) => {
templatePreview.value = `data:image/png;base64, ${data?.content?.['image/png']}`;
kernelManager.shutdown();
});
} catch (err) {
console.error(err);
}
};

onMounted(async () => {
const state = props.node.state;
if (state.modelId) {
model.value = await getModel(state.modelId);

await generateTemplatePreview();
mwdchang marked this conversation as resolved.
Show resolved Hide resolved

if (props.node.outputs.length === 0 && model.value) {
emit('append-output', {
type: 'modelId',
Expand Down
Loading