Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Take care to convert display values to be JSON-serializable.
Browse files
    	
        lynxkite-graph-analytics/src/lynxkite_graph_analytics/lynxkite_ops.py
    CHANGED
    
    | 
         @@ -340,7 +340,7 @@ def visualize_graph( 
     | 
|
| 340 | 
         
             
                label_by: ops.NodeAttribute = None,
         
     | 
| 341 | 
         
             
                color_edges_by: ops.EdgeAttribute = None,
         
     | 
| 342 | 
         
             
            ):
         
     | 
| 343 | 
         
            -
                nodes = graph.dfs["nodes"] 
     | 
| 344 | 
         
             
                if color_nodes_by:
         
     | 
| 345 | 
         
             
                    nodes["color"] = _map_color(nodes[color_nodes_by])
         
     | 
| 346 | 
         
             
                for cols in ["x y", "long lat"]:
         
     | 
| 
         @@ -370,7 +370,9 @@ def visualize_graph( 
     | 
|
| 370 | 
         
             
                    )
         
     | 
| 371 | 
         
             
                    curveness = 0.3
         
     | 
| 372 | 
         
             
                nodes = nodes.to_records()
         
     | 
| 373 | 
         
            -
                edges =  
     | 
| 
         | 
|
| 
         | 
|
| 374 | 
         
             
                if color_edges_by:
         
     | 
| 375 | 
         
             
                    edges["color"] = _map_color(edges[color_edges_by])
         
     | 
| 376 | 
         
             
                edges = edges.to_records()
         
     | 
| 
         @@ -403,8 +405,8 @@ def visualize_graph( 
     | 
|
| 403 | 
         
             
                                    "symbolSize": 50 / len(nodes) ** 0.5,
         
     | 
| 404 | 
         
             
                                    "itemStyle": {"color": n.color} if color_nodes_by else {},
         
     | 
| 405 | 
         
             
                                    "label": {"show": label_by is not None},
         
     | 
| 406 | 
         
            -
                                    "name": getattr(n, label_by,  
     | 
| 407 | 
         
            -
                                    "value": getattr(n, color_nodes_by,  
     | 
| 408 | 
         
             
                                    if color_nodes_by
         
     | 
| 409 | 
         
             
                                    else None,
         
     | 
| 410 | 
         
             
                                }
         
     | 
| 
         @@ -415,7 +417,7 @@ def visualize_graph( 
     | 
|
| 415 | 
         
             
                                    "source": str(r.source),
         
     | 
| 416 | 
         
             
                                    "target": str(r.target),
         
     | 
| 417 | 
         
             
                                    "lineStyle": {"color": r.color} if color_edges_by else {},
         
     | 
| 418 | 
         
            -
                                    "value": getattr(r, color_edges_by,  
     | 
| 419 | 
         
             
                                    if color_edges_by
         
     | 
| 420 | 
         
             
                                    else None,
         
     | 
| 421 | 
         
             
                                }
         
     | 
| 
         @@ -427,17 +429,18 @@ def visualize_graph( 
     | 
|
| 427 | 
         
             
                return v
         
     | 
| 428 | 
         | 
| 429 | 
         | 
| 430 | 
         
            -
            def  
     | 
| 
         | 
|
| 
         | 
|
| 431 | 
         
             
                if isinstance(df, pl.LazyFrame):
         
     | 
| 432 | 
         
             
                    df = df.collect()
         
     | 
| 433 | 
         
             
                if isinstance(df, pl.DataFrame):
         
     | 
| 434 | 
         
            -
                     
     | 
| 435 | 
         
             
                # Convert non-numeric columns to strings.
         
     | 
| 436 | 
         
            -
                df = df.copy()
         
     | 
| 437 | 
         
             
                for c in df.columns:
         
     | 
| 438 | 
         
             
                    if not pd.api.types.is_numeric_dtype(df[c]):
         
     | 
| 439 | 
         
             
                        df[c] = df[c].astype(str)
         
     | 
| 440 | 
         
            -
                return df 
     | 
| 441 | 
         | 
| 442 | 
         | 
| 443 | 
         
             
            @op("View tables", view="table_view")
         
     | 
| 
         @@ -446,7 +449,7 @@ def view_tables(bundle: Bundle, *, limit: int = 100): 
     | 
|
| 446 | 
         
             
                    "dataframes": {
         
     | 
| 447 | 
         
             
                        name: {
         
     | 
| 448 | 
         
             
                            "columns": [str(c) for c in df.columns],
         
     | 
| 449 | 
         
            -
                            "data":  
     | 
| 450 | 
         
             
                        }
         
     | 
| 451 | 
         
             
                        for name, df in bundle.dfs.items()
         
     | 
| 452 | 
         
             
                    },
         
     | 
| 
         | 
|
| 340 | 
         
             
                label_by: ops.NodeAttribute = None,
         
     | 
| 341 | 
         
             
                color_edges_by: ops.EdgeAttribute = None,
         
     | 
| 342 | 
         
             
            ):
         
     | 
| 343 | 
         
            +
                nodes = df_for_frontend(graph.dfs["nodes"], 10_000)
         
     | 
| 344 | 
         
             
                if color_nodes_by:
         
     | 
| 345 | 
         
             
                    nodes["color"] = _map_color(nodes[color_nodes_by])
         
     | 
| 346 | 
         
             
                for cols in ["x y", "long lat"]:
         
     | 
| 
         | 
|
| 370 | 
         
             
                    )
         
     | 
| 371 | 
         
             
                    curveness = 0.3
         
     | 
| 372 | 
         
             
                nodes = nodes.to_records()
         
     | 
| 373 | 
         
            +
                edges = df_for_frontend(
         
     | 
| 374 | 
         
            +
                    graph.dfs["edges"].drop_duplicates(["source", "target"]), 10_000
         
     | 
| 375 | 
         
            +
                )
         
     | 
| 376 | 
         
             
                if color_edges_by:
         
     | 
| 377 | 
         
             
                    edges["color"] = _map_color(edges[color_edges_by])
         
     | 
| 378 | 
         
             
                edges = edges.to_records()
         
     | 
| 
         | 
|
| 405 | 
         
             
                                    "symbolSize": 50 / len(nodes) ** 0.5,
         
     | 
| 406 | 
         
             
                                    "itemStyle": {"color": n.color} if color_nodes_by else {},
         
     | 
| 407 | 
         
             
                                    "label": {"show": label_by is not None},
         
     | 
| 408 | 
         
            +
                                    "name": str(getattr(n, label_by, "")) if label_by else None,
         
     | 
| 409 | 
         
            +
                                    "value": str(getattr(n, color_nodes_by, ""))
         
     | 
| 410 | 
         
             
                                    if color_nodes_by
         
     | 
| 411 | 
         
             
                                    else None,
         
     | 
| 412 | 
         
             
                                }
         
     | 
| 
         | 
|
| 417 | 
         
             
                                    "source": str(r.source),
         
     | 
| 418 | 
         
             
                                    "target": str(r.target),
         
     | 
| 419 | 
         
             
                                    "lineStyle": {"color": r.color} if color_edges_by else {},
         
     | 
| 420 | 
         
            +
                                    "value": str(getattr(r, color_edges_by, ""))
         
     | 
| 421 | 
         
             
                                    if color_edges_by
         
     | 
| 422 | 
         
             
                                    else None,
         
     | 
| 423 | 
         
             
                                }
         
     | 
