-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathusage.py
More file actions
82 lines (69 loc) · 1.89 KB
/
usage.py
File metadata and controls
82 lines (69 loc) · 1.89 KB
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
import math
from typing import Annotated, Literal
import dash
from dash import Input, Output, State, html
from flowfunc import Flowfunc, config, jobrunner
from enum import Enum
def add(a: int, b: int):
"""Add two numbers"""
return a + b
def subtract(
a: Annotated[int | float, {"label": "First number"}],
b: Annotated[int | float, {"label": "Second number"}],
):
"""Subtract one number from another"""
return a - b
class MathFunction(str, Enum):
sin = "sin"
cos = "cos"
tan = "tan"
asin = "asin"
acos = "acos"
atan = "atan"
def trig_function(
x: float, func: Annotated[MathFunction, {"label": "Function", "hidePort": True}]
):
"""Trigonometric function"""
return getattr(math, func)(x)
flist = [
add,
subtract,
trig_function,
]
app = dash.Dash(__name__)
fconfig = config.Config.from_function_list(flist)
runner = jobrunner.JobRunner(fconfig)
app.layout = html.Div(
[
html.Button(id="btn_run", children=["Run"]),
html.Div(
Flowfunc(
id="nodeeditor",
config=fconfig.dict(),
disable_zoom=True,
type_safety=False,
),
style={"height": "500px", "width": "100%"},
),
html.Div(id="output"),
]
)
@app.callback(
[Output("output", "children"), Output("nodeeditor", "nodes_status")],
Input("btn_run", "n_clicks"),
State("nodeeditor", "nodes"),
)
def run(nclicks, nodes):
if not nodes:
return [], {}
output = runner.run(nodes)
output_html = []
nodes_status = {}
for node in output.values():
output_html.append(html.Div(f"{node.type}: {node.result}"))
if node.error:
output_html.append(html.Div(f"{node.error}"))
nodes_status[node.id] = node.status
return output_html, nodes_status
if __name__ == "__main__":
app.run_server(debug=True)