pythondearpygui

DearPyGUI: How can I import a simple csv file (using a file dialog run in UI) for use as a variable


For example, the use case would be the user downloading a csv file that their friend made with another copy of my program, finding it from my program using open_file_dialog and then allowing the program to use it as a list variable to create the output.

I currently have the following but when I print data it is a None object

def openFile(self, sender, data):
    open_file_dialog(callback= csvUser, extensions='.csv')
    #ensures only csvs are searched

def csvUser(self, sender, data):
    print(data)

I would like to do the following but it doesn't seem possible:

listObject = open_file_dialog(extensions='.csv')
for i in listObject:
   print(i)

Solution

  • Creating a minimal dearpygui with your pieces:

    from dearpygui.core import *
    from dearpygui.simple import *
    import os
    
    class MyClass:
    
        def openFile(self, sender, data):
            open_file_dialog(callback= self.csvUser, extensions='.csv')
            #ensures only csvs are searched
    
        def csvUser(self, sender, data):
            filename = os.sep.join(data)
            for i in open(filename,"rt"):
                print (i)
    
    myhandler = MyClass()
    
    with window("Open a csv"):
        add_button("Open",callback=myhandler.openFile)
    
    start_dearpygui()