Py.Cafe

maartenbreddels/

generate-url-for-mesa-virus-network

Generate a url for pycafe

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import solara
import json
import gzip
import base64
from urllib.parse import quote, urlencode



def generate_link():
    json_object = {
        "code": "https://github.com/projectmesa/mesa-examples/raw/main/examples/virus_on_network/app.py",
        "requirements": "https://github.com/projectmesa/mesa-examples/raw/main/examples/virus_on_network/requirements.txt",
        "files": [
            {
                "name": "virus_on_network/model.py",
                "url": "https://github.com/projectmesa/mesa-examples/raw/main/examples/virus_on_network/virus_on_network/model.py",
            },
            {
                "name": "virus_on_network/server.py",
                "url": "https://github.com/projectmesa/mesa-examples/raw/main/examples/virus_on_network/virus_on_network/server.py",
            }
        ],
    }
    json_text = json.dumps(json_object)
    # gzip -> base64
    compressed_json_text = gzip.compress(json_text.encode("utf8"))
    base64_text = base64.b64encode(compressed_json_text).decode("utf8")
    query = urlencode({"c": base64_text}, quote_via=quote)
    base_url = "https://py.cafe"
    return f"{base_url}/snippet/solara?{query}"


@solara.component
def Page():
    solara.Button("Go", href=generate_link(), target="_blank")