How To Build A Simple Web Browser in Python

Web browsers have become an integral part of our daily lives, allowing us to explore the vast realm of the internet. In this article, we'll explore a simple web browser built using Python and the PyQt5 library. PyQt5 provides a set of Python bindings for Qt, a powerful cross-platform toolkit.

Web browser using Python and the PyQt5 library

Let's dissect the provided Python code, which defines a basic web browser. The browser is built using the PyQt5 library and features a user interface with navigation buttons, an address bar, and the ability to display web content. The code is structured using PyQt5's QMainWindow and various related widgets.

# Import necessary modules
import sys
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QToolBar, QAction, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtCore import QThread, pyqtSignal
import time

The code begins by importing the required modules. PyQt5.QtCore and PyQt5.QtWidgets provide the core functionality for the GUI, while PyQt5.QtWebEngineWidgets is used for integrating a web engine into the application.

# Define the main browser class
class Browser(QMainWindow):
    def __init__(self):
        super().__init__()

        # Set up the main QWebEngineView widget
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("http://www.google.com"))

        # Set the QWebEngineView as the central widget
        self.setCentralWidget(self.browser)

        # Set up the status bar
        self.status = QStatusBar()
        self.setStatusBar(self.status)

        # Set up the navigation toolbar
        navtb = QToolBar("Navigation")
        self.addToolBar(navtb)

        # Add navigation actions (Back, Forward, Reload, Home, Stop)
        back_btn = QAction("Back", self)
        back_btn.setStatusTip("Back to previous page")
        back_btn.triggered.connect(self.browser.back)
        navtb.addAction(back_btn)

        # Similar actions for Forward, Reload, Home, and Stop

        # Add a separator and an address bar
        navtb.addSeparator()
        self.urlbar = QLineEdit()
        self.urlbar.returnPressed.connect(self.navigate_to_url)
        navtb.addWidget(self.urlbar)

        # Set up the layout
        layout = QVBoxLayout()
        layout.addWidget(navtb)
        layout.addWidget(self.browser)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        # Show the main window in maximized state
        self.showMaximized()

The Browser class is defined as a subclass of QMainWindow. It initializes the main components of the browser, including the QWebEngineView widget, status bar, navigation toolbar, and address bar. The toolbar is populated with various actions such as back, forward, reload, home, and stop, each connected to specific functions.

    def navigate_home(self):
        # Navigate to the home page (Google in this case)
        self.browser.setUrl(QUrl("http://www.google.com"))

    def navigate_to_url(self):
        # Navigate to the URL specified in the address bar
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")

        self.browser.setUrl(q)

    def update_title(self):
        # Update the window title with the current page title
        title = self.browser.page().title()
        self.setWindowTitle("% s - Enhanced Browser" % title)

    def showEvent(self, event):
        # Override the showEvent method to set up QWebEngineSettings for optimizations
        super().showEvent(event)
        settings = self.browser.settings()
        settings.setAttribute(QWebEngineSettings.LocalStorageEnabled, True)
        settings.setAttribute(QWebEngineSettings.PluginsEnabled, True)
        settings.setAttribute(QWebEngineSettings.JavascriptEnabled, True)

The browser class includes functions for navigating to the home page (navigate_home), navigating to a specified URL (navigate_to_url), updating the window title with the current page title (update_title), and setting up QWebEngineSettings for optimizations during the showEvent.

# Entry point of the application
if __name__ == "__main__":
    app = QApplication(sys.argv)
    QApplication.setApplicationName("Subham's Simple Mini Browser Version 1.1")
    window = Browser()
    app.exec_()

Finally, the script sets up the QApplication, defines the application name, creates an instance of the Browser class, and starts the application's event loop.

Full Code

import sys
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QToolBar, QAction, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtCore import QThread, pyqtSignal
import time

class Browser(QMainWindow):
    def __init__(self):
        super().__init__()

        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("http://www.google.com"))

        self.setCentralWidget(self.browser)

        self.status = QStatusBar()
        self.setStatusBar(self.status)

        navtb = QToolBar("Navigation")
        self.addToolBar(navtb)

        back_btn = QAction("Back", self)
        back_btn.setStatusTip("Back to previous page")
        back_btn.triggered.connect(self.browser.back)
        navtb.addAction(back_btn)

        next_btn = QAction("Forward", self)
        next_btn.setStatusTip("Forward to next page")
        next_btn.triggered.connect(self.browser.forward)
        navtb.addAction(next_btn)

        reload_btn = QAction("Reload", self)
        reload_btn.setStatusTip("Reload page")
        reload_btn.triggered.connect(self.browser.reload)
        navtb.addAction(reload_btn)

        home_btn = QAction("Home", self)
        home_btn.setStatusTip("Go home")
        home_btn.triggered.connect(self.navigate_home)
        navtb.addAction(home_btn)

        navtb.addSeparator()

        self.urlbar = QLineEdit()
        self.urlbar.returnPressed.connect(self.navigate_to_url)
        navtb.addWidget(self.urlbar)

        stop_btn = QAction("Stop", self)
        stop_btn.setStatusTip("Stop loading current page")
        stop_btn.triggered.connect(self.browser.stop)
        navtb.addAction(stop_btn)

        layout = QVBoxLayout()
        layout.addWidget(navtb)
        layout.addWidget(self.browser)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.showMaximized()

    def navigate_home(self):
        self.browser.setUrl(QUrl("http://www.google.com"))

    def navigate_to_url(self):
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")

        self.browser.setUrl(q)

    def update_title(self):
        title = self.browser.page().title()
        self.setWindowTitle("% s - Enhanced Browser" % title)

    def showEvent(self, event):
        super().showEvent(event)
        # Set up QWebEngineSettings for optimizations
        settings = self.browser.settings()
        settings.setAttribute(QWebEngineSettings.LocalStorageEnabled, True)
        settings.setAttribute(QWebEngineSettings.PluginsEnabled, True)
        settings.setAttribute(QWebEngineSettings.JavascriptEnabled, True)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    QApplication.setApplicationName("Subham's Simple Mini Browser Version 1.1")
    window = Browser()
    app.exec_()

Output

Browser in Python | CSharp Corner | Subham Ray

Conclusion

In conclusion, we have explored the creation of a simple web browser using Python and the PyQt5 library. This basic browser provides a foundation for understanding how to integrate a web engine into a desktop application, incorporating essential features such as navigation buttons, an address bar, and a status bar. Thus we learned about How To Build A Simple Web Browser in Python.


Similar Articles