Jupyter notebooks can be a great tool for further custom analyses and plot generation for your data. While we have an easy way to add metadata to your samples, sometimes you might need to work with additional information and files in your notebook. You may also want to export tables and plots that you've generated in your notebook. This article walks you through those processes.

Upload your files to the notebook

Some of our users like to include an image they've generated elsewhere (for instance, their logo) in the PDF they want to generate with their notebook. You can upload files from your computer to your running notebook using the following commands.

# Import the module that's required to upload files
from ipywidgets import FileUpload
# Set the function
upload = FileUpload()
# Call the function

This will generate an "Upload" button under the cell, which, when clicked, will provide you with the option to select the file(s) you want to upload.

Once the file is uploaded, the data from the first file is stored as upload.data[0]. For instance, if you have uploaded a .tsv (tab-separated) file, and want to import it as a pandas dataframe, you can do that with the following.

# Import the required modules to process this data format 
from io import BytesIO
# Read the data
my_data = BytesIO(upload.data[0])

# Now you can convert that data into a pandas dataframe
my_pandas_dataframe = pd.read_csv(my_data, sep='\t')

If you're importing an image for use in your notebook, such as a logo, you can use the following commands to be able to use that image.

# Import the data as you did in the above
from io import BytesIO
my_data = BytesIO(upload.data[0])

# Import the function required to read the image correctly
import PIL.Image as Image
# Now convert your data into image format
my_image = Image.open(my_data)

Downloading your notebook-generated files

Once you've performed your analyses or generated your plots, you often want to download those as files to your desktop. You can easily download a pdf version of the entire report by clicking: File > Download as > PDF report (.pdf). When you generate plots within a notebook, you will see ellipses at the top-right of the plot, which you can click to view a menu of save options for that plot.

If you want to export a pandas dataframe, for instance, that you have generated within the notebook, you can do that with the following commands.

# Import a function that will allow you to view a link to download your files
from IPython.display import FileLink

# Write your pandas dataframe to a csv
my_pandas_dataframe.to_csv("Your_filename.csv", index=False, sep=',')

# Display a link to download that file to your machine

Once you run such a cell, a link will be displayed below it which will allow you to download your file. You can also export to excel, saving your different dataframes to different worksheets with the following.

with pd.ExcelWriter("Your_filename.xlsx") as writer:
my_dataframe1.to_excel(writer, sheet_name="dataframe1")
my_dataframe2.to_excel(writer, sheet_name="dataframe2")


Did this answer your question?