Home > Mobile >  Create list of strings for gui from object in python
Create list of strings for gui from object in python

Time:10-24

I'm using PySimpleGUI and I'd like to create dynamic options for a dropdown.

my code:

#in file gui.py
import PySimpleGUI as psg

layout = [[psg.Text('Choose category:', size=(20, 1), font='Lucida', justification='left')],
          [psg.Combo([prep_data.prepare_dropdown_categories()],
                     default_value='All crimes', key='all')],
          [psg.Button('SAVE', font=('Lucida', 12)), psg.Button('CANCEL', font=('Lucida', 12))]]

#in file prep_data.py
def prepare_dropdown_categories():
    categories = []
    fetched_categories = fetch_categories() #this fetches categories from an api, returns json.loads(data)
    for category in fetched_categories:
        categories.append(category['name'])

    return categories

specific data I want(api): https://data.police.uk/api/crime-categories

My outcome is a dropdown with one option storing all 'name' strings:

{name0}{name1}{name2}name3{name4}

and yes, not all of them have curly braces around them... I hope someone knows how to properly do this.

Thank you for the help.

CodePudding user response:

The names are OK - see below. Make sure you get the same list.

import requests

r = requests.get('https://data.police.uk/api/crime-categories')
if r.status_code == 200:
  print([x['name'] for x in r.json()]) 

output

['All crime', 'Anti-social behaviour', 'Bicycle theft', 'Burglary', 'Criminal damage and arson', 'Drugs', 'Other theft', 'Possession of weapons', 'Public order', 'Robbery', 'Shoplifting', 'Theft from the person', 'Vehicle crime', 'Violence and sexual offences', 'Other crime']
  • Related