Spaces:
Sleeping
Sleeping
Update app.py
Browse files122 unmodified lines123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217122 unmodified lines# ----------------------------# Kepler.gl HTML 生成(ポイントのみ)# ----------------------------def make_kepler_html(df_points: pd.DataFrame, height: int = 640) -> str:"""df_points は 'lat','lon','address_input','CF' を含む DataFrame を想定。ポイントレイヤのみを Kepler.gl で描画し、HTMLを文字列で返す。"""df_valid = df_points.dropna(subset=["lat", "lon"]).copy()if df_valid.empty:# 空のKeplerでもHTMLは返すm = KeplerGl(height=height)return m._repr_html_()# ほどよい初期中心center_lat = float(df_valid["lat"].median())center_lon = float(df_valid["lon"].median())# Kepler 設定(ポイントレイヤのみ)config = {"version": "v1","config": {"visState": {"filters": [],"layers": [{"id": "point_layer","type": "point","config": {"dataId": "points","label": "Points","color": [18, 147, 154],"columns": {"lat": "lat", "lng": "lon"},"isVisible": True,"visConfig": {"radius": 10, # 基本半径"opacity": 0.9,"outline": False}},"visualChannels": {# CF列が数値ならサイズに反映(なければ自動で固定半径)"sizeField": {"name": "CF", "type": "real"} if pd.to_numeric(df_valid.get("CF", pd.Series([])), errors="coerce").notna().any() else None,"sizeScale": "sqrt",},}],"interactionConfig": {"tooltip": {"enabled": True,"fieldsToShow": {"points": [ {"name": "address_input", "format": None},{"name": "CF", "format": None},{"name": "lat", "format": None},{"name": "lon", "format": None} ]},"compareMode": False,"compareType": "absolute"}},"layerBlending": "normal"},"mapState": {"bearing": 0,"pitch": 0,"latitude": center_lat,"longitude": center_lon,"zoom": 6},"mapStyle": {"styleType": "light","topLayerGroups": {},"visibleLayerGroups": {"label": True, "road": True, "border": False, "building": False, "water": True, "land": True}}}}m = KeplerGl(height=height, config=config)# Kepler は DataFrame の列名で自動解釈(lat/lon)m.add_data(data=df_valid[["lat", "lon", "address_input", "CF"]], name="points")# Gradioへは _repr_html_ をそのまま返すのが簡単try:return m._repr_html_()except Exception:# 万一ノートブック外で不安定な場合はHTMLファイルを生成して読み戻すwith tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:tmp = f.namem.save_to_html(file_name=tmp, read_only=True)with open(tmp, "r", encoding="utf-8") as fh:html = fh.read()return html# ----------------------------# 実行パイプライン(ポイントのみ)122 unmodified lines123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201122 unmodified lines# ----------------------------# Kepler.gl HTML 生成(ポイントのみ)# ----------------------------def _iframe_from_html_string(html: str, height: int = 640) -> str:import tempfile, os# 一時HTMLファイルを作り、iframeから "file=..." で参照させるtmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)tmp.write(html.encode("utf-8"))tmp.flush()tmp.close()# Gradioは "file=/abs/path" の形式でローカルファイルを配信できますreturn f'<iframe src="file={tmp.name}" width="100%" height="{height}" style="border:0;"></iframe>'def make_kepler_html(df_points: pd.DataFrame, height: int = 640) -> str:df_valid = df_points.dropna(subset=["lat", "lon"]).copy()if df_valid.empty:m = KeplerGl(height=height)else:center_lat = float(df_valid["lat"].median())center_lon = float(df_valid["lon"].median())config = {"version": "v1","config": {"visState": {"filters": [],"layers": [{"id": "point_layer","type": "point","config": {"dataId": "points","label": "Points","color": [18,147,154],"columns": {"lat": "lat", "lng": "lon"},"isVisible": True,"visConfig": {"radius": 10, "opacity": 0.9, "outline": False},},"visualChannels": {"sizeField": {"name": "CF", "type": "real"} if pd.to_numeric(df_valid.get("CF", pd.Series([])), errors="coerce").notna().any() else None,"sizeScale": "sqrt",},}],"interactionConfig": {"tooltip": {"enabled": True,"fieldsToShow": {"points": [{"name":"address_input","format":None},{"name":"CF","format":None},{"name":"lat","format":None},{"name":"lon","format":None},]},"compareMode": False,"compareType": "absolute",}},"layerBlending": "normal",},"mapState": {"bearing": 0, "pitch": 0, "latitude": center_lat, "longitude": center_lon, "zoom": 6},"mapStyle": {"styleType":"light","topLayerGroups":{},"visibleLayerGroups":{"label":True,"road":True,"border":False,"building":False,"water":True,"land":True}},},}m = KeplerGl(height=height, config=config)m.add_data(data=df_valid[["lat","lon","address_input","CF"]], name="points")# 直接HTML文字列を返すとJSが走らないことがあるので、必ずファイル化→iframeで返すimport tempfilewith tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:path = f.namem.save_to_html(file_name=path, read_only=True)with open(path, "r", encoding="utf-8") as fh:html = fh.read()return _iframe_from_html_string(html, height=height)# ----------------------------# 実行パイプライン(ポイントのみ)
app.py+65-81