Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,87 @@
|
|
1 |
# app.py
|
2 |
import streamlit as st
|
3 |
import streamlit.components.v1 as components
|
4 |
-
import os
|
|
|
5 |
|
6 |
-
# --- 1. Set Page Configuration ---
|
7 |
-
# This must be the first Streamlit command called
|
8 |
st.set_page_config(
|
9 |
-
page_title="Three.js
|
10 |
-
layout="wide" #
|
11 |
)
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
# ---
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
html_file_path = 'index.html'
|
19 |
|
20 |
-
# --- 3. Read the HTML file ---
|
21 |
try:
|
22 |
with open(html_file_path, 'r', encoding='utf-8') as f:
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
#
|
27 |
components.html(
|
28 |
-
|
29 |
-
height=
|
30 |
-
scrolling=False
|
31 |
)
|
32 |
|
33 |
-
st.sidebar.info("Use WASD or Arrow Keys inside the frame above to move the cube.")
|
34 |
-
|
35 |
except FileNotFoundError:
|
36 |
st.error(f"Error: Could not find the file '{html_file_path}'.")
|
37 |
-
st.warning("Please make sure
|
38 |
except Exception as e:
|
39 |
-
st.error(f"An error occurred
|
|
|
|
1 |
# app.py
|
2 |
import streamlit as st
|
3 |
import streamlit.components.v1 as components
|
4 |
+
import os
|
5 |
+
import json # To safely embed the python variable into JS
|
6 |
|
7 |
+
# --- 1. Set Page Configuration (Set first!) ---
|
|
|
8 |
st.set_page_config(
|
9 |
+
page_title="Three.js Editor",
|
10 |
+
layout="wide" # Use the full screen width
|
11 |
)
|
12 |
|
13 |
+
# --- Initialize Session State ---
|
14 |
+
if 'selected_object' not in st.session_state:
|
15 |
+
st.session_state.selected_object = 'None' # Default selection
|
16 |
|
17 |
+
# --- Sidebar Controls ---
|
18 |
+
with st.sidebar:
|
19 |
+
st.title("🛠️ Controls & State")
|
20 |
+
st.caption("Select an object type and click on the ground in the main view to place it.")
|
21 |
+
|
22 |
+
# Define available object types (keys should ideally match JS functions/identifiers)
|
23 |
+
object_types = ["None", "Simple House", "Tree", "Rock", "Fence Post"] # Add more as needed
|
24 |
+
|
25 |
+
# Selectbox to choose object type - this updates session_state on change
|
26 |
+
selected = st.selectbox(
|
27 |
+
"Select Object to Place:",
|
28 |
+
options=object_types,
|
29 |
+
key='selected_object' # Link to session state key
|
30 |
+
)
|
31 |
+
|
32 |
+
st.write("---")
|
33 |
+
st.header("Current State:")
|
34 |
+
st.write(f"Selected Object Type: **{st.session_state.selected_object}**")
|
35 |
+
# Add more state tracking here later if needed
|
36 |
+
st.info("Use WASD/Arrows in the main view if player movement is enabled in JS.")
|
37 |
+
|
38 |
+
|
39 |
+
# --- Main Area for the 3D View ---
|
40 |
+
st.header("🏗️ Three.js Primitive Builder")
|
41 |
+
|
42 |
+
# --- Load and Prepare HTML ---
|
43 |
html_file_path = 'index.html'
|
44 |
|
|
|
45 |
try:
|
46 |
with open(html_file_path, 'r', encoding='utf-8') as f:
|
47 |
+
html_template = f.read()
|
48 |
+
|
49 |
+
# --- Inject Python state into JavaScript ---
|
50 |
+
# We'll add a script tag to set a global JS variable before the main script runs
|
51 |
+
js_injection_script = f"""
|
52 |
+
<script>
|
53 |
+
// Set this global variable based on Streamlit state BEFORE the main script runs
|
54 |
+
window.SELECTED_OBJECT_TYPE = {json.dumps(st.session_state.selected_object)};
|
55 |
+
console.log("Streamlit selected object:", window.SELECTED_OBJECT_TYPE);
|
56 |
+
</script>
|
57 |
+
"""
|
58 |
+
# Insert the injection script just before the closing </head> or starting <script type="module">
|
59 |
+
# A simple approach: find the main script tag and insert before it.
|
60 |
+
# More robust: add a placeholder like in index.html
|
61 |
+
# For now, let's find the module script tag:
|
62 |
+
module_script_tag = '<script type="module">'
|
63 |
+
if module_script_tag in html_template:
|
64 |
+
# Inject our script right before the main module script
|
65 |
+
html_content_with_state = html_template.replace(
|
66 |
+
module_script_tag,
|
67 |
+
js_injection_script + "\n" + module_script_tag,
|
68 |
+
1 # Replace only the first occurrence
|
69 |
+
)
|
70 |
+
else:
|
71 |
+
# Fallback: inject before closing head (less ideal if script needs DOM)
|
72 |
+
html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
|
73 |
|
74 |
+
|
75 |
+
# --- Embed HTML Component ---
|
76 |
components.html(
|
77 |
+
html_content_with_state,
|
78 |
+
height=750, # Adjust height as needed
|
79 |
+
scrolling=False
|
80 |
)
|
81 |
|
|
|
|
|
82 |
except FileNotFoundError:
|
83 |
st.error(f"Error: Could not find the file '{html_file_path}'.")
|
84 |
+
st.warning("Please make sure `index.html` is in the same directory as `app.py`.")
|
85 |
except Exception as e:
|
86 |
+
st.error(f"An error occurred: {e}")
|
87 |
+
st.exception(e) # Show full traceback for debugging
|