Download data from a jupyter server

前端 未结 4 1785
[愿得一人]
[愿得一人] 2021-02-18 17:49

I\'m using ipython notebook by connecting to a server I don\'t know how to download a thing (data frame, .csv file,... for example) programatically to my local computer. Because

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-18 18:17

    Based on another answer, the following function will export a pandas data frame to a csv file and it will provide you with a link to download the csv file in your browser:

    def csv_download_link(df, csv_file_name, delete_prompt=True):
        """Display a download link to load a data frame as csv from within a Jupyter notebook"""
        df.to_csv(csv_file_name, index=False)
        from IPython.display import FileLink
        display(FileLink(csv_file_name))
        if delete_prompt:
            a = input('Press enter to delete the file after you have downloaded it.')
            import os
            os.remove(csv_file_name)
    

    To get a link to a csv file, enter the above function and the code below in a jupyter notebook cell :

    csv_download_link(df, 'df.csv')
    

    By default the argument delete_prompt=True makes sure that once you have downloaded the csv file, it gets deleted so the file doesn't pollute the git repository where you naturally archive your notebooks.

提交回复
热议问题