@@ -335,6 +335,72 @@ async def display_columns_and_rows(
335335 return columns , cell_rows
336336
337337
338+ async def expand_labels (
339+ datasette ,
340+ db ,
341+ request ,
342+ table_name ,
343+ columns ,
344+ rows ,
345+ default_labels ,
346+ ):
347+ """Expands labeled columns if requested"""
348+ expanded_columns = []
349+ # List of (fk_dict, label_column-or-None) pairs for that table
350+ expandable_columns = []
351+ for fk in await db .foreign_keys_for_table (table_name ):
352+ label_column = await db .label_column_for_table (fk ["other_table" ])
353+ expandable_columns .append ((fk , label_column ))
354+
355+ columns_to_expand = None
356+ try :
357+ all_labels = value_as_boolean (request .args .get ("_labels" , "" ))
358+ except ValueError :
359+ all_labels = default_labels
360+ # Check for explicit _label=
361+ if "_label" in request .args :
362+ columns_to_expand = request .args .getlist ("_label" )
363+ if columns_to_expand is None and all_labels :
364+ # expand all columns with foreign keys
365+ columns_to_expand = [fk ["column" ] for fk , _ in expandable_columns ]
366+
367+ if columns_to_expand :
368+ expanded_labels = {}
369+ for fk , _ in expandable_columns :
370+ column = fk ["column" ]
371+ if column not in columns_to_expand :
372+ continue
373+ if column not in columns :
374+ continue
375+ expanded_columns .append (column )
376+ # Gather the values
377+ column_index = columns .index (column )
378+ values = [row [column_index ] for row in rows ]
379+ # Expand them
380+ expanded_labels .update (
381+ await datasette .expand_foreign_keys (
382+ request .actor , db .name , table_name , column , values
383+ )
384+ )
385+ if expanded_labels :
386+ # Rewrite the rows
387+ new_rows = []
388+ for row in rows :
389+ new_row = CustomRow (columns )
390+ for column in row .keys ():
391+ value = row [column ]
392+ if (column , value ) in expanded_labels and value is not None :
393+ new_row [column ] = {
394+ "value" : value ,
395+ "label" : expanded_labels [(column , value )],
396+ }
397+ else :
398+ new_row [column ] = value
399+ new_rows .append (new_row )
400+ rows = new_rows
401+ return rows , expanded_columns
402+
403+
338404class TableInsertView (BaseView ):
339405 name = "table-insert"
340406
@@ -1210,59 +1276,15 @@ async def table_view_data(
12101276 rows = list (results .rows )
12111277
12121278 # Expand labeled columns if requested
1213- expanded_columns = []
1214- # List of (fk_dict, label_column-or-None) pairs for that table
1215- expandable_columns = []
1216- for fk in await db .foreign_keys_for_table (table_name ):
1217- label_column = await db .label_column_for_table (fk ["other_table" ])
1218- expandable_columns .append ((fk , label_column ))
1219-
1220- columns_to_expand = None
1221- try :
1222- all_labels = value_as_boolean (request .args .get ("_labels" , "" ))
1223- except ValueError :
1224- all_labels = default_labels
1225- # Check for explicit _label=
1226- if "_label" in request .args :
1227- columns_to_expand = request .args .getlist ("_label" )
1228- if columns_to_expand is None and all_labels :
1229- # expand all columns with foreign keys
1230- columns_to_expand = [fk ["column" ] for fk , _ in expandable_columns ]
1231-
1232- if columns_to_expand :
1233- expanded_labels = {}
1234- for fk , _ in expandable_columns :
1235- column = fk ["column" ]
1236- if column not in columns_to_expand :
1237- continue
1238- if column not in columns :
1239- continue
1240- expanded_columns .append (column )
1241- # Gather the values
1242- column_index = columns .index (column )
1243- values = [row [column_index ] for row in rows ]
1244- # Expand them
1245- expanded_labels .update (
1246- await datasette .expand_foreign_keys (
1247- request .actor , database_name , table_name , column , values
1248- )
1249- )
1250- if expanded_labels :
1251- # Rewrite the rows
1252- new_rows = []
1253- for row in rows :
1254- new_row = CustomRow (columns )
1255- for column in row .keys ():
1256- value = row [column ]
1257- if (column , value ) in expanded_labels and value is not None :
1258- new_row [column ] = {
1259- "value" : value ,
1260- "label" : expanded_labels [(column , value )],
1261- }
1262- else :
1263- new_row [column ] = value
1264- new_rows .append (new_row )
1265- rows = new_rows
1279+ rows , expanded_columns = await expand_labels (
1280+ datasette ,
1281+ db ,
1282+ request ,
1283+ table_name ,
1284+ columns ,
1285+ rows ,
1286+ default_labels ,
1287+ )
12661288
12671289 _next = request .args .get ("_next" )
12681290
0 commit comments