Search code examples
pythonvue.jsvuetify.jsipyvuetify

ipyvuetify FileInput Widget - retrieving file input


I am currently trying to use an ipyvuetify FileInput field. My expectation was that if I were to define v_model, that I would be then able to read the selected file from filefield via the v_model attribute.

import ipyvuetify as v

def file_selected(x,y,z):
    print(filefield.v_model) # value is {}
    print(x)   # value is FileInput(placeholder='Select an file...', v_model={})
    print(y)   # value is 'change'
    print(z)   # value is {}

filefield = v.FileInput(v_model='', placeholder = 'Select an file...')
filefield.on_event('change', file_selected)

What I find when I execute this code however, is that filefield.v_model is an empty dictionary. There are three arguments passed to the 'change' event, but none of them seem to contain 'what' file has been selected. How do you get the selected file from a ipyvuetify FileInput?


Solution

  • I had a similar issue. The default method of ipyvuetify.FileInput doesn't seem to work. You have to use ipyvuetify.extra.FileInput. Here is an example which is close to what you are trying:

    from ipyvuetify.extra import FileInput
    import ipywidgets as widgets
    
    fi = FileInput()
    
    # reports value when finished
    def on_file_upload(change):
        file = fi.get_files()[0]
        print(file)
    
    fi.observe(on_file_upload, names='file_info')
    

    Or if you just want the file name after the dialog is finished, use:
    filename = fi.get_files()[0]['name']

    Credit to: github.com/widgetti/ipyvuetify/issues/50