MySafeCode commited on
Commit
cee7b68
·
verified ·
1 Parent(s): 76b0c65

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +125 -20
src/app.py CHANGED
@@ -1,43 +1,148 @@
1
  import streamlit as st
2
  import json
3
- import os
4
 
5
- # --- Paths ---
6
- COMMENTS_FILE = "data/comments.json"
7
- os.makedirs("data", exist_ok=True)
8
 
9
- # --- Load existing comments ---
10
- if os.path.exists(COMMENTS_FILE):
 
 
 
 
 
 
 
 
 
11
  with open(COMMENTS_FILE, "r") as f:
12
  comments = json.load(f)
13
  else:
14
  comments = []
15
 
16
- # --- Comments Section ---
17
- st.subheader("Comments")
18
- if comments:
19
- for c in comments:
20
- st.write(f"**{c['name']}**: {c['content']}")
21
- else:
22
- st.write("_No comments yet_")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # --- Add a new comment ---
25
  with st.form("comment_form", clear_on_submit=True):
26
  user_name = st.text_input("Your name", "")
27
- user_content = st.text_area("Content", height=80)
28
  submitted = st.form_submit_button("Submit")
29
 
30
  if submitted and user_content.strip():
31
- # --- Create new comment ---
32
  new_comment = {
33
  "name": user_name or "Anonymous",
34
- "content": user_content
 
35
  }
36
-
37
- # --- Append and save to JSON ---
38
  comments.append(new_comment)
 
 
39
  with open(COMMENTS_FILE, "w") as f:
40
- json.dump(comments, f, indent=2) # THIS line writes the JSON
41
 
42
  st.success("Saved!")
43
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import json
3
+ from pathlib import Path
4
 
5
+ st.set_page_config(page_title="Shader + Comments JSON", layout="wide")
 
 
6
 
7
+ st.title("🌀 Shader + Comments (Hugging Face Ready)")
8
+
9
+ # -------------------------------
10
+ # Use /data folder for persistent storage
11
+ # -------------------------------
12
+ DATA_DIR = Path("./data")
13
+ DATA_DIR.mkdir(exist_ok=True)
14
+ COMMENTS_FILE = DATA_DIR / "comments.json"
15
+
16
+ # Load existing comments
17
+ if COMMENTS_FILE.exists():
18
  with open(COMMENTS_FILE, "r") as f:
19
  comments = json.load(f)
20
  else:
21
  comments = []
22
 
23
+ # -------------------------------
24
+ # Determine current shader
25
+ # -------------------------------
26
+ shader_code_default = """
27
+ precision mediump float;
28
+ uniform vec2 iResolution;
29
+ uniform float iTime;
30
+
31
+ void mainImage(out vec4 fragColor, in vec2 fragCoord) {
32
+ vec2 uv = fragCoord / iResolution.xy;
33
+ vec3 col = 0.5 + 0.5*cos(iTime + uv.xyx + vec3(0,2,4));
34
+ fragColor = vec4(col,1.0);
35
+ }
36
+ void main() { mainImage(gl_FragColor, gl_FragCoord.xy); }
37
+ """
38
+
39
+ latest_shader = shader_code_default
40
+ for c in reversed(comments):
41
+ if c.get("type") == "shader":
42
+ latest_shader = c["content"]
43
+ break
44
+
45
+ # -------------------------------
46
+ # Shader editor
47
+ # -------------------------------
48
+ shader_code = st.text_area("Shader Code (GLSL Fragment Shader)", latest_shader, height=250)
49
+
50
+ # -------------------------------
51
+ # WebGL Canvas
52
+ # -------------------------------
53
+ html_code = f"""
54
+ <canvas id="glcanvas" width="800" height="500"></canvas>
55
+ <script>
56
+ const canvas = document.getElementById('glcanvas');
57
+ const gl = canvas.getContext('webgl');
58
+ if (!gl) {{
59
+ alert('WebGL not supported');
60
+ }}
61
+
62
+ const vertCode = `
63
+ attribute vec4 a_position;
64
+ void main() {{
65
+ gl_Position = a_position;
66
+ }}
67
+ `;
68
+
69
+ const fragCode = `{shader_code}`;
70
+
71
+ function createShader(gl, type, source) {{
72
+ const shader = gl.createShader(type);
73
+ gl.shaderSource(shader, source);
74
+ gl.compileShader(shader);
75
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {{
76
+ alert(gl.getShaderInfoLog(shader));
77
+ }}
78
+ return shader;
79
+ }}
80
+
81
+ const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertCode);
82
+ const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragCode);
83
+
84
+ const program = gl.createProgram();
85
+ gl.attachShader(program, vertexShader);
86
+ gl.attachShader(program, fragmentShader);
87
+ gl.linkProgram(program);
88
+ gl.useProgram(program);
89
+
90
+ const positionBuffer = gl.createBuffer();
91
+ gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
92
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
93
+ -1, -1, 1, -1, -1, 1,
94
+ -1, 1, 1, -1, 1, 1
95
+ ]), gl.STATIC_DRAW);
96
+
97
+ const positionLoc = gl.getAttribLocation(program, "a_position");
98
+ gl.enableVertexAttribArray(positionLoc);
99
+ gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 0, 0);
100
+
101
+ const iResolution = gl.getUniformLocation(program, "iResolution");
102
+ const iTime = gl.getUniformLocation(program, "iTime");
103
+
104
+ function render(time) {{
105
+ gl.uniform2f(iResolution, canvas.width, canvas.height);
106
+ gl.uniform1f(iTime, time * 0.001);
107
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
108
+ requestAnimationFrame(render);
109
+ }}
110
+
111
+ requestAnimationFrame(render);
112
+ </script>
113
+ """
114
+
115
+ st.components.v1.html(html_code, height=520)
116
+
117
+ # -------------------------------
118
+ # Comments Section
119
+ # -------------------------------
120
+ st.markdown("---")
121
+ st.subheader("💬 Comments / Add New Shader")
122
+
123
+ ##comment_type = st.radio("Type", ["Normal Comment", "New Shader"])
124
 
 
125
  with st.form("comment_form", clear_on_submit=True):
126
  user_name = st.text_input("Your name", "")
127
+ user_content = st.text_area("Content", "", height=80)
128
  submitted = st.form_submit_button("Submit")
129
 
130
  if submitted and user_content.strip():
 
131
  new_comment = {
132
  "name": user_name or "Anonymous",
133
+ "content": user_content,
134
+
135
  }
 
 
136
  comments.append(new_comment)
137
+
138
+ # Save to JSON in /data folder
139
  with open(COMMENTS_FILE, "w") as f:
140
+ json.dump(comments, f, indent=2)
141
 
142
  st.success("Saved!")
143
 
144
+
145
+
146
+ # Display all comments
147
+ for c in reversed(comments):
148
+ st.markdown(f"**{c['name']}**: {c['content']}")