St.write inside function printed twice

Hi, community!!

I am struggling to introduce st.write inside a function that creates a bridge plot.
I have a function that returns a plotly object, and if some parameters are correct, it prints a statement as well(coded inside the function).
Then, I assign the result to a variable and I call this variable as a plotly graph.
When I call st.plotly_chart(bridge), it returns my statement printed twice and the graphic. I want it printed only one time… It is working fine in a Jupyter Notebook but I can’t achieve it with Streamlit. Do someone know why it is happening and how to solve it?

Thank you so much!

Hey @Gual !

First Welcome to the Streamlit community! :tada: :tada: :tada: :tada: :partying_face:

Can you add a minimum working example of code so I can try to reproduce the error here? Then we can work on coming up with a solution!

Also, what’s your:

  • Streamlit version
  • environment
  • operating system

Thanks!
Marisa

Hi @Marisa_Smith !!

First of all, thank you for your interest!
Trying to put some code to show you I have discovered the issue. The thing is that I used st.write() inside a function that returns a plotly object. I didn’t know that st.write is showed when the function is called, not when st.plotly_chart() is.

For example (a quick example, it doesn’t have to make sense):

import plotly.graph_objects as go
import NumPy as np
import streamlit as st

def function_created():
    st.write('hello Streamlit Community')
    x = np.arange(10)
    fig = go.Figure(data=go.Scatter(x=x, y=x**2))
    return fig
test = function_created()
st.title('Hello World')
st.plotly_chart(test)

In this code, ‘hello Streamlit Community’ will be printed before ‘Hello World’. In my original code, I call the function twice before plotting the chart, I have added a conditional and it’s solved. I am not sure if it is a good practice (st.write inside a function), I think that’s not so good what I am doing, but I couldn’t come up with another solution…

Thank you so much !!

Hey @Gual,

So I am thinking based on this example that to make the st.write() in the function_created() to be after the st.title() you should restructure your code to the title is the first Streamlit call you make! Like so:

import plotly.graph_objects as go
import NumPy as np
import streamlit as st

def function_created():
    st.write('hello Streamlit Community')
    x = np.arange(10)
    fig = go.Figure(data=go.Scatter(x=x, y=x**2))
    return fig
st.title('Hello World') ## <------- notice I have put this before you call 
## your function, to make sure that the title appears before the st.write 
## statement in the function
test = function_created()
st.plotly_chart(test)

Also, if you keep your st.write() call inside your function then it will be printed each time you call it. If I knew why you wanted your st write call in this function I may be able to help you come up with a different solution. Usually when I am designing apps, if I am calling a function I put any screen output calls (like st.write, st.markdown, etc…) just before or after I call the function itself.

Happy Streamlit-ing!
Marisa

1 Like