Yasu777 commited on
Commit
57fd28b
·
verified ·
1 Parent(s): 4f119a0

Update pages/Artifacts.py

Browse files
Files changed (1) hide show
  1. pages/Artifacts.py +20 -63
pages/Artifacts.py CHANGED
@@ -11,62 +11,34 @@ st.set_page_config(
11
  initial_sidebar_state="expanded"
12
  )
13
 
14
- # Custom Theme and Styling for Dark Mode
15
- st.markdown("""
16
- <style>
17
- /* Dark mode styles */
18
- .main {
19
- background-color: #1e1e1e;
20
- color: #ffffff;
21
- }
22
-
23
- .artifact-container {
24
- background-color: #2d2d2d;
25
- border-radius: 10px;
26
- padding: 20px;
27
- margin: 10px 0;
28
- }
29
-
30
- .artifact-preview {
31
- background-color: #3d3d3d;
32
- border-radius: 5px;
33
- padding: 20px;
34
- margin-top: 10px;
35
- }
36
-
37
- .stTabs [data-baseweb="tab"] {
38
- background-color: #3d3d3d;
39
- color: #ffffff;
40
- }
41
-
42
- .stTabs [data-baseweb="tab-panel"] {
43
- background-color: #2d2d2d;
44
- color: #ffffff;
45
- }
46
-
47
- iframe {
48
- border: 1px solid #4d4d4d;
49
- border-radius: 5px;
50
- }
51
- </style>
52
- """, unsafe_allow_html=True)
53
-
54
  # Initialize React component for previews
55
  react_component = declare_component("react_component", path="frontend/build")
56
 
57
- def render_react_preview(component_code):
58
- """Render React component preview"""
59
  try:
60
- escaped_code = json.dumps(component_code)
61
- return react_component(code=escaped_code, key="preview")
 
 
 
 
 
 
 
 
 
62
  except Exception as e:
63
- st.error(f"Error rendering React component: {str(e)}")
64
- return None
 
 
 
 
65
 
66
  # Main interface
67
  st.title("🎨 Artifacts Viewer")
68
 
69
- # Check if there are any artifacts
70
  if not st.session_state.artifacts:
71
  st.info("No artifacts generated yet. Start a conversation in the Chat page to generate some!")
72
  else:
@@ -87,22 +59,7 @@ else:
87
  preview_tab, code_tab = st.tabs(["Preview", "Code"])
88
 
89
  with preview_tab:
90
- st.markdown('<div class="artifact-preview">', unsafe_allow_html=True)
91
-
92
- if artifact['type'] == "text/html":
93
- # Create responsive container for HTML content
94
- st.markdown(f"""
95
- <div style="position: relative; padding-bottom: 75%; height: 0;">
96
- <iframe src="data:text/html;charset=utf-8,{artifact['content'].replace('"', '&quot;')}"
97
- style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;">
98
- </iframe>
99
- </div>
100
- """, unsafe_allow_html=True)
101
-
102
- elif artifact['type'] == "application/vnd.ant.react":
103
- render_react_preview(artifact['content'])
104
-
105
- st.markdown('</div>', unsafe_allow_html=True)
106
 
107
  with code_tab:
108
  st.code(
 
11
  initial_sidebar_state="expanded"
12
  )
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # Initialize React component for previews
15
  react_component = declare_component("react_component", path="frontend/build")
16
 
17
+ def render_artifact(content, artifact_type):
18
+ """Render an artifact based on its type"""
19
  try:
20
+ if artifact_type == "text/html":
21
+ # Create responsive container for HTML content
22
+ st.markdown(f"""
23
+ <div style="position: relative; padding-bottom: 75%; height: 0;">
24
+ <iframe src="data:text/html;charset=utf-8,{content.replace('"', '&quot;')}"
25
+ style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none;">
26
+ </iframe>
27
+ </div>
28
+ """, unsafe_allow_html=True)
29
+ elif artifact_type == "application/vnd.ant.react":
30
+ react_component(code=content, key=f"preview_{hash(content)}")
31
  except Exception as e:
32
+ st.error(f"Error rendering artifact: {str(e)}")
33
+ st.code(content)
34
+
35
+ # Initialize session state if needed
36
+ if "artifacts" not in st.session_state:
37
+ st.session_state.artifacts = []
38
 
39
  # Main interface
40
  st.title("🎨 Artifacts Viewer")
41
 
 
42
  if not st.session_state.artifacts:
43
  st.info("No artifacts generated yet. Start a conversation in the Chat page to generate some!")
44
  else:
 
59
  preview_tab, code_tab = st.tabs(["Preview", "Code"])
60
 
61
  with preview_tab:
62
+ render_artifact(artifact['content'], artifact['type'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  with code_tab:
65
  st.code(