streamlit_helpers.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Streamlit helper functions
  14. """
  15. import streamlit as st
  16. import streamlit.components.v1 as components
  17. from web.i18n import tr
  18. from pixelle_video.config import config_manager
  19. def safe_rerun():
  20. """Safe rerun that works with both old and new Streamlit versions"""
  21. if hasattr(st, 'rerun'):
  22. st.rerun()
  23. else:
  24. st.experimental_rerun()
  25. # ============================================================================
  26. # SelfHost Workflow Warning - Using Native JavaScript Alert
  27. # ============================================================================
  28. # Uses native browser alert() to avoid Streamlit's dialog limitations.
  29. # This is simple, reliable, and works across all browsers.
  30. def check_and_warn_selfhost_workflow(workflow_path: str):
  31. """
  32. Check if user just switched to a selfhost workflow and show JS alert.
  33. Uses native JavaScript alert() which bypasses all Streamlit dialog limitations.
  34. The alert is shown immediately when user switches to a selfhost workflow.
  35. Args:
  36. workflow_path: The workflow path (e.g., "selfhost/image_flux.json")
  37. """
  38. if not workflow_path:
  39. return
  40. # Check if this is a transition TO selfhost
  41. is_selfhost = workflow_path.startswith("selfhost/")
  42. # Only show alert when transitioning TO selfhost
  43. if is_selfhost:
  44. _show_js_alert(workflow_path)
  45. def _show_js_alert(workflow_path: str):
  46. """
  47. Show a native JavaScript alert with selfhost workflow warning.
  48. Args:
  49. workflow_path: The workflow path to display in the alert
  50. """
  51. # Get ComfyUI URL from config
  52. comfyui_config = config_manager.get_comfyui_config()
  53. comfyui_url = comfyui_config.get("comfyui_url", "http://localhost:8188")
  54. # Build alert message
  55. title = tr("selfhost.warning.title")
  56. message = tr("selfhost.warning.message",
  57. comfyui_url=comfyui_url,
  58. workflow_path=f"workflows/{workflow_path}")
  59. hint = tr("selfhost.warning.hint")
  60. # Clean up markdown formatting for plain text alert
  61. # Remove ** (bold markers) and other markdown
  62. message = message.replace("**", "").replace("*", "")
  63. hint = hint.replace("**", "").replace("*", "")
  64. # Combine into single alert message
  65. full_message = f"{title}\\n\\n{message}\\n\\n{hint}"
  66. # Escape for JavaScript string
  67. full_message = full_message.replace("'", "\\'").replace('"', '\\"')
  68. full_message = full_message.replace("\n", "\\n")
  69. # Inject JavaScript alert
  70. js_code = f"""
  71. <script>
  72. alert("{full_message}");
  73. </script>
  74. """
  75. components.html(js_code, height=0, width=0)