ehsk commited on
Commit
bd8fe60
·
1 Parent(s): 2909da8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -10,12 +10,27 @@ DATA_FILE = "data/gwf_2017-2021_specter2_base.json"
10
  THEMES = {"cluster": "fall", "year": "mint", "source": "phase"}
11
 
12
 
 
 
 
 
 
 
 
13
  def load_df(data_file: os.PathLike):
14
  df = pd.read_json(data_file, orient="records")
15
  df["x"] = df["point2d"].apply(lambda x: x[0])
16
  df["y"] = df["point2d"].apply(lambda x: x[1])
17
  df["year"] = df["year"].replace("", 0)
18
  df["year"] = df["year"].astype(int)
 
 
 
 
 
 
 
 
19
  if "publication_type" in df.columns:
20
  df["type"] = df["publication_type"]
21
  df = df.drop(columns=["point2d", "publication_type"])
@@ -65,7 +80,7 @@ with st.sidebar:
65
  DF.loc[df_mask, "opacity"] = 1.0
66
  st.write(f"Number of points: {DF[df_mask].shape[0]}")
67
 
68
- color = st.selectbox("Color", ("cluster", "source"))
69
 
70
 
71
  fig = px.scatter(
@@ -76,9 +91,12 @@ fig = px.scatter(
76
  color=color,
77
  width=1000,
78
  height=800,
79
- hover_data=["title", "authors", "year", "source", "type"],
80
  color_continuous_scale=THEMES[color],
81
  )
 
 
 
82
  fig.update_layout(
83
  # margin=dict(l=10, r=10, t=10, b=10),
84
  showlegend=False,
@@ -86,8 +104,14 @@ fig.update_layout(
86
  family="Times New Roman",
87
  size=30,
88
  ),
 
 
 
 
 
 
89
  )
90
  fig.update_xaxes(title="")
91
  fig.update_yaxes(title="")
92
 
93
- st.plotly_chart(fig, use_container_width=True)
 
10
  THEMES = {"cluster": "fall", "year": "mint", "source": "phase"}
11
 
12
 
13
+ def decorated_to_string(list_of_things):
14
+ if len(list_of_things) > 2:
15
+ return ", ".join(list_of_things[:-1]) + ", and " + list_of_things[-1]
16
+ else:
17
+ return " and ".join(list_of_things)
18
+
19
+
20
  def load_df(data_file: os.PathLike):
21
  df = pd.read_json(data_file, orient="records")
22
  df["x"] = df["point2d"].apply(lambda x: x[0])
23
  df["y"] = df["point2d"].apply(lambda x: x[1])
24
  df["year"] = df["year"].replace("", 0)
25
  df["year"] = df["year"].astype(int)
26
+
27
+ df["authors_trimmed"] = df.authors.apply(
28
+ lambda row: [(x[x.index(",") + 1:].strip() + " " + x.split(",")[0].strip()) if "," in x else x for x in row]
29
+ )
30
+ df["authors_trimmed"] = df.authors_trimmed.apply(
31
+ lambda row: decorated_to_string(row[:5]) + (" et al." if len(row) > 5 else "")
32
+ )
33
+
34
  if "publication_type" in df.columns:
35
  df["type"] = df["publication_type"]
36
  df = df.drop(columns=["point2d", "publication_type"])
 
80
  DF.loc[df_mask, "opacity"] = 1.0
81
  st.write(f"Number of points: {DF[df_mask].shape[0]}")
82
 
83
+ color = st.selectbox("Color", ("cluster", "year", "source"))
84
 
85
 
86
  fig = px.scatter(
 
91
  color=color,
92
  width=1000,
93
  height=800,
94
+ custom_data=("title", "authors_trimmed", "year", "source", "keywords"),
95
  color_continuous_scale=THEMES[color],
96
  )
97
+ fig.update_traces(
98
+ hovertemplate="<b>%{customdata[0]}</b><br>%{customdata[1]}<br>%{customdata[2]}<br><i>%{customdata[3]}</i><br><i font-size='8'>Keywords: %{customdata[4]}</i>"
99
+ )
100
  fig.update_layout(
101
  # margin=dict(l=10, r=10, t=10, b=10),
102
  showlegend=False,
 
104
  family="Times New Roman",
105
  size=30,
106
  ),
107
+ hoverlabel=dict(
108
+ align="left",
109
+ font_size=14,
110
+ font_family="Rockwell",
111
+ namelength=-1,
112
+ ),
113
  )
114
  fig.update_xaxes(title="")
115
  fig.update_yaxes(title="")
116
 
117
+ st.plotly_chart(fig, use_container_width=True)