networkx-saas / app.py
LeonceNsh's picture
Update app.py
75a7b9a verified
raw
history blame
3.12 kB
# Gradio app function to update CheckboxGroup and filtered data
def app(selected_country, selected_industry):
investor_list, filtered_data = filter_investors_by_country_and_industry(selected_country, selected_industry)
logger.info("Updating CheckboxGroup and filtered data holder.")
# Use gr.update() to create an update dictionary for the CheckboxGroup
return gr.update(
choices=investor_list,
value=investor_list,
visible=True
), filtered_data
# Gradio Interface
def main():
country_list = ["All"] + sorted(data["Country"].dropna().unique())
industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
# Ensure the default values for dropdowns exist
default_country = "United States" if "United States" in country_list else "All"
default_industry = "Enterprise Tech" if "Enterprise Tech" in industry_list else "All"
logger.info(f"Available countries: {country_list}")
logger.info(f"Available industries: {industry_list}")
with gr.Blocks() as demo:
with gr.Row():
# Set default value for country and industry dropdowns
country_filter = gr.Dropdown(choices=country_list, label="Filter by Country", value=default_country)
industry_filter = gr.Dropdown(choices=industry_list, label="Filter by Industry", value=default_industry)
filtered_investor_list = gr.CheckboxGroup(choices=[], label="Select Investors", visible=False)
graph_output = gr.Plot(label="Venture Network Graph")
valuation_display = gr.Markdown(value="Click on a company node to see its valuation.", label="Company Valuation")
filtered_data_holder = gr.State()
# Event handlers for filters
country_filter.change(
app,
inputs=[country_filter, industry_filter],
outputs=[filtered_investor_list, filtered_data_holder]
)
industry_filter.change(
app,
inputs=[country_filter, industry_filter],
outputs=[filtered_investor_list, filtered_data_holder]
)
# Generate graph when investors are selected
filtered_investor_list.change(
generate_graph,
inputs=[filtered_investor_list, filtered_data_holder],
outputs=graph_output
)
# Handle plot click to display valuation
def display_valuation(plotly_event):
if not plotly_event or "points" not in plotly_event or not plotly_event["points"]:
return "Click on a company node to see its valuation."
point_data = plotly_event["points"][0]
if "customdata" in point_data and point_data["customdata"]:
return f"**Valuation:** {point_data['customdata']}"
return "Click on a company node to see its valuation."
graph_output.events().on_click(
fn=display_valuation,
inputs=[graph_output],
outputs=valuation_display
)
demo.launch()
if __name__ == "__main__":
main()