jairajsahgal
Posted on April 3, 2021
About
In this blog you will learn how to create a file uploader that runs from a terminal and asks the user to locate the file using a Graphical User Interface and uploads the file using an API from the File.io, the uploaded link is then displayed into the terminal and copied into clipboard, it also creates a QR code for the link.
Imports
- Requests (Posting file in API)
- Tkinter (For GUI)
- Json (To convert result in a dictionary)
- Pandas ( To copy the link into a clipboard)
- qrcode (To create a QR Code of the link with filename)
import requests
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import json
import pandas as pd
import qrcode
import time
Program
Structure
The program is created in Python and frequently uses methods from different libraries. Also the proram only works in Unix only because of the /
and \
difference and I do not want to make distinction of operating system in this simple program.
We hide the main window in tkinter using Tk().withdraw()
so that user can only use tkinter to select the file and get its path.
python
print("Please select your file")
time.sleep(1)
filelocation = askopenfilename()
file_name=filelocation.split("/")[-1]
print(file_name)
Asks the user to select the file and waits for 1 second, then a dialog box opens and the user selects a file, user cannot select a directory. The path is stored in a file location and then the file name is extracted using string and array manipulation which is explained below.
file_name=filelocation.split("/")[-1]
Remember filelocation is a variable of class type string, hence using split("/")
will split it into an array with elements distributed where /
is encountered.
Example of filelocation string
/home/username/Downloads/Documents/file.html
This is what a file location in unix based operating system will show when asked for a file location. Then [-1] ensures that I get the file name.
python
files = {
'file': (file_name, open(filelocation, 'rb')),
}
response = requests.post('https://file.io/', files=files)
Then we create a dictionary that will have a key of 'files' and name of the file we are uploading and an object of open() with file location with parameter of reading it as a binary file.
Using requests library function post we post the file in file.io with the parameter as required by their API, we fill the parameter files with our created dictionary i.e. files in our syntax above.
python
data=json.loads(response.text)
if data["success"]==True:
print("One use hyperlink with your file -> ",data["link"]," copied to your clipboard")
img=qrcode.make(data["link"])
img.save(file_name.split(".")[0]+".png")
df=pd.DataFrame([data["link"]])
df.to_clipboard(index=False,header=False)
print("Created QR code for link with name: "+file_name.split(".")[0]+".png")
else:
print("Error: \n Please check your network \n Possible uploading same file too many times\n Else:\n Server problem")
We convert the text from response from the API which is in a json format into a python dictionary.
We check if the file was uploaded successfully with data["success"]==True
If the file is uploaded successfully, we then print the link into the terminal
python
print("One use hyperlink with your file -> ",data["link"]," copied to your clipboard")
Then we make the QR code, using the following code
python
img=qrcode.make(data["link"])
img.save(file_name.split(".")[0]+".png")
qrcode.make(data["link"])
is passing the link string into make method of qrcode which returns an object of image which we stored in img
variable and then using save()
we saved the image into program directory.
We then copy the link into our clipboard using below code
python
df=pd.DataFrame([data["link"]])
df.to_clipboard(index=False,header=False)
We create a series in pandas of our link string and then convert the series into a dataframe, and then used to_clipboard()
to copy the link into clipboard.
I used pandas library because it is maintained and frequently used by people who handle data.
We tell the user that the QR code of the link has been created and that the link has been copied into computer's clipboard.
Link to the github repository :
Project_Link
Thank you for visiting my article.
Posted on April 3, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.