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

update examples to use new solara_viz #193

Merged
merged 4 commits into from
Sep 17, 2024
Merged

update examples to use new solara_viz #193

merged 4 commits into from
Sep 17, 2024

Conversation

Corvince
Copy link
Contributor

@Corvince Corvince commented Sep 4, 2024

WIP to update the models to use the new solaraviz API.

This helps me to write a migration guide for things I notice. So far it goes smoothly, but is a bit tedious

@@ -4,9 +4,10 @@

import networkx as nx
import solara
from mesa.visualization.utils import update_counter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this import used at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, fixed. Currently, normal functions that accept a model and return a solara component automatically update correctly. If annotated with @solara.component you need to call the update counter. This has eventually go into the "author own components" of the viz tutorial

@EwoutH
Copy link
Member

EwoutH commented Sep 9, 2024

Examples that are difficult to update we could move back to the old, now experimental API.

@Corvince
Copy link
Contributor Author

Corvince commented Sep 9, 2024

Finally found some time to update the remaining models. There are still a lot of examples that use the old (tornado based) visualizations though. But they are out of scope of this pr

Copy link
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

I especially like that the agent_portrayal now is a part of a the SpaceDrawer class/component, and you thus can have a portrayal for each drawer. Way more scalable!

@@ -20,11 +20,15 @@
},
}

gol = GameOfLifeModel(10, 10)

AlivePlot = make_plot_measure("Cells alive", "Fraction alive")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this plot nicely on a single plot? Since "Cells alive" might be on the 0-100 scale, while "Fraction alive" is in the 0-1 scale.

(they are a bit duplicate though, so removing one is also fine)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, I split this into 2 plots

@@ -108,7 +108,8 @@ def agent_portrayal(agent):
return portrayal


def space_drawer(model, agent_portrayal):
@solara.component
def SpaceDrawer(model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is capitalized like a Class. For my own knowlegde, does @solara.component make it Class-like?

Copy link
Contributor Author

@Corvince Corvince Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't they all just objects? ;)

As far as I understood this is mostly a convention. It's best to imagine them as neither a class nor a function but as a solara component (because some custom rules apply)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, since I have looked it up by now. @solara.component does indeed return a regular ´class` object (with a a defined call function, so you can call it like a function). Usually they should still be written in snake_case for standard Python formatting, but thats where the solara convention takes over

@Corvince Corvince marked this pull request as ready for review September 17, 2024 06:57
@Corvince Corvince merged commit 9c30387 into main Sep 17, 2024
5 checks passed
EwoutH pushed a commit to EwoutH/mesa-examples that referenced this pull request Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants