# St.form getting displayed before st.title

**URL:** <https://discuss.streamlit.io/t/st-form-getting-displayed-before-st-title/40768>\
**Category:** Using Streamlit\
**Tags:** session-state, cache\
**Created:** [April 5, 2023, 4:23am UTC](https://discuss.streamlit.io/t/st-form-getting-displayed-before-st-title/40768 "2023-04-05T04:23:55Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![snehankekre](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/snehankekre/32/6504_2.png) [@snehankekre](https://discuss.streamlit.io/u/snehankekre)\
**Post date:** [April 6, 2023, 11:26am UTC](https://discuss.streamlit.io/t/st-form-getting-displayed-before-st-title/40768/6 "2023-04-06T11:26:14Z")

</div>

> [@nikhiljha97](#):
>
> Can u please modify my above code if possible?

Rather than writing the entire app for you (which isn’t a [minimal reproducible example](https://discuss.streamlit.io/t/using-streamlit-how-to-post-a-question-in-the-streamlit-forum/30960) and not fun 😉), we can give you pointers like the one below:

> [@St.form getting displayed before st.title](https://discuss.streamlit.io/t/st-form-getting-displayed-before-st-title/40768/2):
>
> Callback functions act as a prefix to the page run. If you render anything within a callback, it is going to get prepended to the next page load. If you don’t want this, then you can have your callback write information into session state and then retrieve it later on the page.

Here are two minimal examples that demonstrate the current undesired behavior, and how to fix it:

1. The table is displayed at the top of the app 👎

```python
import streamlit as st
import pandas as pd

def show_df():
    # This callback is executed before the rest of the code
    # so the table is displayed before the subheader and so on
    st.table(pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}))

st.subheader("This is a subheader")

with st.form("Form"):
    text = st.text_input("Text input", key="text")
    btn = st.form_submit_button("Submit", on_click=show_df)

```

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/2/c/2cf4b6259a2603f780cec390ee6784d015ff8f21.png)

1. Table is shown below the form submit button 👍

```python
import streamlit as st
import pandas as pd

def show_df():
    # Write dataframe to session state
    st.session_state.df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

st.subheader("This is a subheader")

with st.form("Form"):
    text = st.text_input("Text input", key="text")
    btn = st.form_submit_button("Submit", on_click=show_df)

if btn:
    # display dataframe from session state upon submitting the form
    st.table(st.session_state.df)

```

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/e/e/ee5935b9794dfc7761eca53dd834b8834687e73e.png)

You should be able to use the above concept and adapt it to your application. 🎈

[edit] If I had to guess (and this may not work since your code isn’t reproducible), this could work:

```python
def show_df():
    df_pred.drop(index=[1,2],inplace=True)
    df_pred.set_index('Strain',inplace=True)
    df2 = style_df(df_pred)
    st.session_state.df2 = df2
with st.form("Show what you have"):
    submit = st.form_submit_button(on_click=show_df)

if submit:
    st.table(st.session_state.df2)

```

---

_[View the full topic](https://discuss.streamlit.io/t/st-form-getting-displayed-before-st-title/40768)._
