I’m working on a Streamlit app where I want to provide users with an editable code block box, similar to the existing Streamlit text box component, but with Python syntax highlighting. This would allow users to input and edit Python code within the app.
I’ve looked at the Streamlit documentation and the available components, but I couldn’t find a direct solution for this specific use case. I would like to add a feature that not only lets users input Python code but also highlights the syntax for better readability.
Is there a recommended approach or a community-contributed solution for adding an editable code block box with syntax highlighting for Python code in Streamlit?
Any insights, tips, or guidance would be greatly appreciated. Thanks in advance for your help!
I would recommend streamlit-code-editor if the look you are going for is like st.code but editable (so not exactly like st.text_area). By default, the code editor will return a response when the user presses Ctrl+Enter just like st.text_area.
Here is the current repo where you will find installation instructions and more:
Another option that hasnt been mentioned yet is the Monaco editor:
Oh also, quick tip if you just started exploring using the code editor. You will get a nicer experience if you set allow_reset=True and give it a fixed key (like say key="my_editor"). This will prevent the editor from being recreated everytime your script re-runs and also allowing you to change/update the arguments to the component.
Do note that if you do the recommended thing and set a fixed key, the response dictionary will persist (because the code editor is the same one that existed prior to re-run) and so the old response sticks around until a new one is sent and the way you can tell if a new response has just been sent is by the id in the dict. Each response has a unique id. Like, for example, each time you press Ctrl+Enter the id value will change. So you can just use this fact to make an if statement that checks if the response is new.
I think a cleaner way to avoid performing an action twice (or more) if the id has not changed is to encapsule the logic to perform in a handle_action function and cache it using the id value of the dict returned by code_editor.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.