Creating your own browser using Python

Hey there! In this tutorial, we will be learning to create a simple browser in Python using PyQt5 in PyCharm.

Qt is a set of cross-platform C++ libraries that implement high-level APIs for accessing various aspects of modern desktop and mobile systems such as location and positioning services, Bluetooth connectivity, etc.
PyQt5 is a comprehensive set of Python bindings for Qt v5, that enables Python to be used as an alternative application development language to C++ on all supported platforms including iOS and Android.

Implementation

  1. Firstly, open PyCharm and create a project titled Browser. Then open the terminal and type the below-listed commands to install the respective libraries.
    pip install PyQt5
    pip install PyQtWebEngine
  2. Then, within the main.py file in thiproject, type the below-specified code. Refer to the code’s comments regarding various functionalities supported by the browser and their implementations.
    # Import necessary libraries
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtWebEngineWidgets import *
    import sys
    
    
    # Create a main window class
    class MainWindow(QMainWindow):
        # Constructor of this class
        def __init__(self):
            super(MainWindow, self).__init__()
            # To provide a widget for viewing and editing web documents:
            self.browser = QWebEngineView()
            # To set default browser homepage as google homepage:
            self.browser.setUrl(QUrl("http://www.google.com"))
            # To set browser as central widget of main window:
            self.setCentralWidget(self.browser)
            # To open browser in a maximized window:
            self.showMaximized()
    
            # To create a navigation bar:
            navbar = QToolBar()
            navbar.adjustSize()
            # To add the navigation bar to the browser:
            self.addToolBar(navbar)
    
            # To add back button within navigation bar:
            back_btn = QAction('⮜', self)
            back_btn.triggered.connect(self.browser.back)
            navbar.addAction(back_btn)
    
            # To add forward button within navigation bar:
            forward_btn = QAction('⮞', self)
            forward_btn.triggered.connect(self.browser.forward)
            navbar.addAction(forward_btn)
    
            # To add reload button within navigation bar:
            reload_btn = QAction('⟳', self)
            reload_btn.triggered.connect(self.browser.reload)
            navbar.addAction(reload_btn)
    
            # To add URL bar within navigation bar:
            self.url_bar = QLineEdit()
            self.url_bar.returnPressed.connect(self.open_url)
            navbar.addWidget(self.url_bar)
            self.browser.urlChanged.connect(self.update_url)
    
        # To navigate to desired URL specified within URL bar:
        def open_url(self):
            url = self.url_bar.text()
            self.browser.setUrl(QUrl(url))
    
        # To update the URL bar contents when navigated from one page to another:
        def update_url(self, q):
            self.url_bar.setText(q.toString())
    
    
    # To call constructor of the C++ class QApplication:
    # Here, sys.argv is used to initialize the QT application
    app = QApplication(sys.argv)
    # To specify name of the browser:
    QApplication.setApplicationName("My_Explorer")
    # To create an object of MainWindow class defined above:
    window = MainWindow()
    # To run the main event loop and wait until exit() is called:
    app.exec()
    

To access the arrowhead and reload symbols used in the above code, click here.

Output

In the video below, you can see the output of our project that we have just built:

You can see our browser is ready.

Also read, Implement Browser Automation using Selenium with Python

 

Leave a Reply

Your email address will not be published.