How to create a table with clickable hyperlink to a local file in Pandas?
If you are familiar with Data Science or Machine Learning field then we can definitely say that we are going to learn something new from this article, So let’s begin. Here in this article, we will learn how to create a clickable hyperlink of the local file path using pandas. It is not a very complicated task, we can easily do it in only 4 to 5 steps. Let’s dig deeper into it and talk about required libraries.
- Pandas: It is one of the most favorite and popular libraries in python for data wrangling and analysis. Pandas is the backbone of all Data Science and Machine Learning projects. It is one of the most important tools for Data Scientists and Analysts. Some features of pandas are: Data Handling, Alignment and indexing, Handling missing data, Cleaning data, Merging and joining of datasets, and much more. Here as well we will use pandas to create a dataset.
- OS library: The OS module present in python also plays a very important role in various projects. It provides lots of functions for interacting with the operating system. It includes many functions to interact with the file system as well. Here we have to work with local files that’s why we need an os module to handle them.
Step 1: Import required libraries.
Step 2: Creating a dataset using a dictionary and list.
Here in the above code snippet, we are creating a small dataset of the local file’s paths present in our local system. First, we are creating a dictionary where the image name acts as a unique key and the path of an image is acts as the value of a unique key here. Finally, converting the dictionary into a list so further we can easily convert the list into pandas dataframe.
Step 3: Converting list into Dataframe and printing it.
Till Step 2 our dataframe was in list form so in Step 3 we are converting it into pandas dataframe so we can easily apply dozens of operations on it for better analysis. Currently, the path of images is not in clickable form, we have to perform some tricky operation on it to convert in into clickable hyperlink form.
Step 4: Creating a function to convert the path into Clickable form.
In the above code snippet, we have created a function that will convert the file path(location of file) into a clickable hyperlink form.
Step 5: Applying the function on column “location” which will convert path into clickable hyperlink form. Now, whenever we will print the dataframe we will get our required to be output.
Finally, we have successfully converted our local file path into a clickable hyperlink form. Let’s check out some examples.
Example 1: Creating a Dataframe that contains the clickable hyperlink path of images present in our local system.
Example 2: Creating a Dataframe containing a clickable hyperlink path of files present in our local system.
Note: Whenever we are trying to load local files from the browser we are continuously getting an error ‘Not allowed to load local resources’ in our browser. If we Google this problem and came to know that it is happening due to some security issue of our system. But there is a way we can solve this problem without disturbing our system security, put the required files in the same working directory in which we are working. In the above examples, we created a folder and put all required files in it, and finally easily load them without any issue. In Example 1 we created a folder named “New” in my working directory and put all the required files in it similarly in Example 2 as well.