okld/streamlit-ace

Component Error Minified Material-UI error #3; visit https://material-ui.com/production-error/?code=3&args[]=red for the full message.

Opened this issue ยท 0 comments

Well the I get the error (not python error, an error displayed as html in the page) Component Error Minified Material-UI error #3; visit https://material-ui.com/production-error/?code=3&args[]=red for the full message. , It must be cuz of making a new st_ace object each type a widget updates.

Also here is a screenshot:
Screenshot_20240706_180743_Chrome

Here is the code I wrote:

import streamlit as st
from streamlit_ace import st_ace, KEYBINDINGS, LANGUAGES, THEMES
import sys
from io import StringIO
from CustomModules.initializer import set_page_info, initial_run

set_page_info("Code Editor", "๐Ÿง‘โ€๐Ÿ’ป")

def app():
    st.title("๐Ÿง‘โ€๐Ÿ’ป Code Editor")
    banned_commands = ["open", "sys", "os"]


    if "sub" not in st.session_state:
        st.session_state.sub = False

    c1, c2 = st.columns([3, 1])
    with c1:
        with st.spinner("Updating Values..."):
            langs = [l.capitalize() for l in LANGUAGES]
            themes = [t.capitalize() for t in THEMES]
            kb = [k.capitalize() for k in KEYBINDINGS]

            lang = c2.selectbox("Language Syntax", options=langs, index=121, help="Select Language Syntax")
            theme = c2.selectbox("Theme", options=themes, index=35, help="Select Editor's Theme")
            keybinding = c2.selectbox("Keybinding mode", options=kb, index=3, help="Colored Syntax Text")
            font_size = c2.slider("Font size", 5, 24, 14, help="Font size of text")
            tab_size = c2.slider("Tab size", 1, 8, 4, help="Size of tabs")
            show_gutter = c2.checkbox("Show gutter", value=True, help="Show line indicators [1, 2, 3, etc.]")

            comment_sign = ""
            if lang.lower() == "python" or lang.lower() == "django":
                comment_sign = "#"
            elif lang.lower() == "csharp" or lang.lower() == "java" or lang.lower() == "javascript" or lang.lower() == "cpp":
                comment_sign = "//"
            else:
                comment_sign = "//"

            placeholder_txt = f"{comment_sign} Write your code here:"

            content = st_ace(
                placeholder=placeholder_txt,
                language=lang.lower(),
                theme=theme.lower(),
                keybinding=keybinding.lower(),
                font_size=font_size,
                tab_size=tab_size,
                show_gutter=show_gutter,
                key="ace_editor_1",
            )
            banned_code = False
            banned_command_used = ""
            for i in banned_commands:
                if i in content:
                    banned_code = True
                    banned_command_used = f"{banned_command_used}, {i}" if banned_command_used != "" else f"{i}"

            def sub_event_change():
                st.session_state.sub = True

            cc1, _, cc3 = st.columns(3)
            sub = cc3.button("๐Ÿ—๏ธ Execute ๐Ÿ—๏ธ", help="Interpret & Parse the code and give output(s).",
                             on_click=sub_event_change)

            suffix = ""
            if lang.lower() == "python" or lang.lower() == "django":
                suffix = ".py"
            elif lang.lower() == "csharp":
                suffix = ".cs"
            elif lang.lower() == "java":
                suffix = ".java"
            elif lang.lower() == "javascript":
                suffix = ".js"
            elif lang.lower() == "c_cpp":
                suffix = ".cpp"
            elif lang.lower() == "json":
                suffix = ".json"
            elif lang.lower() == "yaml":
                suffix = ".yaml"
            elif lang.lower() == "ini":
                suffix = ".ini"
            elif lang.lower() == "toml":
                suffix = ".toml"
            else:
                suffix = f".program_{lang.lower()}"

            if "dwn_sub" not in st.session_state:
                st.session_state.dwn_sub = False

            def u_dwn_sub():
                st.session_state.dwn_sub = True

            dwn_sub = cc1.button("๐Ÿ“ฉ Download ๐Ÿ“ฉ", help="Download your code.", on_click=u_dwn_sub)
            if st.session_state.dwn_sub:
                filename = st.text_input("Enter filename [Without extension]:", placeholder="my_code",
                                         help="Pick a name for your code / file.")
                download_code = st.download_button(
                    label="Download Code",
                    data=str(content),
                    file_name=f"{filename}{suffix}",
                    mime="text/plain",
                    help="Download your code."
                )

            if st.session_state.sub and lang == "Python" and content and not banned_code:
                # Execute the user's code and display the output
                with st.spinner('Executing code...'):
                    # Create a StringIO object to capture the output of the user's code
                    output = StringIO()
                    # Redirect stdout to the StringIO object
                    sys.stdout = output
                    with st.expander("Results", expanded=True):
                        try:
                            exec(content)
                        except Exception as e:
                            st.subheader("**:red[Error]:**")
                            st.exception(e)
                        else:
                            # Display the captured output using st.write()

                            st.subheader("**:green[Output]:**")
                            st.write(output.getvalue())
                        finally:
                            # Reset stdout to its original value
                            sys.stdout = sys.__stdout__

            elif st.session_state.sub and lang != "Python":
                st.warning(icon="โš’๏ธ", body="**Only Python Is Supported!**")

            elif st.session_state.sub and not content:
                st.info(icon="โ„น๏ธ", body="Please provide a code.")

            elif st.session_state.sub and banned_code:
                st.warning(body=f"For security reasons, a script containing **\":red[{banned_command_used}]\"** functions and attributes cannot be executed in the code editor!", icon="โ›”")


initial_run(lambda: app())