Welcome to Day Eighteen of my 21-day project series!
Today I’ll make a Browser In Python Using PyQt5 that will not store browsing history!!
(I mean for security of course)
I am super Excited!! Are you??
Do read the experience…
Contents
Project Contents
This mini project is called Browser In Python Using PyQt5.
This Python project is a user-friendly web browser application built using the PyQt5 library. This is a very simple implementation using OOP concepts.
It has got a URL bar, a go button, a back button, a forward button, and a refresh button.
Upon launching the application, it opens with our very favorite the popular website “https://google.com“.
It is built using PyQt5. And that’s all…
The final look of the mini project:
Tell me what should I improve in this.
I wouldn’t get into the step-by-step explanation of the code as the article becomes super long. (And I guess boring too.)
But I would post an explanatory article for it soon.
Full Source Code
Here’s the full source code of Browser In Python Using PyQt5:
# Importing necessary classes from PyQt5 library
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWebEngineWidgets import *
class MyWebBrowser(): # Creating a class for the web browser
""" A simple web browser application using PyQt5. """
def __init__(self):
""" Initialize the web browser."""
self.window = QWidget() # Creating a QWidget instance
self.window.setWindowTitle("My Web Browser") # Setting the window title
# main layout: holds browser and horizontal layout
self.layout = QVBoxLayout()
# holds the buttons and url bar
self.horizontal = QHBoxLayout()
self.url_bar = QLineEdit()
self.url_bar.setMaximumHeight(30)
self.go_btn = QPushButton("Go")
self.go_btn.setMinimumHeight(30)
# to load a page when enter is pressed in the url bar
self.url_bar.returnPressed.connect(self.go_btn.click) # Connect the returnPressed signal to the go_btn click slot
self.back_btn = QPushButton("<")
self.back_btn.setMinimumHeight(30)
self.forward_btn = QPushButton(">")
self.forward_btn.setMinimumHeight(30)
self.reload_btn = QPushButton()
self.reload_btn.setIcon(QIcon('images/reload.png')) # Set the icon using the image file path
self.reload_btn.setMinimumHeight(30)
# Adding the widgets to the horizontal layout
self.horizontal.addWidget(self.reload_btn)
self.horizontal.addWidget(self.url_bar)
self.horizontal.addWidget(self.go_btn)
self.horizontal.addWidget(self.back_btn)
self.horizontal.addWidget(self.forward_btn)
self.browser = QWebEngineView()
# connecting buttons
self.go_btn.clicked.connect(lambda: self.navigate(self.url_bar.text()))
self.back_btn.clicked.connect(self.go_back)
self.forward_btn.clicked.connect(self.go_forward)
self.reload_btn.clicked.connect(self.browser.reload)
self.layout.addLayout(self.horizontal) # Adding the horizontal layout to the main layout
self.layout.addWidget(self.browser) # Adding the browser to the main layout
self.browser.setUrl(QUrl("https://google.com")) # Setting the initial URL for the browser
self.window.setLayout(self.layout) # Setting the main layout for the window
self.window.show()
def navigate(self, url):
"""
Navigate to the specified URL.
Args:
url (str): The URL to navigate to.
"""
if not url.startswith("http"): # Checking if the URL does not start with "http"
url = "https://" + url # Prepending "https://" to the URL
self.url_bar.setText(url) # Updating the URL bar text with the modified URL
self.browser.setUrl(QUrl(url)) # Navigating the browser to the specified URL
def go_back(self):
""" Go back to the previous page. """
self.url_bar.setText("")
self.browser.back()
def go_forward(self):
""" Go forward to the next page. """
self.url_bar.setText("")
self.browser.forward()
app = QApplication([]) # Creating a QApplication instance
window = MyWebBrowser() # Creating an instance of the MyWebBrowser class
app.exec_() # Starting the application event loop
The icon for the refresh button:
The Browser In Python Using PyQt5 is well documented and well written it would be easy to understand. Here are the links to topics within the mini-project.
You can check them if you have any doubts or confusion:
So how is the Browser In Python Using PyQt5?
Possible Improvements (That I’ll add after 21 days)
- Bookmarks
- Tabbed Browsing
- Customizable User Interface (Dark mode, themes, and stuff)
- Search Suggestions/Autocomplete
- Password Manager
- Spell Checker
And a lot more…
Experience of the day
In case you don’t know this project is a part of the brand new series that I have started. #21days21projects in python. To read more about it you can check out this page.
And for those who already know here’s my experience of the day.
It was a great day! Seems like things can’t go better. I had enough amount of time for my project. I was happy. And making projects now seems like a part of my daily rituals.
About the project, at first, I thought something like yesterday to be there. Because I assumed it to be pretty complicated.
But it was surprisingly easy to do with python.
And what’s more, it’s day 18!!! I’ll die with happiness. It’s an extraordinary feeling.
I did take quite a lot of help online to make this up. And this seems to be the best project so far.
And yes it’s fun to make projects while a piece of light music is on. I thought I would be disturbing and will hamper the making project.
On the contrary, it improves the flow of my work…
Do let me know whether you liked this Browser In Python Using PyQt5. Also, let me know if there are any suggestions.
And most importantly how was your DAY 18?
Amazing, like never before??
Well, I hope it was fun. And remember it’s perfectly awesome if you missed out. Continue to work from tomorrow.
And today just write one line of code as simple as making a variable or printing something.
Your suggestions are welcomed!
Happy coding…
And wait, if you have some ideas for the upcoming projects do let me know that too. Even when the series ends do let me know what you like…
Hi there I am so happy I found your site, I really found you by mistake, while I was researching on Askjeeve for something else, Regardless I am here now and would just like to say thanks a lot for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the fantastic job.
Thanks for your kind words