MySafeCode commited on
Commit
2158f0d
·
verified ·
1 Parent(s): 48782f4

Upload 3 files

Browse files
Files changed (3) hide show
  1. src/app.py +154 -0
  2. src/data/comments.json +12 -0
  3. src/streamlit_app.py +154 -40
src/app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "type": "shader" if comment_type == "New Shader" else "comment"
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
+ # If it's a shader, update editor immediately
145
+ if new_comment["type"] == "shader":
146
+ st.experimental_rerun()
147
+
148
+ # Display all comments
149
+ for c in reversed(comments):
150
+ if c["type"] == "shader":
151
+ st.markdown(f"**{c['name']} added a new shader:**")
152
+ st.code(c["content"], language="glsl")
153
+ else:
154
+ st.markdown(f"**{c['name']}**: {c['content']}")
src/data/comments.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "name": "Testing",
4
+ "content": "Comments",
5
+ "type": "comment"
6
+ },
7
+ {
8
+ "name": "More",
9
+ "content": "Commentators",
10
+ "type": "comment"
11
+ }
12
+ ]
src/streamlit_app.py CHANGED
@@ -1,40 +1,154 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "type": "shader" if comment_type == "New Shader" else "comment"
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
+ # If it's a shader, update editor immediately
145
+ if new_comment["type"] == "shader":
146
+ st.experimental_rerun()
147
+
148
+ # Display all comments
149
+ for c in reversed(comments):
150
+ if c["type"] == "shader":
151
+ st.markdown(f"**{c['name']} added a new shader:**")
152
+ st.code(c["content"], language="glsl")
153
+ else:
154
+ st.markdown(f"**{c['name']}**: {c['content']}")