adrianpierce commited on
Commit
b9fc179
1 Parent(s): 7932995

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import plotly.graph_objects as go
3
+ import networkx as nx
4
+ import pandas as pd
5
+
6
+ # Load the CSV file
7
+ df = pd.read_csv('/spices_by_cuisine_with_all_flavors.csv', index_col=0)
8
+
9
+ # Create a graph
10
+ G = nx.Graph()
11
+
12
+ # Add nodes for each cuisine and spice, and edges based on the DataFrame
13
+ for col in df.columns:
14
+ if col != "Flavor Description":
15
+ G.add_node(col, type='cuisine')
16
+ spices_for_cuisine = df[df[col] == 1].index.tolist()
17
+ for spice in spices_for_cuisine:
18
+ G.add_node(spice, type='spice')
19
+ G.add_edge(col, spice)
20
+
21
+ # Get node positions using the spring layout
22
+ pos = nx.spring_layout(G)
23
+
24
+ # Create edge trace
25
+ edge_trace = go.Scatter(
26
+ x=[],
27
+ y=[],
28
+ line=dict(width=0.5, color='#888'),
29
+ hoverinfo='none',
30
+ mode='lines')
31
+
32
+ for edge in G.edges():
33
+ x0, y0 = pos[edge[0]]
34
+ x1, y1 = pos[edge[1]]
35
+ edge_trace['x'] += tuple([x0, x1, None])
36
+ edge_trace['y'] += tuple([y0, y1, None])
37
+
38
+ # Assign a unique color to each cuisine
39
+ cuisine_colors = {cuisine: f"hsl({i * (360 // len(df.columns[:-1]))}, 80%, 50%)"
40
+ for i, cuisine in enumerate(df.columns) if cuisine != "Flavor Description"}
41
+
42
+
43
+ # Create node trace for cuisines
44
+ node_trace_cuisines = go.Scatter(
45
+ x=[],
46
+ y=[],
47
+ text=[],
48
+ hovertext=[],
49
+ mode='markers+text',
50
+ hoverinfo='text',
51
+ marker=dict(
52
+ showscale=False,
53
+ size=20,
54
+ color=[],
55
+ line=dict(width=0)))
56
+
57
+ # Create node trace for spices
58
+ node_trace_spices = go.Scatter(
59
+ x=[],
60
+ y=[],
61
+ text=[],
62
+ hovertext=[],
63
+ mode='markers+text',
64
+ hoverinfo='text',
65
+ marker=dict(
66
+ showscale=False,
67
+ color='grey',
68
+ size=10,
69
+ line=dict(width=0)))
70
+
71
+ for node in G.nodes():
72
+ x, y = pos[node]
73
+ if G.nodes[node]['type'] == 'cuisine':
74
+ node_trace_cuisines['x'] += tuple([x])
75
+ node_trace_cuisines['y'] += tuple([y])
76
+ node_trace_cuisines['text'] += tuple([node])
77
+ node_trace_cuisines['marker']['color'] += tuple([cuisine_colors[node]])
78
+
79
+ # Collect all spices associated with this cuisine
80
+ spices_associated = df[df[node] == 1].index.tolist()
81
+ hover_text = f"{node} uses: {', '.join(spices_associated)}"
82
+ node_trace_cuisines['hovertext'] += tuple([hover_text])
83
+
84
+ else:
85
+ node_trace_spices['x'] += tuple([x])
86
+ node_trace_spices['y'] += tuple([y])
87
+ node_trace_spices['text'] += tuple([node])
88
+
89
+ # Collect all cuisines that use this spice
90
+ cuisines_using_spice = df.columns[df.loc[node] == 1].tolist()
91
+ hover_text = f"{node} is used in: {', '.join(cuisines_using_spice)}"
92
+ node_trace_spices['hovertext'] += tuple([hover_text])
93
+
94
+ # Create the network graph figure with updated hover information
95
+ fig = go.Figure(data=[edge_trace, node_trace_cuisines, node_trace_spices],
96
+ layout=go.Layout(
97
+ title="Network Graph of Cuisines and their Spices",
98
+ titlefont_size=16,
99
+ showlegend=False,
100
+ hovermode='closest',
101
+ margin=dict(b=20, l=5, r=5, t=40),
102
+ xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
103
+ yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
104
+ )
105
+
106
+ st.plotly_chart(fig, use_container_width=True)