77from datetime import datetime , timedelta
88from io import IOBase
99from pathlib import Path
10- from typing import Mapping , Optional , Protocol , Sequence
10+ from typing import Any , Mapping , Optional , Protocol , Sequence
1111
1212from importlib_resources import files
1313
14+ from .adaptors import _create_df_adaptor , _DFAdaptor
1415from .cache import PinsCache
1516from .config import get_allow_rsc_short_name
1617from .drivers import default_title , load_data , load_file , save_data
@@ -1121,15 +1122,24 @@ def path_to_deploy_version(self, name: str, version: str):
11211122 def user_name (self ):
11221123 return self .fs .api .get_user ()["username" ]
11231124
1125+ # TODO(NAMC) what about the functions that call this one?
11241126 def prepare_pin_version (self , pin_dir_path , x , name : "str | None" , * args , ** kwargs ):
1127+ try :
1128+ x = _create_df_adaptor (x )
1129+ except NotImplementedError :
1130+ # Not a dataframe.
1131+ pass
1132+
11251133 # RSC pin names can have form <user_name>/<name>, but this will try to
11261134 # create the object in a directory named <user_name>. So we grab just
11271135 # the <name> part.
11281136 short_name = name .split ("/" )[- 1 ]
11291137
11301138 # TODO(compat): py pins always uses the short name, R pins uses w/e the
11311139 # user passed, but guessing people want the long name?
1132- meta = super ()._create_meta (pin_dir_path , x , short_name , * args , ** kwargs )
1140+ meta = super ()._create_meta (
1141+ pin_dir_path , x , short_name , * args , ** kwargs
1142+ ) # TODO(NAMC) ensure .create_meta can accept adaptor
11331143 meta .name = name
11341144
11351145 # copy in files needed by index.html ----------------------------------
@@ -1147,7 +1157,7 @@ def prepare_pin_version(self, pin_dir_path, x, name: "str | None", *args, **kwar
11471157 # render index.html ------------------------------------------------
11481158
11491159 all_files = [meta .file ] if isinstance (meta .file , str ) else meta .file
1150- pin_files = ", " .join (f"""<a href="{ x } ">{ x } </a>""" for x in all_files )
1160+ pin_files = ", " .join (f"""<a href="{ file } ">{ file } </a>""" for file in all_files )
11511161
11521162 context = {
11531163 "date" : meta .version .created .replace (microsecond = 0 ),
@@ -1164,15 +1174,13 @@ def prepare_pin_version(self, pin_dir_path, x, name: "str | None", *args, **kwar
11641174
11651175 import json
11661176
1167- import pandas as pd
1168-
1169- if isinstance (x , pd .DataFrame ):
1177+ if isinstance (x , _DFAdaptor ):
11701178 # TODO(compat) is 100 hard-coded?
1171- # Note that we go df -> json -> dict, to take advantage of pandas type conversions
1172- data = json .loads (x .head (100 ).to_json ( orient = "records" ))
1179+ # Note that we go df -> json -> dict, to take advantage of type conversions in the dataframe library
1180+ data : list [ dict [ Any , Any ]] = json .loads (x .head (100 ).write_json ( ))
11731181 columns = [
11741182 {"name" : [col ], "label" : [col ], "align" : ["left" ], "type" : ["" ]}
1175- for col in x
1183+ for col in x . columns
11761184 ]
11771185
11781186 # this reproduces R pins behavior, by omitting entries that would be null
0 commit comments