water64 commited on
Commit
fca6b25
·
verified ·
1 Parent(s): f58f2b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py CHANGED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ initial_p5js_code = """
5
+ function setup() {
6
+ createCanvas(800, 400);
7
+ }
8
+
9
+ function draw() {
10
+ // 左眼視圖 (0, 0, 400, 400)
11
+ push();
12
+ translate(0, 0);
13
+ background(200);
14
+ fill(255, 0, 0);
15
+ ellipse(200, 200, 100, 100);
16
+ pop();
17
+
18
+ // 右眼視圖 (400, 0, 400, 400)
19
+ push();
20
+ translate(400, 0);
21
+ background(200);
22
+ fill(255, 0, 0);
23
+ ellipse(200, 200, 100, 100);
24
+ pop();
25
+ }
26
+ """
27
+
28
+ html_template = """
29
+ <!DOCTYPE html>
30
+ <html>
31
+ <head>
32
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
33
+ <style>
34
+ html, body {
35
+ margin: 0;
36
+ padding: 0;
37
+ }
38
+ </style>
39
+ </head>
40
+ <body>
41
+ <script>
42
+ {p5js_code}
43
+ </script>
44
+ </body>
45
+ </html>
46
+ """
47
+
48
+ def update_p5js(code):
49
+ # 生成唯一的ID以避免快取問題
50
+ unique_id = random.randint(1, 1000000)
51
+ html_content = html_template.format(p5js_code=code)
52
+ return html_content
53
+
54
+ interface = gr.Interface(
55
+ fn=update_p5js,
56
+ inputs=gr.Code(language="javascript", label="P5.js Code", value=initial_p5js_code),
57
+ outputs=gr.HTML(label="VR Preview"),
58
+ title="P5.js VR Viewer",
59
+ description="輸入P5.js程式碼來創建VR左右眼畫面。畫布寬度為800px(每隻眼睛400px)、高度為400px。"
60
+ )
61
+
62
+ if __name__ == "__main__":
63
+ interface.launch()