Update src/streamlit_app.py
Browse files- src/streamlit_app.py +57 -154
src/streamlit_app.py
CHANGED
|
@@ -1,154 +1,57 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import json
|
| 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 |
-
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']}")
|
|
|
|
| 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 |
+
# --- Shader Editor ---
|
| 17 |
+
st.subheader("Shader Editor")
|
| 18 |
+
default_shader = """// Fresh GLSL shader example
|
| 19 |
+
#ifdef GL_ES
|
| 20 |
+
precision mediump float;
|
| 21 |
+
#endif
|
| 22 |
+
|
| 23 |
+
uniform float iTime;
|
| 24 |
+
uniform vec2 iResolution;
|
| 25 |
+
|
| 26 |
+
void main() {
|
| 27 |
+
vec2 uv = gl_FragCoord.xy / iResolution.xy;
|
| 28 |
+
vec3 color = 0.5 + 0.5 * cos(iTime + uv.xyx * 3.0);
|
| 29 |
+
gl_FragColor = vec4(color, 1.0);
|
| 30 |
+
}
|
| 31 |
+
"""
|
| 32 |
+
shader_code = st.text_area("Edit shader code", default_shader, height=300)
|
| 33 |
+
|
| 34 |
+
# --- Comments Section ---
|
| 35 |
+
st.subheader("Comments")
|
| 36 |
+
if comments:
|
| 37 |
+
for c in comments:
|
| 38 |
+
st.write(f"**{c['name']}**: {c['content']}")
|
| 39 |
+
else:
|
| 40 |
+
st.write("_No comments yet_")
|
| 41 |
+
|
| 42 |
+
# --- Add a new comment ---
|
| 43 |
+
with st.form("comment_form", clear_on_submit=True):
|
| 44 |
+
user_name = st.text_input("Your name", "")
|
| 45 |
+
user_content = st.text_area("Content", height=80)
|
| 46 |
+
submitted = st.form_submit_button("Submit")
|
| 47 |
+
|
| 48 |
+
if submitted and user_content.strip():
|
| 49 |
+
new_comment = {
|
| 50 |
+
"name": user_name or "Anonymous",
|
| 51 |
+
"content": user_content
|
| 52 |
+
}
|
| 53 |
+
comments.append(new_comment)
|
| 54 |
+
with open(COMMENTS_FILE, "w") as f:
|
| 55 |
+
json.dump(comments, f, indent=2)
|
| 56 |
+
st.success("Saved!")
|
| 57 |
+
st.experimental_rerun() # refresh to show new comment immediately
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|