Save the result of a page when switching pages

Summary

Basically when pressing โ€œokโ€ some functions that contain various instructions like querying APIs, extracting JSON data from APIs, table elaboration, formatting and the like are executed to print the seen result. But when you change pages and return the result generated after pressing โ€œokโ€ is deleted, so you have to enter an ID again and perform another search. Any solution to keep the result from the โ€œMy Farmโ€ page when switching to other pages?

My suggestion is not executing the functions when pressing โ€œokโ€ but when the text_input is not empty.

st.markdown("### ๐Ÿ”Ž Please enter your **farm ID**")
farm_id = st.text_input("#", value="", key="farm_id_input")
if farm_id:
    #Call Functions
    function_example(farm_id)

Like this? And after that how can I keep the result of the page? If the user enters the first character will the function execute automatically or is there a specific trigger?

Well, I thought the value of the text input would be kept when switching pages, but I was wrong. You need to explicitly store it in the session state, which requires some work:

import streamlit as st
from streamlit import session_state as ss

if "farm_id" not in ss:
    ss["farm_id"] = ""

st.markdown("### ๐Ÿ”Ž Please enter your **farm ID**")
ss["farm_id"] = st.text_input("#", value=ss["farm_id"])

farm_id = ss["farm_id"]  # Just so we can type less from now on.
if farm_id:
    #Call Functions
    st.write(f"Your farm: {farm_id}")

I still think the button is just clutter and you donโ€™t need it. Try it out.

1 Like

It worked! Thanks Goyo