'Return the results of a function (input from tkinter) as new variabels for use going forward

I am trying to create a function in tkinter that takes in some file path of a csv, converts it to json and that json file is then useable as a pandas dataframe (assigned to a variable) moving forward in the program.

def upload_initial(): # command for uploading 3 csv files, running them and creating json files to work with.
    try:
        df1 = upload_entry.get()
        df2 = upload_entry2.get()
        df3 = upload_entry3.get()

        airports_df = pd.read_csv(df1)
        freq_df = pd.read_csv(df2)
        runways_df = pd.read_csv(df3)

        freq_df.to_json("freq.json")
        airports_df.to_json("airports.json")
        runways_df.to_json("runways.json")

        freq_json = pd.read_json("freq.json")
        runways_json = pd.read_json("runways.json")
        airports_json = pd.read_json("airports.json")   

        success_label = tk.Label(text="You have successfully uploaded your files! They have been converted to JSON", foreground='green')
        success_label.pack()

        openMenuNew()

    except FileNotFoundError:
        fileNotFound = tk.Label(text="File does not exist, please enter the full file path", foreground='red')
        fileNotFound.pack()
    
    return freq_json, runways_json, airports_json
    
freq_json, runways_json, airports_json = upload_initial()  

The code above works for:

  1. taking the data set in from the user input
  2. converting it to json and saving it locally
  3. printing the success message
  4. handling the file error

I want to then be able to use the json files (now pandas dataframes after conversion in the function) as a variable moving forward but cant seem to save the variables freq_json, airports_json, runways_json globally so I can then use them in other funtions, access the df etc. How do I save that variable from user input for this purpose?

Essentially, can someone explain how to get it so I could them call airports_json.head() in another cell and return that head?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source