Forum Archive

dialogs.list_dialog can pass a list of dicts, but then what?

Phuket2

@omz, not so important. But dialogs.list_dialog can accept a list of strings or a list of dicts. Reference back to ui.ListDataSource.
It's actually very nice, you can have associate data.

But under normal circumstances is it possible for example to set accessory_action = some_func ?

I have tried various ways. I could not get it. And maybe it's not meant to be, which is ok. But I miss so many things, I had to ask!

Eg code:

# coding: utf-8

import os, dialogs

def files_from_dir(the_dir):
    return  [os.path.join(the_dir, f) for f in os.listdir(the_dir) if os.path.isfile(os.path.join(the_dir, f))]

def show_filename_list(list_of_files, title = 'Choose', multiple = False ):
    new_list = []
    for f in list_of_files:
        p, fn = os.path.split(f)
        new_list.append(dict(title = fn, accessory_type = 'detail_button' ,full_file_name = f)) 

    result = dialogs.list_dialog(title = title, items = new_list, multiple = multiple)
    if result:
        return result['full_file_name']
    else:
        return None

if __name__ == '__main__':
    the_path = os.path.expanduser('~')
    the_path = os.path.join(the_path, 'Documents')

    test_file_list = files_from_dir(the_path)

    print show_filename_list(test_file_list)

omz

No, that's not possible. Basically, the only things you can do by passing a list of dicts to list_dialog are:

  • Associating arbitrary data with an entry (e.g. if you have a list of colors, you might want the title to be "red", but associate a value of "#ff0000" with the selected entry

  • Set an image for an entry, using an 'image' key in the dict. It can either be a ui.Image instance or the name of a built-in image (as a string).

You could of course write your own list_dialog function that would allow this (it's implemented in pure Python after all), but you asked about "normal circumstances". ;)

Phuket2

@omz, thanks. I thought that was going to be case. Just wanted to check.

Phuket2

@omz , maybe you could consider to expose the ui.ListDataSource in the future if you are using it. I know it's meant to be a simple case. But what is nice, if we make just a little more functionality in a dialog and its part of Pythonista, then more easy to share that functionality with the community without extra files etc

omz

@Phuket2 I'll consider that.I guess an easy way to make list_dialog more powerful would be to allow the items argument to be a ui.ListDataSource object instead of a list.

Phuket2

@omz , ok great. As I say, for me the power lies in the fact it's in the app. I know it's easy to roll your own. But the distribution is not easy