Py.Cafe

maartenbreddels/

pycafe-url-generator

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
37
38
39
40
41
42
43
44
import solara
import json
import gzip
import base64
from urllib.parse import quote, urlencode



def generate_link():
    json_object = {
        "code": "import solara; page = solara.Button('hi')",
        "requirements": "solara",
        "files": [
            {
                "name": "some_file_loaded_from_a_url.py",
                "url": "https://github.com/projectmesa/mesa-examples/raw/main/examples/virus_on_network/virus_on_network/model.py",
            },
            {
                "name": "other_file_from_data.py",
                "content": "plain text",
            },
            {
                "name": "binary_data.dat",
                "content": base64.b64encode(b"binary data").decode("utf8"),
                "encoding": "base64"
            }
        ],
    }
    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"
    type = "solara"  # replace by dash, or streamlit
    return f"{base_url}/snippet/{type}/v1?{query}"


@solara.component
def Page():
    with solara.Card("Snippet link generator"):
        solara.Markdown("Edit the code to generate a new url")
        solara.Button("Open PyCafe snippet", href=generate_link(), target="_blank")