Spaces:
Running
Running
File size: 3,669 Bytes
96af891 7b02833 96af891 569c20e 7b02833 96af891 7b02833 96af891 3c6ab3c 7b02833 96af891 7b02833 96af891 7b02833 5b1e3c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
from config import BASE_DIR
from utils.file_utils import get_subfolders, get_csv_files
from utils.data_utils import get_quantities_from_csv, display_quantity_info
# Reset logic
def reset_all():
return (
None, # country
None, # state
None, # city
None, # store
None, # category
None, # product
None, # brand
gr.update(choices=[], visible=False), # quantity
"", # result box
{} # state
)
# Main UI builder
def create_navigator_tab():
with gr.TabItem("π§ Navigator"):
with gr.Row():
country = gr.Dropdown(label="π Country", choices=get_subfolders(BASE_DIR), value=None, interactive=True)
state = gr.Dropdown(label="π State", choices=[], interactive=True)
city = gr.Dropdown(label="π City", choices=[], interactive=True)
store = gr.Dropdown(label="πͺ Store", choices=[], interactive=True)
category = gr.Dropdown(label="π Category", choices=[], interactive=True)
product = gr.Dropdown(label="π¦ Product", choices=[], interactive=True)
brand = gr.Dropdown(label="π· Brand", choices=[], interactive=True)
quantity = gr.Dropdown(label="π’ Quantity", choices=[], visible=False, interactive=True)
result = gr.Textbox(label="π Product Info", lines=5)
data_state = gr.State()
reset_btn = gr.Button("π Reset All")
# Dropdown chaining logic
country.change(
lambda c: gr.update(choices=get_subfolders(f"{BASE_DIR}/{c}") if c else [], value=None),
inputs=country, outputs=state
)
state.change(
lambda c, s: gr.update(choices=get_subfolders(f"{BASE_DIR}/{c}/{s}") if c and s else [], value=None),
inputs=[country, state], outputs=city
)
city.change(
lambda c, s, ci: gr.update(choices=get_subfolders(f"{BASE_DIR}/{c}/{s}/{ci}") if c and s and ci else [], value=None),
inputs=[country, state, city], outputs=store
)
store.change(
lambda c, s, ci, st: gr.update(choices=get_subfolders(f"{BASE_DIR}/{c}/{s}/{ci}/{st}") if all([c, s, ci, st]) else [], value=None),
inputs=[country, state, city, store], outputs=category
)
category.change(
lambda c, s, ci, st, cat: gr.update(choices=get_subfolders(f"{BASE_DIR}/{c}/{s}/{ci}/{st}/{cat}") if all([c, s, ci, st, cat]) else [], value=None),
inputs=[country, state, city, store, category], outputs=product
)
product.change(
lambda c, s, ci, st, cat, prod: gr.update(choices=get_csv_files(f"{BASE_DIR}/{c}/{s}/{ci}/{st}/{cat}/{prod}") if all([c, s, ci, st, cat, prod]) else [], value=None),
inputs=[country, state, city, store, category, product], outputs=brand
)
brand.change(
lambda c, s, ci, st, cat, prod, b: get_quantities_from_csv(f"{BASE_DIR}/{c}/{s}/{ci}/{st}/{cat}/{prod}/{b}.csv") if all([c, s, ci, st, cat, prod, b]) else (gr.update(choices=[], visible=False), {}),
inputs=[country, state, city, store, category, product, brand],
outputs=[quantity, data_state]
)
quantity.change(
display_quantity_info,
inputs=[quantity, data_state],
outputs=result
)
reset_btn.click(
reset_all,
inputs=[],
outputs=[
country, state, city, store,
category, product, brand, quantity,
result, data_state
]
)
|