URL Shortener Application In Python

Introduction

In today's world, many websites have very long and complex URLs, so sometimes it is very hard to maintain and share long URLs. Longer URLs are truncated by search engines, web browsers, and in many other areas.

Visitors like to see clean, human-readable URLs since it makes them easy to read, remember, and type. A long, unwieldy URL can be a usability issue.

So we need to convert long URLs in to short URLs. Because short URLs make it easier for your readers to share your content.

So today we will learn about how to create short URLs in Python.

Step 1

Install following libraries,

pip install pyshorteners
pip install pyperclip

pyshorteners

Python libraries are used to shorten the URLs.

pyperclip

provides a cross-platform Python module for copying and pasting text to the clipboard.

Step 2

Create a new python file and import the above two libraries, and also import tKinter.

import pyperclip
import pyshorteners
from tkinter import *

Step 3

Create tkinter and define the GUI size, background color, and title.

root = Tk()
root.geometry("700x350")
root.title("URL Shortner Application")
root.configure(bg="#7F7FFF")

Step 4

Define the following variable to hold the long URL and short URL values.

urlmain = StringVar()
urlshortmain = StringVar()

Step 5

Create a function to generate short URLs. 

def urlShortner():
    urladdress = urlmain.get()
    urlshort = pyshorteners.Shortener().tinyurl.short(urladdress)
    urlshortmain.set(urlshort)

Step 6

Create a function that allows you to copy text to the clipboard. 

def copyurl():
    urlshort = urlshortmain.get()
    pyperclip.copy(urlshort)

Step 7

Create GUI elements as per the following that contains One textbox for the long URL and one button that makes a short URL. Another textbox that holds a short URL Another button is used to copy the short URL into the clipboard.

Label(root,text="URL Shortner App",font=('Arial', 12)).pack(pady=10)
Entry(root,textvariable=urlmain,width=50,font=('Arial 14')).pack(padx=10, pady=10)
Button(root,text="Generate Short Url",command=urlShortner).pack(pady=7)
Entry(root,textvariable=urlshortmain,width=50,font=('Arial 14')).pack(padx=10, pady=10)
Button(root,text="Copy Short Url",command=copyurl).pack(pady=5)

root.mainloop()

The final code is as follows,

from copy import copy
from struct import pack
from tokenize import String
import pyperclip
import pyshorteners
from tkinter import *

#GUI
root = Tk()
root.geometry("700x350")
root.title("URL Shortner Application")
root.configure(bg="#7F7FFF")

#Define Variable for url
urlmain = StringVar()
urlshortmain = StringVar()


#Define Function
def urlShortner():
    urladdress = urlmain.get()
    urlshort = pyshorteners.Shortener().tinyurl.short(urladdress)
    urlshortmain.set(urlshort)

def copyurl():
    urlshort = urlshortmain.get()
    pyperclip.copy(urlshort)


Label(root,text="URL Shortner App",font=('Arial', 12)).pack(pady=10)
Entry(root,textvariable=urlmain,width=50,font=('Arial 14')).pack(padx=10, pady=10)
Button(root,text="Generate Short Url",command=urlShortner).pack(pady=7)
Entry(root,textvariable=urlshortmain,width=50,font=('Arial 14')).pack(padx=10, pady=10)
Button(root,text="Copy Short Url",command=copyurl).pack(pady=5)

root.mainloop()

Step 8

Run your Python application and it should look like this.

URL Shortener Application in Python

Step 9

Paste any long URL into the first textbox and then click on the Generate Short Url button, so you see a short URL in the second textbox. You can copy short urls directly from the textbox or use the Copy Short Url button.

URL Shortener Application in Python

Summary

So you see, it's very easy to create a short URL in Python with little coding.

In this article, we will learn how to create a short URL application in Python.

Thank you. Enjoy Coding.


Recommended Free Ebook
Similar Articles