mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Feat: Added Model sharing through links

Open Sahil-Chhoker opened this issue 9 months ago • 8 comments

Summary

This PR provides a basic model sharing through a button on the solara web-interface. It uses PyCafe to generate links.

Motive

As explained in #2724. Closes #2724.

Implementation

A new module named model_share.py is added to mesa that handles the link generating and a new button labeled "Open on PyCafe" on the solara's AppBar, that redirects to the generated link that contains all the files your model uses. Strict naming convention is followed for autodetection i.e. model.py, agents.py and app.py.

image

Additional Notes

There are many uncertainties and assumptions in this implementation like the button only works if you are present in that directory and I am unsure about if this is even the correct approach to handle this. But having a draft to work with will make things easier that's why this PR is opened. Please list your opinions.

Summary by CodeRabbit

  • New Features
    • Introduced functionality to generate a shareable link for your Python code and its dependencies, with options to customize included files.
    • Added an "Open on PyCafe" button to the visualization interface, which opens the generated link in a new browser tab.

Sahil-Chhoker avatar Mar 18 '25 14:03 Sahil-Chhoker

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.5% [-1.1%, +0.1%] 🔵 -0.7% [-0.9%, -0.5%]
BoltzmannWealth large 🔵 -0.7% [-1.7%, +0.1%] 🔵 -1.2% [-2.2%, -0.3%]
Schelling small 🔵 -0.1% [-0.3%, +0.0%] 🔵 -0.3% [-0.4%, -0.2%]
Schelling large 🔵 -0.0% [-0.4%, +0.3%] 🔵 -2.2% [-3.0%, -1.5%]
WolfSheep small 🔵 -0.6% [-1.0%, -0.3%] 🔵 -0.2% [-0.4%, -0.0%]
WolfSheep large 🔵 -1.0% [-1.3%, -0.7%] 🔵 -0.3% [-0.7%, +0.2%]
BoidFlockers small 🔵 -0.4% [-1.1%, +0.3%] 🔵 -1.5% [-1.6%, -1.3%]
BoidFlockers large 🔵 -0.5% [-0.9%, -0.1%] 🔵 -1.2% [-1.6%, -0.9%]

github-actions[bot] avatar Mar 18 '25 14:03 github-actions[bot]

@Sahil-Chhoker This is very cool!

My initial thoughts, to which I am not tied) would be

  • Make it a "Share with PYCAFE" (Thinking is for those that dont know py.cafe it gives a more obvious use of the button)
  • With the uncertainties I would not make it a default button, we dont want a users visualization to break for a feature they may not want. We should advertise though, so would need to add it to the visualization tutorial and default on all examples. Maybe a Boolean kwarg?
  • With the regards to the location, I am thinking in the docs. This is about sharing Mesa/ models/ approaches which is effectively what the docs does, but can go either way

Regardless, this is a great idea and feature!

tpike3 avatar Mar 23 '25 10:03 tpike3

Thanks for your reply @tpike3

  • Make it a "Share with PYCAFE" (Thinking is for those that dont know py.cafe it gives a more obvious use of the button)

Makes sense, will do!

  • With the uncertainties I would not make it a default button, we dont want a users visualization to break for a feature they may not want. We should advertise though, so would need to add it to the visualization tutorial and default on all examples. Maybe a Boolean kwarg?

It makes sense to not make it a default button, but some things I want to clear are that the viz. won't break even if the button does not work as expected, in such case on clicking on the button an empty solara pycafe project opens. And another thing I want to propose is that we should have a shared account on PyCafe for Mesa, and we can host every example in the main repo their and add their respective links. Since the button functionality works great when you are in the current working directory, it can be a user only button (if this even makes any sense).

  • With the regards to the location, I am thinking in the docs. This is about sharing Mesa/ models/ approaches which is effectively what the docs does, but can go either way

Makes sense, will do!

Sahil-Chhoker avatar Mar 24 '25 04:03 Sahil-Chhoker

Walkthrough

The changes introduce a new module in mesa/model_share.py that provides shareable link generation for Python code and its dependencies via the py.cafe service. The module defines the get_pycafe_link function, which builds a JSON object from provided files, requirements, and optionally autodetected Python files, compresses and encodes this data, and returns a URL. Additionally, the mesa/visualization/solara_viz.py file now features an "Open on PyCafe" button that utilizes this function to generate and open the shareable link in a new browser tab.

Changes

File(s) Changed Change Summary
mesa/model_share.py Added new module with get_pycafe_link and _scan_python_files functions to generate shareable PyCafe links for code and dependencies.
mesa/visualization/solara_viz.py Integrated a new "Open on PyCafe" button in the UI that calls get_pycafe_link to generate a shareable link and opens it in a new browser tab.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant V as SolaraViz Component
    participant M as model_share Module

    U->>V: Click "Open on PyCafe" button
    V->>M: Call get_pycafe_link(files, requirements, autodetect)
    M-->>V: Return PyCafe URL
    V->>U: Open URL in new browser tab

Assessment against linked issues

Objective Addressed Explanation
Sharing of Models through links (#2724)

Poem

I'm just a bunny with a joyful hop,
Hop-skipping code where the ideas never stop.
I crafted a link that makes sharing so grand,
Hop into py.cafe with a gentle command.
Cheers from my fluffy ears to your clever command!

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 04 '25 06:04 coderabbitai[bot]

@coderabbitai full review

Sahil-Chhoker avatar Apr 04 '25 06:04 Sahil-Chhoker

:white_check_mark: Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot] avatar Apr 04 '25 06:04 coderabbitai[bot]

:white_check_mark: Actions performed

Full review triggered.

coderabbitai[bot] avatar Apr 04 '25 06:04 coderabbitai[bot]

@jackiekazil sorry for leaving this PR unfinished, I plan to complete the work as part of my GSoC commitment.

Sahil-Chhoker avatar May 19 '25 17:05 Sahil-Chhoker