GithubHelp home page GithubHelp logo

Comments (2)

Mi-La avatar Mi-La commented on June 13, 2024 1

I've found a workaround but still I think it would be better if the widget would behave same as the built-in widgets.

import os
import streamlit as st

from streamlit_ace import st_ace

# create file if doesn't exist
if not os.path.exists("file.txt"):
    open("file.txt", "w").close()

with open("file.txt", "r") as f:
    content = f.read()

# the key must be assigned otherwise it's probably generated from content again an again on each re-run
# and thus the old content is still reloaded from the file
new_content = st_ace(content, key="content")

# after changing the editor's content, whole script will be executed from the top
# the content set to the st_ace won't be used since the key "content" exists in st.session_state
# and to accept a change on re-run the key must be changed
if new_content != content:
    with open("file.txt", "w") as f:
        f.write(new_content)

Note that it doesn't work with st.text_area, instead we must set st.session_state["content"] before the st.text_area is created:

st.session_state["content"] = content
new_content = st.text_area("caption", key="content") # do NOT touch the value here

Also note that if you want to change the editor content on some re-run (e.g. when choosing different file), the key must be changed or removed from st.session_state.

from streamlit-ace.

okld avatar okld commented on June 13, 2024 1

Hello @Mi-La,

Unfortunately it is not an issue of this component, but of streamlit custom components in general. They don't support callbacks or session state binding for now.

This feature was already requested on streamlit repository : streamlit/streamlit#3977

from streamlit-ace.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.