返回 Pixelle-Video
streamlit_helpers.py
根目录 / web / utils / streamlit_helpers.py
1 # Copyright (C) 2025 AIDC-AI
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
12
13 """
14 Streamlit helper functions
15 """
16
17 import streamlit as st
18 import streamlit.components.v1 as components
19
20 from web.i18n import tr
21 from pixelle_video.config import config_manager
22
23
24 def safe_rerun():
25 """Safe rerun that works with both old and new Streamlit versions"""
26 if hasattr(st, 'rerun'):
27 st.rerun()
28 else:
29 st.experimental_rerun()
30
31
32 # ============================================================================
33 # SelfHost Workflow Warning - Using Native JavaScript Alert
34 # ============================================================================
35 # Uses native browser alert() to avoid Streamlit's dialog limitations.
36 # This is simple, reliable, and works across all browsers.
37
38 def check_and_warn_selfhost_workflow(workflow_path: str):
39 """
40 Check if user just switched to a selfhost workflow and show JS alert.
41
42 Uses native JavaScript alert() which bypasses all Streamlit dialog limitations.
43 The alert is shown immediately when user switches to a selfhost workflow.
44
45 Args:
46 workflow_path: The workflow path (e.g., "selfhost/image_flux.json")
47 """
48 if not workflow_path:
49 return
50
51 # Check if this is a transition TO selfhost
52 is_selfhost = workflow_path.startswith("selfhost/")
53
54 # Only show alert when transitioning TO selfhost
55 if is_selfhost:
56 _show_js_alert(workflow_path)
57
58
59 def _show_js_alert(workflow_path: str):
60 """
61 Show a native JavaScript alert with selfhost workflow warning.
62
63 Args:
64 workflow_path: The workflow path to display in the alert
65 """
66 # Get ComfyUI URL from config
67 comfyui_config = config_manager.get_comfyui_config()
68 comfyui_url = comfyui_config.get("comfyui_url", "http://localhost:8188")
69
70 # Build alert message
71 title = tr("selfhost.warning.title")
72 message = tr("selfhost.warning.message",
73 comfyui_url=comfyui_url,
74 workflow_path=f"workflows/{workflow_path}")
75 hint = tr("selfhost.warning.hint")
76
77 # Clean up markdown formatting for plain text alert
78 # Remove ** (bold markers) and other markdown
79 message = message.replace("**", "").replace("*", "")
80 hint = hint.replace("**", "").replace("*", "")
81
82 # Combine into single alert message
83 full_message = f"{title}\\n\\n{message}\\n\\n{hint}"
84
85 # Escape for JavaScript string
86 full_message = full_message.replace("'", "\\'").replace('"', '\\"')
87 full_message = full_message.replace("\n", "\\n")
88
89 # Inject JavaScript alert
90 js_code = f"""
91 <script>
92 alert("{full_message}");
93 </script>
94 """
95
96 components.html(js_code, height=0, width=0)
97
98
98 lines PYTHON