Skip to content

Conversation

@sfc-gh-bhess
Copy link
Contributor

📚 Context

Please describe the project or issue background here
st.echo() has a simplistic approach to figuring out what to consider inside the echo(). It actually has issues with some use cases entirely, such as:

with st.echo(
    location="below"
): 
    st.markdown("# Hiya")

Instead, we should use ast to properly parse the file and output the proper full with st_echo() block.

  • What kind of change does this PR introduce?

    • Bugfix
    • Feature
    • Refactoring
    • Other, please describe:

🧠 Description of Changes

  • Add bullet points summarizing your changes here

  • use ast to parse the file and find the block for the st.echo() properly

    • This is a breaking API change
    • This is a visible (user-facing) change

Revised:

Insert screenshot of your updated UI/code here

Current:

Insert screenshot of existing UI/code here

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests
  • Added/Updated e2e tests

🌐 References

Does this depend on other work, documents, or tickets?

  • Issue: Closes #XXXX

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@sfc-gh-bhess sfc-gh-bhess requested a review from a team September 19, 2022 18:48
@stale
Copy link

stale bot commented Oct 4, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 4, 2022
@sfc-gh-kbregula
Copy link
Contributor

Do you think we should add tests for this change?

@stale stale bot removed the stale label Oct 4, 2022
@sfc-gh-bhess
Copy link
Contributor Author

The fix replaces the logic for how to determine the code to be echoed. The existing st.echo tests should cover the fix, no?

@blackary
Copy link
Collaborator

blackary commented Oct 4, 2022

@sfc-gh-brianhess I don't think there are any test cases that cover the case you're fixing, so it seems like it would be good to add one to https://github.com/streamlit/streamlit/blob/develop/lib/tests/streamlit/elements/echo_test.py

@sfc-gh-bhess
Copy link
Contributor Author

Added test case

@lukasmasuch lukasmasuch removed their request for review October 10, 2022 18:08
@lukasmasuch lukasmasuch requested a review from kajarenc October 20, 2022 18:30
@kajarenc
Copy link
Collaborator

Hello, Brain [ @sfc-gh-brianhess ], thank you very much for your contribution, it really looks great!

Unfortunately end_lineno attribute of ast node was added only in Python 3.8.
https://docs.python.org/3/whatsnew/3.8.html#ast

We should either wait until we stop supporting Python 3.7 for Streamlit, or have a decision to maintain different behavior based on the Python version for st.echo widget.

@stale
Copy link

stale bot commented Nov 4, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 4, 2022
@kajarenc kajarenc closed this Nov 11, 2022
@blackary blackary reopened this Jun 27, 2023
@blackary
Copy link
Collaborator

Since we're now no longer officially supporting 3.7, this should be good to go, right @kajarenc?

@kajarenc
Copy link
Collaborator

@blackary yes, I looked at this PR just after removing 3.7 from the list

now we have some conflicts in the tests with the current develop, so we couldn't just merge these changes as is.

But I definitely take a look at it again in the near future, and will try to resolve conflicts and merge it!

@kajarenc kajarenc added security-assessment-completed Security assessment has been completed for PR impact:internal PR changes only affect internal code change:chore PR contains maintenance or housekeeping change labels Jun 29, 2023
@AnOctopus AnOctopus self-requested a review June 29, 2023 18:19

ap = ast.parse("".join(source_lines))

ap_map = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose/use of this map is not very clear from the name

def do_y(self):
pass"""

element = self.get_delta_from_queue(echo_index).new_element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests using get_delta_from_queue could also be written with the interactive script tests framework, which might be clearer, though this is not required.

Copy link
Contributor

@AnOctopus AnOctopus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST traversal code isn't the most obvious (though it wasn't that hard to follow after I pulled up the ast module docs), so could benefit from better naming and/or explanatory comments.

@kajarenc kajarenc merged commit 92fdb9f into streamlit:develop Jun 30, 2023
tconkling added a commit to tconkling/streamlit that referenced this pull request Jul 10, 2023
* develop:
  Respect current existing env var in build_info.py (streamlit#6906)
  fix: frontend/package.json & frontend/yarn.lock to reduce vulnerabilities (streamlit#6959)
  Update hamburger menu icon to overflow (streamlit#6947)
  Fix: description of config options (streamlit#6917)
  Add missing border radius (streamlit#6944)
  Up version to 1.24.0 (streamlit#6905)
  modify st.echo to use ast to handle indenting properly (streamlit#5375)
asmeralt pushed a commit to asmeralt/streamlit that referenced this pull request Sep 29, 2025
st.echo() has a simplistic approach to figuring out what to consider inside the echo(). It actually has issues with some use cases entirely, such as:

with st.echo(
    location="below"
): 
    st.markdown("# Hiya")
Instead, we should use ast to properly parse the file and output the proper full with st_echo() block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:chore PR contains maintenance or housekeeping change impact:internal PR changes only affect internal code security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants