RetailGenie / ui /navigator_tab.py
Parishri07's picture
Update ui/navigator_tab.py
3c6ab3c verified
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
]
)