ahuang11 commited on
Commit
fd51647
1 Parent(s): e9c905e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -8
app.py CHANGED
@@ -11,6 +11,15 @@ from langchain.chat_models import ChatOpenAI
11
  pn.extension(sizing_mode="stretch_width", notifications=True)
12
  hv.extension("bokeh")
13
 
 
 
 
 
 
 
 
 
 
14
 
15
  RANDOM_NAME_QUERY = """
16
  SELECT name, count,
@@ -73,13 +82,14 @@ class StreamHandler(BaseCallbackHandler):
73
 
74
 
75
  class NameChronicles:
76
- def __init__(self, refresh=False):
77
  super().__init__()
78
  self.db_path = Path("data/names.db")
79
- self._initialize_database(refresh=refresh)
80
 
81
  # Main
82
- self.holoviews_pane = pn.pane.HoloViews(sizing_mode="stretch_both")
 
 
83
  self.selection = hv.streams.Selection1D()
84
 
85
  # Sidebar
@@ -94,7 +104,6 @@ class NameChronicles:
94
  solid=False,
95
  )
96
  self.names_choice.param.watch(self._update_plot, "value")
97
- self.names_choice.value = ["Andrew"]
98
 
99
  # Reset Widgets
100
  self.clear_button = pn.widgets.Button(
@@ -168,9 +177,11 @@ class NameChronicles:
168
  title="Ask AI",
169
  )
170
 
 
 
171
  # Database Methods
172
-
173
- def _initialize_database(self, refresh):
174
  """
175
  Initialize database with data from the Social Security Administration.
176
  """
@@ -196,6 +207,12 @@ class NameChronicles:
196
  self.conn.execute("DROP TABLE IF EXISTS names")
197
  self.conn.execute("CREATE TABLE names AS SELECT * FROM df_processed")
198
 
 
 
 
 
 
 
199
  def _query_names(self, names):
200
  """
201
  Query the database for the given name.
@@ -438,6 +455,7 @@ class NameChronicles:
438
  align="end",
439
  )
440
  sidebar = pn.Column(
 
441
  self.names_input,
442
  self.names_choice,
443
  reset_row,
@@ -446,13 +464,16 @@ class NameChronicles:
446
  self.ai_pane,
447
  data_url,
448
  )
 
 
 
449
  template = pn.template.FastListTemplate(
450
  sidebar=[sidebar],
451
- main=[self.holoviews_pane],
452
  title="Name Chronicles",
453
  theme="dark",
454
  )
455
  return template
456
 
457
 
458
- NameChronicles().view().servable()
 
11
  pn.extension(sizing_mode="stretch_width", notifications=True)
12
  hv.extension("bokeh")
13
 
14
+ INSTRUCTIONS = """
15
+ #### Name Chronicles lets you explore the history of names in the United States.
16
+ - Enter a name to add to plot.
17
+ - See stats by hovering a line.
18
+ - Click on a line to see the gender distribution.
19
+ - Get a random name based on selected criteria.
20
+ - Ask AI for some background info on a name.
21
+ - Have ideas? [Open an issue](https://github.com/ahuang11/name-chronicles/issues).
22
+ """
23
 
24
  RANDOM_NAME_QUERY = """
25
  SELECT name, count,
 
82
 
83
 
84
  class NameChronicles:
85
+ def __init__(self):
86
  super().__init__()
87
  self.db_path = Path("data/names.db")
 
88
 
89
  # Main
90
+ self.holoviews_pane = pn.pane.HoloViews(
91
+ min_height=675, sizing_mode="stretch_both"
92
+ )
93
  self.selection = hv.streams.Selection1D()
94
 
95
  # Sidebar
 
104
  solid=False,
105
  )
106
  self.names_choice.param.watch(self._update_plot, "value")
 
107
 
108
  # Reset Widgets
109
  self.clear_button = pn.widgets.Button(
 
177
  title="Ask AI",
178
  )
179
 
180
+ pn.state.onload(self._initialize_database)
181
+
182
  # Database Methods
183
+
184
+ def _initialize_database(self):
185
  """
186
  Initialize database with data from the Social Security Administration.
187
  """
 
207
  self.conn.execute("DROP TABLE IF EXISTS names")
208
  self.conn.execute("CREATE TABLE names AS SELECT * FROM df_processed")
209
 
210
+ if self.names_choice.value == []:
211
+ self.names_choice.value = ["Andrew"]
212
+ else:
213
+ self.names_choice.param.trigger("value")
214
+ self.main.objects = [self.holoviews_pane]
215
+
216
  def _query_names(self, names):
217
  """
218
  Query the database for the given name.
 
455
  align="end",
456
  )
457
  sidebar = pn.Column(
458
+ INSTRUCTIONS,
459
  self.names_input,
460
  self.names_choice,
461
  reset_row,
 
464
  self.ai_pane,
465
  data_url,
466
  )
467
+ self.main = pn.Column(
468
+ pn.widgets.StaticText(value="Loading, this may take a few seconds...", sizing_mode="stretch_both"),
469
+ )
470
  template = pn.template.FastListTemplate(
471
  sidebar=[sidebar],
472
+ main=[self.main],
473
  title="Name Chronicles",
474
  theme="dark",
475
  )
476
  return template
477
 
478
 
479
+ NameChronicles().view().servable()