-
Notifications
You must be signed in to change notification settings - Fork 1
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
Preview for Mira context #114
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||
import base64 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
from mira.metamodel import TemplateModel | ||||||||||||||||||||||||||||
from mira.modeling.viz import GraphicalModel | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def __visualize_model(name: str, model: TemplateModel) -> str: | ||||||||||||||||||||||||||||
graphical_model = GraphicalModel.from_template_model(model) | ||||||||||||||||||||||||||||
filename = f"_preview_{name}.png" | ||||||||||||||||||||||||||||
graphical_model.write(filename, format="png") | ||||||||||||||||||||||||||||
with open(filename, "rb") as f: | ||||||||||||||||||||||||||||
data = f.read() | ||||||||||||||||||||||||||||
enc = base64.b64encode(bytes(data)) | ||||||||||||||||||||||||||||
return enc.decode("utf-8") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
__state = locals() | ||||||||||||||||||||||||||||
__models = {} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for model in {{model_vars}}: | ||||||||||||||||||||||||||||
model: str | ||||||||||||||||||||||||||||
__models[model] = {"text/plain": repr(__state[model]), "image/png": __visualize_model(model, __state[model])} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
del __state | ||||||||||||||||||||||||||||
Comment on lines
+17
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using jinja templating, I think we can clean this up to something like the following.
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
__models |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will actually leave files in the "jupyter" user's home directory, since subkernels currently actually run as a normal kernel in the same environment. We probably don't want to create clutter.
We should probably delete these files after we read them, or write to a IOBuffer so the contents are all in memory.