Streamlit and Python, The problem of not using the return value of the function inside the other function and undesired path visual

Question:

The code:

#Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import fitz 
import io
import streamlit as st

global path_input_0    #HERE ASSIGNMENTS-------------------------------
path_input_0=""
path_input_last=""

def login():
  
    # interface codes for the login screen
    path_input_0 =st.text_input("File Path:", "Please enter the path to the folder containing the Abstract_Account_Web_App.py file")
    st.write(path_input_0)
    global proceed
    if st.button("Proceed"):
        st.session_state["page"] = "main"
        st.balloons()
        st.experimental_rerun()
    return path_input_0


def main():
    # interface codes for the main screen
    
    pathz=path_input_last+"/Bankomat Alışveriş Özeti.pdf"
    st.write("path1:",pathz)
    file_names = [r'%s' % pathz]
    st.write(file_names)


if __name__=='__main__':
               
    if "page" not in st.session_state:
                  st.session_state["page"] = "login"
    
    if st.session_state["page"] == "login":
                 
                  path_input_last=login()
                  st.write("last:",path_input_last)
                  
     
    elif st.session_state["page"] == "main":
                  st.balloons()
                  main()

Main screen: (First login screen, after main screen)
enter image description here

Hello friends, I’m trying to take the path from the login screen and use it on the main screen. The login function returns the path_input_0 from the user. But I can’t get this output and use it in the main function Pathz=path_input_last+“/Bankomat Alışveriş Özeti.pdf”. Main function’s path_input_last comes as null.(“”) So it just prints path1 as /Bankomat Alışveriş Özeti.pdf. I don’t want that. The full file path is not coming.
Secondly, file_names = [r'%s' %pathz] looks weird in the interface. My goal here was to take the file path as a variable and read it properly.

If I call login inside the main function again, I can get the variables I want, but this time the problem of calling the two interfaces together arises. It breaks this as my goal is to fetch the interfaces in order

How Can I solve these problems? Could you explain the solution?

Asked By: murat taşçı

||

Answers:

Note: I had to shrink some parts of the code with so it could be somehow readable and straight forward. Looking at your code, you are calling two functions separately with conditions. when you pay attention to where you called the login() is way at the bottom which is also outside the main(). With your approach, yon can only get the path when you call the login() in the main() and at the right place, otherwise you will have to assign an independent variable and tell login() to pass the path to that variable so that main() will know where to look for the path when it needs it.

list_path_input_last = []

def login():
    ...

    path_input_0 = st.text_input("File Path:", "Please enter the path to the folder containing the Abstract_Account_Web_App.py file")
    if path_input_0 is not None:
        list_path_input_last.append(path_input_0)
    st.write(path_input_0)
    ...

def main(): 
    for path in list_path_input_last:
      
        pathz = path + "/Bankomat Alışveriş Özeti.pdf"
        st.write("path1:",pathz)
        file_names = [r'%s' % pathz]
        st.write(file_names)

login() here is not called in the main() that is why you don’t get the path.

if st.session_state["page"] == "login":
    path_input_last=login() # is not in the main()
    st.write("last:",path_input_last)
Answered By: Jamiu Shaibu
def login():
    # interface codes for the login screen
    # (css,buttons,background,input area etc )
    path_input_0 = st.text_input(
        "File Path:",
        "Please enter the path to the folder containing the Abstract_Account_Web_App.py file",
        key="PATH_INPUT_LAST",
    )
    st.write(path_input_0)
    global proceed
    if st.button("Proceed"):
        st.session_state["page"] = "main"
        st.balloons()
        st.experimental_rerun()



def main():
    
    file0 = st.session_state["PATH_INPUT_LAST"]+"/Bankomat Alışveriş Özeti.csv"
    st.write(file0)
    if(os.path.exists(file0) and os.path.isfile(file0)):
      os.remove(file0)
      print("file was deleted as preprocessing")
    else:
      print("file was not found to delete")
    
    # interface codes for the main screen
    # (css,input,buttons etc.)
    def set_texts(pdf_files:list):
        print("starting to text process")
        #This function reads pdf and gets "CRC-32" components as texts
        for pdf_file in pdf_files:
            with fitz.open(pdf_file) as doc:
                text = ""
                for page in doc:
                    new_text = page.get_text()
                    text += new_text
            
                    return text
  



    pathz = st.session_state["PATH_INPUT_LAST"] + "/Bankomat Alışveriş Özeti.pdf"
    st.write("path1:", pathz)
   
    file_names = r'%s' % pathz
    
    st.write(file_names)
    set_texts([file_names])
   
 


if __name__ == "__main__":
    if "page" not in st.session_state:
        st.session_state["page"] = "login"

    if st.session_state["page"] == "login":
        login()

    elif st.session_state["page"] == "main":
        st.balloons()
        main()
Answered By: murat taşçı