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

deploy/publish multiple models in a single webapp ? #1800

Open
rlskoeser opened this issue Sep 13, 2023 · 10 comments
Open

deploy/publish multiple models in a single webapp ? #1800

rlskoeser opened this issue Sep 13, 2023 · 10 comments

Comments

@rlskoeser
Copy link
Contributor

Is there a standard way to publish interactive Mesa models on the web? I'm particularly interested in publishing multiple related models together. I'm wondering about setting up a webapp on a sandbox/dev server as an alternative to using Colab for project team members to work with the simulations we're developing.

It seems like the new jupyterviz / solara code may make this easier. I see from the Solara deploy documentation that Solara apps can be embedded in Flask, so it seems like it should be possible to embed multiple different solara apps at different urls within a single Flask app.

Would appreciate any advice or input on how to proceed or examples if anyone has done something like this.

@rht
Copy link
Contributor

rht commented Sep 14, 2023

You are in an uncharted territory. #481 is the past issue when the old Tornado viz (mesa runserver) was the only option. We are planning to phase out the Tornado viz, and the Solara viz should be the supported one. I think adding multiple routes for a single Flask app might work well.

@rlskoeser
Copy link
Contributor Author

Ok. I'll report back with how it goes using flask + solara for our models, maybe it will be a useful approach for others.

@rlskoeser
Copy link
Contributor Author

Reporting back on this - flask isn't actually needed, you can just combine the existing solara apps as components into a single solara app. Here's the code for how I'm doing it currently for my project: https://github.com/Princeton-CDH/simulating-risk/blob/main/simulatingrisk/app.py

And here's a screenshot of the app that shows how you can toggle between the simulations:
Screenshot 2023-10-18 at 10 33 32 AM

@rht
Copy link
Contributor

rht commented Oct 19, 2023

That's great. We should document this in a howto page, just like in the Solara docs (https://solara.dev/docs/howto) -- they are structured based on https://diataxis.fr/ (tutorial, how-to guides, explanation, and reference). There are https://github.com/projectmesa/mesa/blob/main/docs/useful-snippets/snippets.rst and https://github.com/projectmesa/mesa/blob/main/docs/best-practices.rst. But I think snippets.rst should be renamed to howto.md. I will change it.

Is there a reason why it has to be solara.Route(path="riskybet", component=riskybet, label="Risky Bet"), instead of solara.Route(path="riskybet", component=riskybet_page, label="Risky Bet"),, given that JupyterViz is already a Solara component?

@rht
Copy link
Contributor

rht commented Oct 19, 2023

#1839

@rlskoeser
Copy link
Contributor Author

Is there a reason why it has to be solara.Route(path="riskybet", component=riskybet, label="Risky Bet"), instead of solara.Route(path="riskybet", component=riskybet_page, label="Risky Bet"),, given that JupyterViz is already a Solara component?

I tried using the JupyterViz components directly at first, but things didn't work right - I got errors that made it clear the wrong model was being used, like somehow when I switched simulations the model didn't switch. I wondered if if that might be caused by something in the mesa jupyterviz code, but since I'm not that familiar with mesa code and even less familiar with solara, I didn't want to go digging too far. Wrapping them in a component turned out to be an easy solution that kept them from contaminating each other.

@rht
Copy link
Contributor

rht commented Oct 20, 2023

I will investigate.

@rht
Copy link
Contributor

rht commented Oct 20, 2023

I think it's because the page object is a Solara component that has already been instantiated. Whereas the route requires a component creator that hasn't been instantiated. There is no way around this, it seems.

@rlskoeser
Copy link
Contributor Author

I think it's because the page object is a Solara component that has already been instantiated. Whereas the route requires a component creator that hasn't been instantiated. There is no way around this, it seems.

Ah, I see, that makes sense. If we had a method to instantiate the page could we import and call that to generate a new instance, instead of wrapping the page?

@rht
Copy link
Contributor

rht commented Oct 20, 2023

Yes, but it amounts to moving the component definition to each app.py (e.g. simulatingrisk/hawkdove/app.py)

@solara.component
def page():
    return JupyterViz(
        HawkDoveSingleRiskModel,
        jupyterviz_params,
        measures=[plot_hawks],
        name="Hawk/Dove game with risk attitudes; all agents have the same risk attitude",
        agent_portrayal=agent_portrayal,
        space_drawer=draw_hawkdove_agent_space,
    )
# required to render the visualization with Jupyter/Solara
page()

In the main app.py

from simulatingrisk.hawkdove.app import page as hawkdove_page

...
routes = [
    ...
    solara.Route(
        path="hawkdove-single", component=hawkdove_page, label="Hawk/Dove (single r)"
    ),
    ...
]

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

No branches or pull requests

2 participants