Py.Cafe

maartenbreddels/

solara-menu-demo

Interactive Model Deployment and Fitting demonstrating the Menu componnent

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import asyncio
import solara
import solara.lab
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

slope = solara.reactive(1.5)
intercept = solara.reactive(0.5)

x = np.arange(0, 100, 2) / 10
# fake data with noise
y_data = (slope.value * 0.8) * x + (intercept.value + 0.4) + np.random.normal(0, 0.5, len(x))


@solara.component
def Page():
    deploy_environment_request = solara.use_reactive("")
    deploy_environment = solara.use_reactive("")
    fake_failed = solara.use_reactive(False)

    async def deploy_model():
        if deploy_environment.value == "":
            return

        # 'dummy' deployment
        await asyncio.sleep(1.5)

        # fake a failure the first time
        if not fake_failed.value:
            fake_failed.value = True
            return False
        else:
            return True

    deploy_state: solara.lab.Task[bool] = solara.lab.use_task(deploy_model, dependencies=[deploy_environment.value])

    x_model = x
    y_model = (slope.value) * x_model + (intercept.value)
    df1 = pd.DataFrame({"x": x, "y": y_data})
    df2 = pd.DataFrame({"x": x_model, "y": y_model})
    df = pd.concat([df1, df2], keys=["data", "model"], names=["type"]).reset_index()
    fig = px.line(df, x="x", y="y", color="type")
    with solara.Card("My fancy model"):
        fig_element = solara.FigurePlotly(fig)

        def reset():
            slope.set(1.5)
            intercept.set(0.5)

        def fit():
            slope.set(1.5 * 0.8)
            intercept.set(0.9)

        with solara.lab.ContextMenu(activator=fig_element):
            solara.Button("Reset", text=True, icon_name="mdi-refresh", on_click=reset)
            solara.Button("Fit", text=True, icon_name="mdi-brain", on_click=fit)

        solara.FloatSlider("Slope", value=slope, min=-2, max=5, step=0.1)
        solara.FloatSlider("Intercept", value=intercept, min=-10, max=10, step=0.1)

        solara.lab.ConfirmationDialog(
            deploy_environment_request.value != "",
            title="Confirm deployment",
            content=solara.Markdown(f"Are you sure you want to deploy to **{deploy_environment_request.value}**?"),
            ok=f"Deploy to {deploy_environment_request.value}",
            on_ok=lambda: deploy_environment.set(deploy_environment_request.value),
            on_close=lambda: deploy_environment_request.set(""),
        )

        solara.lab.ConfirmationDialog(
            deploy_state.value is False and deploy_state.finished,
            title="Deployment failed",
            on_close=lambda: None,
            on_ok=lambda: deploy_state.retry(),
            content=solara.Error(solara.Markdown(f"Deployment to **{deploy_environment.value}** failed. Do you want to try again?")),
            ok="Retry deployment",
            cancel="Ignore",
        )

        def request_deploy(environment):
            deploy_environment.value = ""
            deploy_environment_request.set(environment)

        deploying = bool(deploy_environment.value and deploy_state.pending)
        deploy_button = solara.Button("Deploy model", color="primary", icon_name="mdi-rocket", loading=deploying)
        with solara.lab.Menu(activator=deploy_button):
            with solara.Column(gap="0px"):
                solara.Button("Deploy to testing environment", text=True, icon_name="mdi-bug", on_click=lambda: request_deploy("testing"))
                solara.Button("Deploy to staging environment", text=True, icon_name="mdi-test-tube", on_click=lambda: request_deploy("staging"))
                solara.Button(
                    "Deploy to production",
                    text=True,
                    icon_name="mdi-rocket",
                    style={"justify-content": "left"},  # make the icon appear on the left
                    on_click=lambda: request_deploy("production"),
                )
        if deploy_state.value is True and deploy_state.finished:
            # add some margin in the y direction using my-4
            solara.Success(solara.Markdown(f"Model deployed successfully to **{deploy_environment.value}**!"), classes=["my-4"])