from db import MongoDao from data import BrandInfo, ProductInfo import gradio as gr product_dao = MongoDao("vbrand-ec") class Service: product_map = {} @staticmethod def get_brand_list(): """获取数据库中存储的品牌列表""" brand_list = product_dao.get_one_field_data("brandName") brand_list = list(dict.fromkeys(brand_list)) return brand_list @staticmethod def get_init_state_product_list(brand_name): """获取初始状态下的产品列表""" brand_info = BrandInfo(brand_name) prodct_titles = [p.title for p in brand_info.product_list] Service.product_map[brand_name] = brand_info.product_list return prodct_titles @staticmethod def on_brand_change(brand_name): """加载选定品牌的所有商品""" brand_info = BrandInfo(brand_name) produt_titles = [p.title for p in brand_info.product_list] Service.product_map[brand_name] = brand_info.product_list return gr.update(choices=produt_titles, value=produt_titles[0]) @staticmethod def on_product_change(title, brand_name): product_list = Service.product_map.get(brand_name, []) product = next((p for p in product_list if p.title == title), None) if not product: return [], [["商品未找到", ""]] # 加载图片 images = [image_url for image_url in product.iamges] # 产品信息 info = [ ["商品链接", product.url], ["平台", product.platFormName], ["商户名称", product.shopTitle], ["颜色", product.colors], ["尺寸", product.sizes], ["价格", product.price], ] return images, info if __name__ == "__main__": brand_list = Service.get_brand_list() print(brand_list)