pyspark 创建html下载链接,以python中excel格式下载 Dataframe

iyzzxitl  于 2023-02-18  发布在  Spark
关注(0)|答案(1)|浏览(223)

可以修改下面的代码来创建下载链接的xlsx文件与多个df保存为不同的表1 excel文件导入base64从IPython.显示导入HTML

def create_download_link( MM_df_pd, title = "Download CSV file", filename = "case_review.csv"):
   csv = MM_df_pd.to_csv()
   b64 = base64.b64encode(csv.encode())
   payload = b64.decode()
   html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
   html = html.format(payload=payload,title=title,filename=filename)
   return HTML(html)


create_download_link(MM_df_pd)`

我尝试与保存excel文件保存在s3的位置第一,但它是漫长的道路,不会创建下载链接,我取代了csv的代码与xlsx,但没有工作

bvuwiixz

bvuwiixz1#

您需要使用boto3将文件上传到s3,然后创建一个presigned_url...

import boto3

def get_presigned_url(bucket:str, key:str, exp:int=3600):
    s3 = boto3.client('s3')
    return s3.generate_presigned_url('get_object',
                                                Params={'Bucket': bucket,
                                                        'Key': key},
                                                ExpiresIn=exp)

def s3_upload_file(bucket: str, source_file: str, target_path: str):
    s3 = boto3.client('s3')
    s3.upload_file(source_file, bucket, target_path)

df.to_csv("filename.csv") #save your csv

s3_upload_file("your_bucket","filaname.csv","your_s3_path_target")
presigned_url = get_presigned_url("your_bucket","your_s3_path_target")
print(presigned_url)

相关问题