Skip to content
Related Articles
Open in App
Not now

Related Articles

Corona Virus Details in India – Using Python PyQt5

Improve Article
Save Article
  • Last Updated : 29 Dec, 2020
Improve Article
Save Article

In this article, we will see how we can create a PyQt5 application that tells about the details of coronavirus cases i.e total cases, recovered cases and total deaths across India i.e state wise result.

Modules required and Installation:

PyQt5 : PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing.

Requests : Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs.

Beautiful Soup: Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.

Implementations steps :

1. Get coronavirus state wise list and store it in a list
2. Create a non-editable combo box, set its geometry
3. From list get the state names and add it to the combo box
4. Create three labels to show information about total cases, recovered cases and death cases
5. Set geometry, alignment and border to each label
6. Add action to the combo box
7. Inside the action filter the list to get desired information
8. Show the output the screen with the help of labels

Below is the implementation

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
from bs4 import BeautifulSoup
import requests
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 400, 500)
        # calling method
        # calling method
        # showing all the widgets
    def corona(self):
        extract_contents = lambda row: [x.text.replace('\n', '') for x in row]
        URL = ''
        SHORT_HEADERS = ['SNo', 'State', 'Indian-Confirmed',
                         'Foreign-Confirmed', 'Cured', 'Death']
        response = requests.get(URL).content
        soup = BeautifulSoup(response, 'html.parser')
        header = extract_contents('th'))
        self.stats = []
        all_rows = soup.find_all('tr')
        for row in all_rows:
            stat = extract_contents(row.find_all('td'))
            if stat:
                if len(stat) == 5:
                    # last row
                    stat = ['', *stat]
                elif len(stat) == 6:
        self.stats[-1][1] = "Total Cases"
        # method for widgets
    def UiComponents(self):
        # creating a combo box widget
        self.combo_box = QComboBox(self)
        # setting geometry to combo box
        self.combo_box.setGeometry(100, 50, 200, 40)
        # setting font
        self.combo_box.setFont(QFont('Times', 10))
        # adding items to combo box
        for i in self.stats:
        # adding action to the combo box
        # creating label to show the total cases
        self.label_total = QLabel("Total Cases ", self)
        # setting geometry
        self.label_total.setGeometry(100, 300, 200, 40)
        # setting alignment to the text
        # adding border to the label
        self.label_total.setStyleSheet("border : 2px solid black;")
        # creating label to show the recovered cases
        self.label_reco = QLabel("Recovered Cases ", self)
        # setting geometry
        self.label_reco.setGeometry(100, 350, 200, 40)
        # setting alignment to the text
        # adding border
        self.label_reco.setStyleSheet("border : 2px solid black;")
        # creating label to show death cases
        self.label_death = QLabel("Total Deaths ", self)
        # setting geometry
        self.label_death.setGeometry(100, 400, 200, 40)
        # setting alignment to the text
        # adding border to the label
        self.label_death.setStyleSheet("border : 2px solid black;")
    # method called by push
    def get_cases(self):
        # getting index
        index = self.combo_box.currentIndex()
        # getting data
        total = self.stats[index][3]
        recovered = self.stats[index][4]
        deaths = self.stats[index][5]
        # show data through labels
        self.label_total.setText("Total Cases : " + total)
        self.label_reco.setText("Recovered Cases : " + recovered)
        self.label_death.setText("Total Deaths : " + deaths)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!