| 
         | 
|
| 429 | 
         
             
                return v
         
     | 
| 430 | 
         | 
| 431 | 
         | 
| 432 | 
         
            +
            def df_for_frontend(df: pd.DataFrame, limit: int) -> pd.DataFrame:
         
     | 
| 433 | 
         
            +
                """Returns a DataFrame with values that are safe to send to the frontend."""
         
     | 
| 434 | 
         
            +
                df = df[:limit]
         
     | 
| 435 | 
         
             
                if isinstance(df, pl.LazyFrame):
         
     | 
| 436 | 
         
             
                    df = df.collect()
         
     | 
| 437 | 
         
             
                if isinstance(df, pl.DataFrame):
         
     | 
| 438 | 
         
            +
                    df = df.to_pandas()
         
     | 
| 439 | 
         
             
                # Convert non-numeric columns to strings.
         
     | 
| 
         | 
|
| 440 | 
         
             
                for c in df.columns:
         
     | 
| 441 | 
         
             
                    if not pd.api.types.is_numeric_dtype(df[c]):
         
     | 
| 442 | 
         
             
                        df[c] = df[c].astype(str)
         
     | 
| 443 | 
         
            +
                return df
         
     | 
| 444 | 
         | 
| 445 | 
         | 
| 446 | 
         
             
            @op("View tables", view="table_view")
         
     | 
| 
         | 
|
| 449 | 
         
             
                    "dataframes": {
         
     | 
| 450 | 
         
             
                        name: {
         
     | 
| 451 | 
         
             
                            "columns": [str(c) for c in df.columns],
         
     | 
| 452 | 
         
            +
                            "data": df_for_frontend(df, limit).values.tolist(),
         
     | 
| 453 | 
         
             
                        }
         
     | 
| 454 | 
         
             
                        for name, df in bundle.dfs.items()
         
     | 
| 455 | 
         
             
                    },
         
     |