In this Python PyQt5 article we are going to learn about How to Build Web Applications with Python PyQt5, PyQt5 is Python library that provides bindings for the Qt framework, it allows you to build desktop and web applications using Python. in this article we will focus on building web applications with PyQt5.
How to Build Web Applications with Python PyQt5
First of all we need to install PyQt5 and we can use pip for that
1 |
pip install PyQt5 |
We’ll also need the QtWebEngineWidgets module, which provides the web engine that we’ll use to display web content. you can install it like this.
1 |
pip install PyQtWebEngine |
Let’s start by creating simple web application that displays webpage. this is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtCore import QUrl from PyQt5.QtWebEngineWidgets import QWebEngineView class WebApplication(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders - PyQt5") self.setGeometry(100, 100, 800, 600) self.browser = QWebEngineView() self.browser.setUrl(QUrl("https://www.geekscoders.com")) self.setCentralWidget(self.browser) app = QApplication(sys.argv) window = WebApplication() window.show() sys.exit(app.exec_()) |
In the above code we have created QMainWindow object that displays QWebEngineView widget. We set the URL for the widget to be https://www.geekscoders.com. and lastly we show the main window and run the application.
Run the code and this will be result
Handling JavaScript
Now let’s take a look that how to handle JavaScript in web application. this is an example that demonstrates how to handle user input in a web application using JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtCore import QUrl from PyQt5.QtWebEngineWidgets import QWebEngineView class WebApplication(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("GeeksCoders - PyQt5") self.setGeometry(100, 100, 800, 600) self.browser = QWebEngineView() self.browser.setUrl(QUrl("https://www.google.com")) self.setCentralWidget(self.browser) self.browser.loadFinished.connect(self.search) def search(self): self.browser.page().runJavaScript( """ document.querySelector('input[type="text"]').value = "geekscoders.com"; document.querySelector('input[type="submit"]').click(); """ ) app = QApplication(sys.argv) window = WebApplication() window.show() sys.exit(app.exec_()) |
In this example we have created WebApplication class that inherits from the QMainWindow class. we have set the window title and geometry, after that we creates QWebEngineView object, set its URL to GeeksCoders homepage and set the central widget of the main window to the web browser widget.
Run the code and this will be the result
So In this article we have seen that how to use PyQt5 to build web applications. we have started by setting up our environment and creating simple web application that displays a webpage. after that we have learned how to handle JavaScript in web application using PyQt5.
Learn More on PyQt5
- How to Deploy PyQt5 Applications
- How to Integrate PyQt5 with OpenCV
- How to Use Stylesheets in PyQt5
- Learn PyQt5 Event Handling with Signals and Slots
- PyQt5 Tutorial: Creating Menus and Toolbars
- How to Open & Save Files in Python PyQt5
- PyQt5 Designer Tutorial: Complete Guide to Qt Designer
- PyQt5 vs Tkinter: Which GUI Library is Right for You