Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -169,20 +169,12 @@ class NameChronicles:
|
|
169 |
)
|
170 |
|
171 |
# Database Methods
|
172 |
-
|
173 |
-
def _connect_database(self):
|
174 |
-
"""
|
175 |
-
Connect to the database.
|
176 |
-
"""
|
177 |
-
return duckdb.connect(database=str(self.db_path))
|
178 |
-
|
179 |
def _initialize_database(self, refresh):
|
180 |
"""
|
181 |
Initialize database with data from the Social Security Administration.
|
182 |
"""
|
183 |
-
|
184 |
-
return
|
185 |
-
|
186 |
df = pd.concat(
|
187 |
[
|
188 |
pd.read_csv(
|
@@ -201,9 +193,8 @@ class NameChronicles:
|
|
201 |
.rename(columns={"F": "female", "M": "male"})
|
202 |
.fillna(0)
|
203 |
)
|
204 |
-
|
205 |
-
|
206 |
-
conn.execute("CREATE TABLE names AS SELECT * FROM df_processed")
|
207 |
|
208 |
def _query_names(self, names):
|
209 |
"""
|
@@ -216,19 +207,18 @@ class NameChronicles:
|
|
216 |
top_names_query = TOP_NAMES_WILDCARD_QUERY
|
217 |
else:
|
218 |
top_names_query = TOP_NAMES_SELECT_QUERY
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
df = conn.execute(data_query, top_names).fetch_df()
|
232 |
dfs.append(df)
|
233 |
|
234 |
if len(dfs) > 0:
|
@@ -241,21 +231,20 @@ class NameChronicles:
|
|
241 |
# Widget Methods
|
242 |
|
243 |
def _randomize_name(self, event):
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
RANDOM_NAME_QUERY, [name_pattern, *count_range, gender_select]
|
255 |
-
)
|
256 |
-
.fetch_df()["name"]
|
257 |
-
.tolist()
|
258 |
)
|
|
|
|
|
|
|
259 |
if random_names:
|
260 |
for i in range(len(random_names)):
|
261 |
random_name = random_names[i]
|
@@ -466,4 +455,4 @@ class NameChronicles:
|
|
466 |
return template
|
467 |
|
468 |
|
469 |
-
NameChronicles().view().servable()
|
|
|
169 |
)
|
170 |
|
171 |
# Database Methods
|
172 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
def _initialize_database(self, refresh):
|
174 |
"""
|
175 |
Initialize database with data from the Social Security Administration.
|
176 |
"""
|
177 |
+
self.conn = duckdb.connect(":memory:")
|
|
|
|
|
178 |
df = pd.concat(
|
179 |
[
|
180 |
pd.read_csv(
|
|
|
193 |
.rename(columns={"F": "female", "M": "male"})
|
194 |
.fillna(0)
|
195 |
)
|
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 |
"""
|
|
|
207 |
top_names_query = TOP_NAMES_WILDCARD_QUERY
|
208 |
else:
|
209 |
top_names_query = TOP_NAMES_SELECT_QUERY
|
210 |
+
top_names = (
|
211 |
+
self.conn.execute(top_names_query, [name.lower()])
|
212 |
+
.fetch_df()["name"]
|
213 |
+
.tolist()
|
214 |
+
)
|
215 |
+
if len(top_names) == 0:
|
216 |
+
pn.state.notifications.info(f"No names found matching {name!r}")
|
217 |
+
continue
|
218 |
+
data_query = DATA_QUERY.format(
|
219 |
+
placeholders=", ".join(["?"] * len(top_names))
|
220 |
+
)
|
221 |
+
df = self.conn.execute(data_query, top_names).fetch_df()
|
|
|
222 |
dfs.append(df)
|
223 |
|
224 |
if len(dfs) > 0:
|
|
|
231 |
# Widget Methods
|
232 |
|
233 |
def _randomize_name(self, event):
|
234 |
+
name_pattern = self.name_pattern.value.lower()
|
235 |
+
if not name_pattern:
|
236 |
+
name_pattern = "%"
|
237 |
+
else:
|
238 |
+
name_pattern = name_pattern.replace("*", "%")
|
239 |
+
count_range = self.count_range.value
|
240 |
+
gender_select = self.gender_select.value.lower()
|
241 |
+
random_names = (
|
242 |
+
self.conn.execute(
|
243 |
+
RANDOM_NAME_QUERY, [name_pattern, *count_range, gender_select]
|
|
|
|
|
|
|
|
|
244 |
)
|
245 |
+
.fetch_df()["name"]
|
246 |
+
.tolist()
|
247 |
+
)
|
248 |
if random_names:
|
249 |
for i in range(len(random_names)):
|
250 |
random_name = random_names[i]
|
|
|
455 |
return template
|
456 |
|
457 |
|
458 |
+
NameChronicles().view().servable()
|