import gradio as gr from src.gr_func import init,get_medicines,get_first_material,get_second_material,get_possible_material medicine_list_init = init() def medicine_select_acc_change(medicine_select_acc): medicine_list = get_medicines(medicine_select_acc) return gr.Dropdown.update(choices=medicine_list,value=medicine_list[0]) def run_btn_click(medicine_select,material_1_select,material_2_select): rtn = medicine_select+"\n" possible_material_list = get_possible_material(medicine_select,material_1_select,material_2_select) for index,(main_material,auxi_material,material_third_list) in enumerate(possible_material_list): rtn += f"""### 选择{index+1} - **主药**:{main_material} - **药引**:{",".join(material_third_list)} - **辅药**:{auxi_material} """ return rtn def medicine_select_change(medicine_select): a = get_first_material(medicine_select) return gr.Dropdown.update(choices=["无"]+a,value="无",visible=True),gr.Dropdown.update(visible=True,value="ALL"),gr.Number.update(visible=True,value=5) def material_1_grade_select_change(medicine_select,material_1_grade_select,material_1_num): a = get_first_material(medicine_select,material_1_grade_select,material_1_num) return gr.Dropdown.update(choices=["无"]+a, value="无", visible=True) def material_1_select_change(medicine_select,material_1_select): if material_1_select!="无": a = get_second_material(medicine_select,material_1_select) return gr.Dropdown.update(choices=["无"] + a, value="无", visible=True), gr.Dropdown.update(visible=True,value="ALL"), gr.Number.update(visible=True, value=5) else: return gr.Dropdown.update(choices=["无"], value="无", visible=False), gr.Dropdown.update(visible=False,value="ALL"), gr.Number.update(visible=False, value=5) def material_2_grade_select_change(medicine_select,material_1_select,material_2_grade_select,material_2_num): a = get_second_material(medicine_select,material_1_select,material_2_grade_select,material_2_num) return gr.Dropdown.update(choices=["无"]+a, value="无", visible=True) with gr.Blocks() as demo: gr.Markdown("选择你要炼制的丹药") with gr.Row(): with gr.Column(): with gr.Accordion("丹药限制",open=False): medicine_select_acc = gr.Radio(["ALL","增加修为", "突破概率", "加攻击力"],value="ALL",show_label=False) medicine_select = gr.Dropdown(choices=medicine_list_init,value=medicine_list_init[0],label="丹药选择") with gr.Row(): material_1_grade_select = gr.Dropdown(choices=["ALL"]+[f"{i}品药材" for i in "一二三四五六七八九"],value="ALL",visible=False,label="药材等级") material_1_num = gr.Number(value=5,label="最大数量",visible=False) material_1_select = gr.Dropdown(visible=False,label="第一个药材") with gr.Row(): material_2_grade_select = gr.Dropdown(choices=["ALL"]+[f"{i}品药材" for i in "一二三四五六七八九"],value="ALL",visible=False,label="药材等级") material_2_num = gr.Number(value=5,label="最大数量",visible=False) material_2_select = gr.Dropdown(visible=False,label="第二个药材") run_btn = gr.Button("Run") output_mk = gr.Markdown("输出结果") medicine_select_acc.change(fn=medicine_select_acc_change, inputs=[medicine_select_acc], outputs=[medicine_select]) medicine_select.change(fn=medicine_select_change,inputs=[medicine_select],outputs=[material_1_select,material_1_grade_select,material_1_num]) material_1_grade_select.change(fn=material_1_grade_select_change,inputs=[medicine_select,material_1_grade_select,material_1_num],outputs=[material_1_select]) material_1_num.change(fn=material_1_grade_select_change,inputs=[medicine_select,material_1_grade_select,material_1_num],outputs=[material_1_select]) material_1_select.change(fn=material_1_select_change,inputs=[medicine_select,material_1_select],outputs=[material_2_select,material_2_grade_select,material_2_num]) material_2_grade_select.change(fn=material_2_grade_select_change,inputs=[medicine_select, material_1_select, material_2_grade_select, material_2_num],outputs=[material_2_select]) material_2_num.change(fn=material_2_grade_select_change,inputs=[medicine_select, material_1_select, material_2_grade_select, material_2_num],outputs=[material_2_select]) run_btn.click(fn=run_btn_click,inputs=[medicine_select,material_1_select,material_2_select],outputs=[output_mk]) demo.launch()