Skip to content

HTML comments in Python docstring are not removed when rendering by streamlit #1

Open

Description

@nukopy

Problem

HTML comments like <!-- md --> in Python docstring are not removed when rendering by streamlit. This is different than what I was expecting. We need investigate how Python docstring is rendered by streamlit.

This promblem is like below:

demo-streamlit

Solution

Maybe, we can find the solution to this problem by reading a part of docstring parser in streamlit's implementation:

In addition, docstirng is treated as "magic command" in streamlit. May be there's a hint of a solution in streamlit's documentation.

How it works is simple: any time Streamlit sees either a variable or literal value on its own line, it automatically writes that to your app using st.write (which you’ll learn about later).

Screen Shot 2020-06-10 at 16 38 22

Activity

  1. nukopy commented on Jun 10, 2020

    @nukopy
    OwnerAuthor

    A problem in this issue is by design for security, HTML.

    After Python docstring is parsed, it is passed to streamlit.write(body: str, unsafe_allow_html).

    write(string) :Prints the formatted Markdown string, with support for LaTeX expression and emoji shortcodes. See docs for st.markdown for more.

    Description of unsafe_allow_html is below:

    • unsafe_allow_html (bool) –
      This is a keyword-only argument that defaults to False.

    By default, any HTML tags found in strings will be escaped and therefore treated as pure text. This behavior may be turned off by setting this argument to True.

    That said, we strongly advise against it*. It is hard to write secure HTML, so by using this argument you may be compromising your users’ security. For more information, see:

    streamlit/issues/152

    Also note that unsafe_allow_html is a temporary measure and may be removed from Streamlit at any time.

    If you decide to turn on HTML anyway, we ask you to please tell us your exact use case here: https://discuss.streamlit.io/t/96.

    This will help us come up with safe APIs that allow you to do what you want.

    According to the above, HTML tags are escaped by default, so comments like <!-- md -->, <!-- end-md --> in Python docstring are treated as pure text and rendered as pure text.

    Is it better to solve this issue in Python side, not an extension side?

  2. TezRomacH commented on Jul 18, 2021

    @TezRomacH

    Hi, @nukopy ! How is it going on this fix?

  3. TezRomacH commented on Jul 18, 2021

    @TezRomacH

    for now, I tried using a solution like

    from functools import partial
    import streamlit as st
    
    write_markdown = partial(st.write, unsafe_allow_html=True)
    
    write_markdown(""" <!-- md --> 
    ### Step 1: See your app
    <!-- end-md --> 
    """)

    but the extension isn't working inside functions
    Screenshot 2021-07-19 at 00 41 04
    Screenshot 2021-07-19 at 00 41 09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions