-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt.py
More file actions
192 lines (148 loc) · 5.18 KB
/
Copy pathyt.py
File metadata and controls
192 lines (148 loc) · 5.18 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from dash import Dash, dcc, html, Input, Output, State, ALL, ctx
import dash_player
import json
import os
import dash_mantine_components as dmc
app = Dash(__name__)
pirate_king_message = "Hello I am the man who will king of Pirates!"
title_message = "Whenever the list of playlists goes long, refresh after finishing your work and start doing work again"
app.layout = dmc.MantineProvider(
dmc.Container(
size="md",
children=dmc.Stack(
align="center",
gap="xl",
children=[
dmc.Alert(
title_message,
title=pirate_king_message,
color="violet",
),
dmc.TextInput(
id="my-input",
placeholder="Type yt video id & See the latest video...",
value="",
styles={"input": {"color": "red", "textAlign": "center"}},
w="100%"
),
html.Div(id="yt-div"),
dmc.Space(h=40),
dmc.TextInput(
id="my-playlist-input",
placeholder="Add playlist videos (After Entering utube id press Enter)...",
value="",
styles={"input": {"color": "gold", "textAlign": "center"}},
w="100%"
),
dmc.Space(h=40),
html.Div(id="yt-extended-div"),
dmc.Space(h=20),
]
)
)
)
FILE_PATH = "saved_urls.json"
# --- file helpers ---
def load_urls():
if os.path.exists(FILE_PATH):
with open(FILE_PATH, "r") as f:
return json.load(f)
return []
def save_urls(urls):
with open(FILE_PATH, "w") as f:
json.dump(urls, f, indent=4)
# app.layout = dmc.MantineProvider(
# html.Div(
# [
# dcc.Input(
# id='my-input',
# type='text',
# placeholder='Type yt video id & See the latest video...',
# value='',
# style={"width":"33%","color":"red",'textAlign': 'center',"margin-left":"33%"}
# ),
# html.Br(), html.Br(), html.Br(), html.Br(),
# html.Div(id="yt-div"),
# html.Br(), html.Br(), html.Br(), html.Br(),
# dcc.Input(
# id='my-playlist-input',
# type='text',
# placeholder='Add playlist videos (After Entering utube id press Enter)...',
# value='',
# style={"width":"33%","color":"gold",'textAlign': 'center',"margin-left":"33%"}
# ),
# html.Br(), html.Br(), html.Br(), html.Br(),
# html.Div(id="yt-extended-div"),
# html.Br(),
# ]
# )
# )
# --- SINGLE VIDEO ---
@app.callback(
Output('yt-div', 'children'),
Input('my-input', 'value'),
prevent_initial_call=True
)
def update_single(value):
if not value:
return []
return [
html.Div([
dash_player.DashPlayer(
url=f"https://youtu.be/{value}",
controls=True,
style={"width":"33%", "margin-left":"33%"}
),
html.Br(), html.Br(), html.Br(), html.Br(),
])
]
# --- PLAYLIST (TRIGGER ONLY ON ENTER) ---
@app.callback(
Output('yt-extended-div', 'children'),
Output('my-playlist-input', 'value'), # clears input after Enter
Input('my-playlist-input', 'n_submit'), # 👈 Enter key trigger
State('my-playlist-input', 'value'),
State('yt-extended-div', 'children'),
prevent_initial_call=True
)
def update_playlist(n_submit, value, children):
if children is None:
children = []
if not value:
return children, ""
children.append(
html.Div([
dash_player.DashPlayer(
url=f"https://youtu.be/{value}",
controls=True,
style={"width":"33%", "margin-left":"33%"}
),
html.Br(), html.Br(), html.Br(), html.Br(),
html.Button(
"Save",
id={"type": "save-btn", "index": value},
n_clicks=0,
style={"width":"10%","padding":"10px", "margin-left":"35%","color":"white","backgroundColor":"blue","borderRadius":"1000px"}
),
html.Br(), html.Br(), html.Br(), html.Br(),
])
)
return children, "" # clear input box
# --- SAVE WHEN ANY PLAYLIST BUTTON CLICKED ---
@app.callback(
Output('my-input', 'placeholder'), # dummy output
Input({"type": "save-btn", "index": ALL}, "n_clicks"),
prevent_initial_call=True
)
def save_from_playlist(n_clicks):
if not ctx.triggered:
return "Type yt video id..."
video_id = ctx.triggered_id["index"]
url = f"https://youtu.be/{video_id}"
saved = load_urls()
if url not in saved:
saved.append(url)
save_urls(saved)
return "Type yt video id & See the latest video..."
if __name__ == "__main__":
app.run(debug=True)