In this article we will see how we can create a PyQt5 application which tells about the cases of corona around the world i.e number of confirmed cases, number of cases in which patient has recovered and total deaths due to corona.
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. Create a push-button set its geometry
2. Create three labels to show information about total cases, recovered cases and death cases
3. Set geometry, alignment and border to each label
4. Add action to the push button
5. Inside the action scrap the data from the website with the help of requests BeautifulSoup
6. Convert the raw data into html code and then filter it to get the required output
7. Show the output the screen with the help of labels
Below is the implementation
Python3 1==
# importing librariesfromPyQt5.QtWidgetsimport*fromPyQt5importQtCore,QtGuifromPyQt5.QtGuiimport*fromPyQt5.QtCoreimport*frombs4importBeautifulSoupasBSimportrequestsimportsysclassWindow(QMainWindow):def__init__(self):super().__init__()# setting titleself.setWindowTitle("Python ")# setting geometryself.setGeometry(100,100,400,500)# calling methodself.UiComponents()# showing all the widgetsself.show()# method for widgetsdefUiComponents(self):# create push button to perform functionpush=QPushButton("Press",self)# setting geometry to the push buttonpush.setGeometry(125,100,150,40)# creating label to show the total casesself.label_total=QLabel("Total Cases ",self)# setting geometryself.label_total.setGeometry(100,200,200,40)# setting alignment to the textself.label_total.setAlignment(Qt.AlignCenter)# adding border to the labelself.label_total.setStyleSheet("border : 2px solid black;")# creating label to show the recovered casesself.label_reco=QLabel("Recovered Cases ",self)# setting geometryself.label_reco.setGeometry(100,250,200,40)# setting alignment to the textself.label_reco.setAlignment(Qt.AlignCenter)# adding borderself.label_reco.setStyleSheet("border : 2px solid black;")# creating label to show death casesself.label_death=QLabel("Total Deaths ",self)# setting geometryself.label_death.setGeometry(100,300,200,40)# setting alignment to the textself.label_death.setAlignment(Qt.AlignCenter)# adding border to the labelself.label_death.setStyleSheet("border : 2px solid black;")# adding action to the push buttonpush.clicked.connect(self.get_cases)# method called by pushdefget_cases(self):# getting the request from urldata=requests.get("https://www.worldometers.info/coronavirus/")# converting the textsoup=BS(data.text,'html.parser')# finding meta info for total casestotal=soup.find("div",class_="maincounter-number").text# filtering ittotal=total[1:len(total)-2]# finding meta info for other numbersother=soup.find_all("span",class_="number-table")# getting recovered cases numberrecovered=other[2].text# getting death cases numberdeaths=other[3].text# filtering the datadeaths=deaths[1:]self.label_total.setText("Total Cases : "+total)self.label_reco.setText("Recovered Cases : "+recovered)self.label_death.setText("Total Deaths : "+deaths)# create pyqt5 appApp=QApplication(sys.argv)# create the instance of our Windowwindow=Window()window.show()# start the appsys.exit(App.exec())