Py.Cafe

giselle.pomodor/

dash-knob-led-control

Control Panel for Knob and LED Displays

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import dash
from dash import html, dcc
from dash.dependencies import Input, Output
import dash_daq as daq

# Initialize the Dash app
app = dash.Dash(__name__)

# Define the app layout
app.layout = daq.DarkThemeProvider(
    children=html.Div(
        style={
            'backgroundColor': '#1a1a1a',
            'color': '#e0e0e0',
            'textAlign': 'center',
            'fontFamily': '"Droid Sans", "Lucida Sans Unicode", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif',
            'padding': '50px'
        },
        children=[
            html.Div(
                daq.Knob(
                    id='my-knob',
                    min=0,
                    max=100,
                    value=0,
                    color={"default": "#FFDD44"},
                    size=150,
                    style={
                        'backgroundColor': '#333',
                        'borderRadius': '50%',
                        'padding': '20px',
                        'display': 'inline-block',
                        'margin': '20px'
                    }
                )
            ),
            html.Div(
                [
                    daq.LEDDisplay(
                        id='my-led-1',
                        value="0",
                        color="#FFDD44",
                        backgroundColor="#444",
                        style={
                            'borderRadius': '10px',
                            'padding': '10px',
                            'margin': '5px',
                            'display': 'inline-block',
                            'textAlign': 'center'
                        }
                    ),
                    daq.LEDDisplay(
                        id='my-led-2',
                        value="0",
                        color="#FFDD44",
                        backgroundColor="#444",
                        style={
                            'borderRadius': '10px',
                            'padding': '10px',
                            'margin': '5px',
                            'display': 'inline-block',
                            'textAlign': 'center'
                        }
                    ),
                    daq.LEDDisplay(
                        id='my-led-3',
                        value="0",
                        color="#FFDD44",
                        backgroundColor="#444",
                        style={
                            'borderRadius': '10px',
                            'padding': '10px',
                            'margin': '5px',
                            'display': 'inline-block',
                            'textAlign': 'center'
                        }
                    ),
                    daq.LEDDisplay(
                        id='my-led-4',
                        value="0",
                        color="#FFDD44",
                        backgroundColor="#444",
                        style={
                            'borderRadius': '10px',
                            'padding': '10px',
                            'margin': '5px',
                            'display': 'inline-block',
                            'textAlign': 'center'
                        }
                    ),
                    daq.LEDDisplay(
                        id='my-led-5',
                        value="0",
                        color="#FFDD44",
                        backgroundColor="#444",
                        style={
                            'borderRadius': '10px',
                            'padding': '10px',
                            'margin': '5px',
                            'display': 'inline-block',
                            'textAlign': 'center'
                        }
                    )
                ],
                style={'display': 'flex', 'justifyContent': 'center', 'marginBottom': '50px'}
            ),
            daq.ToggleSwitch(
                id='reset-switch',
                value=False,
                color="#FFDD44",
                label="Reset to Zero",
                labelPosition="top",
                style={'margin': '20px'}
            )
        ]
    )
)

# Define the callback to update the LEDs based on the knob value and reset switch
@app.callback(
    [Output('my-led-1', 'value'),
     Output('my-led-2', 'value'),
     Output('my-led-3', 'value'),
     Output('my-led-4', 'value'),
     Output('my-led-5', 'value')],
    [Input('my-knob', 'value'),
     Input('reset-switch', 'value')]
)
def update_leds(knob_value, reset_switch):
    if reset_switch:
        return ["0", "0", "0", "0", "0"]
    else:
        # Scale the knob value to fit within the range 00000 to 99999
        scaled_value = int((knob_value / 100) * 99999)
        # Format the scaled value as a 5-digit string with leading zeros
        formatted_value = f"{scaled_value:05d}"
        return list(formatted_value)

# Run the server
if __name__ == '__main__':
    app.run_server(debug=True)