zhaokeyao1
commited on
Commit
•
684f45f
1
Parent(s):
39a18a5
Update app
Browse files
app.py
CHANGED
@@ -1,97 +1,82 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
body {
|
7 |
-
font-family: Arial, sans-serif;
|
8 |
-
background-color: #f0f0f0;
|
9 |
-
}
|
10 |
-
.container {
|
11 |
-
max-width: 800px;
|
12 |
-
margin: 0 auto;
|
13 |
-
padding: 20px;
|
14 |
-
background-color: white;
|
15 |
-
border-radius: 10px;
|
16 |
-
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
17 |
-
}
|
18 |
-
h1 {
|
19 |
-
color: #333;
|
20 |
-
}
|
21 |
-
#custom-button {
|
22 |
-
background-color: #4CAF50;
|
23 |
-
border: none;
|
24 |
-
color: white;
|
25 |
-
padding: 15px 32px;
|
26 |
-
text-align: center;
|
27 |
-
text-decoration: none;
|
28 |
-
display: inline-block;
|
29 |
-
font-size: 16px;
|
30 |
-
margin: 4px 2px;
|
31 |
-
cursor: pointer;
|
32 |
-
}
|
33 |
-
#update-area {
|
34 |
-
margin-top: 20px;
|
35 |
-
padding: 10px;
|
36 |
-
border: 1px solid #ddd;
|
37 |
-
}
|
38 |
-
</style>
|
39 |
-
|
40 |
-
<div class="container">
|
41 |
-
<h1>动态更新界面示例</h1>
|
42 |
-
<button id="custom-button" onclick="updateInterface()">更新界面</button>
|
43 |
-
<div id="update-area">
|
44 |
-
点击按钮来更新这里的内容
|
45 |
-
</div>
|
46 |
-
</div>
|
47 |
-
|
48 |
-
<script>
|
49 |
-
function updateInterface() {
|
50 |
-
// 调用Gradio函数
|
51 |
-
var gradioApp = document.querySelector("gradio-app");
|
52 |
-
if (gradioApp == null) gradioApp = document.querySelector("body > gradio-app");
|
53 |
-
gradioApp.querySelector("#component-0").querySelector("button").click();
|
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 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import datetime
|
3 |
|
4 |
+
def update_content():
|
5 |
+
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
6 |
+
return f"�����Ѹ��£���ǰʱ��: {current_time}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
with gr.Blocks() as demo:
|
9 |
+
gr.HTML("""
|
10 |
+
<style>
|
11 |
+
body {
|
12 |
+
font-family: Arial, sans-serif;
|
13 |
+
background-color: #f0f0f0;
|
14 |
+
}
|
15 |
+
.container {
|
16 |
+
max-width: 800px;
|
17 |
+
margin: 0 auto;
|
18 |
+
padding: 20px;
|
19 |
+
background-color: white;
|
20 |
+
border-radius: 10px;
|
21 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
22 |
+
}
|
23 |
+
h1 {
|
24 |
+
color: #333;
|
25 |
+
}
|
26 |
+
#custom-button {
|
27 |
+
background-color: #4CAF50;
|
28 |
+
border: none;
|
29 |
+
color: white;
|
30 |
+
padding: 15px 32px;
|
31 |
+
text-align: center;
|
32 |
+
text-decoration: none;
|
33 |
+
display: inline-block;
|
34 |
+
font-size: 16px;
|
35 |
+
margin: 4px 2px;
|
36 |
+
cursor: pointer;
|
37 |
+
}
|
38 |
+
#update-area {
|
39 |
+
margin-top: 20px;
|
40 |
+
padding: 10px;
|
41 |
+
border: 1px solid #ddd;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
|
45 |
+
<div class="container">
|
46 |
+
<h1>��̬���½���ʾ��</h1>
|
47 |
+
<button id="custom-button" onclick="update()">���½���</button>
|
48 |
+
<div id="update-area">
|
49 |
+
�����ť���������������
|
50 |
+
</div>
|
51 |
+
</div>
|
52 |
+
""")
|
53 |
|
54 |
+
output = gr.Textbox(label="��������")
|
55 |
+
update_button = gr.Button("����", visible=False)
|
56 |
+
|
57 |
+
update_button.click(fn=update_content, inputs=[], outputs=[output])
|
58 |
|
59 |
+
gr.HTML("""
|
60 |
+
<script>
|
61 |
+
function update() {
|
62 |
+
document.querySelector('button.gr-button').click();
|
63 |
+
}
|
64 |
|
65 |
+
// ����Gradio����ı仯
|
66 |
+
document.addEventListener('DOMContentLoaded', (event) => {
|
67 |
+
const outputElement = document.querySelector('.gr-textbox-output');
|
68 |
+
const observer = new MutationObserver(function(mutations) {
|
69 |
+
mutations.forEach(function(mutation) {
|
70 |
+
if (mutation.type === "childList") {
|
71 |
+
document.getElementById("update-area").innerHTML = outputElement.value;
|
72 |
+
}
|
73 |
+
});
|
74 |
+
});
|
75 |
|
76 |
+
const config = { childList: true, subtree: true };
|
77 |
+
observer.observe(outputElement, config);
|
78 |
+
});
|
79 |
+
</script>
|
80 |
+
""")
|
81 |
|
82 |
+
demo.launch()
|