import streamlit as st
import json
from pathlib import Path
st.set_page_config(page_title="Shader + Comments JSON", layout="wide")
st.title("🌀 Shader + Comments (Hugging Face Ready)")
# -------------------------------
# Use /data folder for persistent storage
# -------------------------------
DATA_DIR = Path("./data")
DATA_DIR.mkdir(exist_ok=True)
COMMENTS_FILE = DATA_DIR / "comments.json"
# Load existing comments
if COMMENTS_FILE.exists():
with open(COMMENTS_FILE, "r") as f:
comments = json.load(f)
else:
comments = []
# -------------------------------
# Determine current shader
# -------------------------------
shader_code_default = """
precision mediump float;
uniform vec2 iResolution;
uniform float iTime;
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
vec3 col = 0.5 + 0.5*cos(iTime + uv.xyx + vec3(0,2,4));
fragColor = vec4(col,1.0);
}
void main() { mainImage(gl_FragColor, gl_FragCoord.xy); }
"""
latest_shader = shader_code_default
for c in reversed(comments):
if c.get("type") == "shader":
latest_shader = c["content"]
break
# -------------------------------
# Shader editor
# -------------------------------
shader_code = st.text_area("Shader Code (GLSL Fragment Shader)", latest_shader, height=250)
# -------------------------------
# WebGL Canvas
# -------------------------------
html_code = f"""
"""
st.components.v1.html(html_code, height=520)
# -------------------------------
# Comments Section
# -------------------------------
st.markdown("---")
st.subheader("💬 Comments / Add New Shader")
##comment_type = st.radio("Type", ["Normal Comment", "New Shader"])
with st.form("comment_form", clear_on_submit=True):
user_name = st.text_input("Your name", "")
user_content = st.text_area("Content", "", height=80)
submitted = st.form_submit_button("Submit")
if submitted and user_content.strip():
new_comment = {
"name": user_name or "Anonymous",
"content": user_content,
}
comments.append(new_comment)
# Save to JSON in /data folder
with open(COMMENTS_FILE, "w") as f:
json.dump(comments, f, indent=2)
st.success("Saved!")
# Display all comments
for c in reversed(comments):
st.markdown(f"**{c['name']}**: {c['content']}")