In this PyQt6 article we want to learn How to Integrate Yahoo Finance API with PyQt6, for this we are using yfinance API, now first of all let’s talk about PyQt6.
What is PyQt6 ?
PyQt6 is a Python binding for cross platform GUI toolkit Qt. Qt is popular C++ framework for building graphical applications which provides different widgets, layouts and other tools for creating desktop and mobile applications. PyQt6 allows developers to create GUI applications in Python that can run on multiple platforms including Windows, Linux and macOS. it provides different Python modules that wrap the Qt classes and allow developers to use them in more Pythonic way.
What is Yahoo Finance API (yfinance) ?
yfinance is popular Python library that provides easy API for accessing historical market data and live financial data from Yahoo Finance. it allows users to fetch data for stock quotes, options, mutual funds, exchange traded funds (ETFs) and cryptocurrencies. yfinance is built on top of the Pandas library and is widely used for financial analysis and data science.
How to Integrate Yahoo Finance API with PyQt6
First if all we need to install required libraries
1 2 |
pip install yfinance pip install PyQt6 |
So this is the complete code for this article
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import sys import yfinance as yf from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem class StockTrackerApp(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('GeeksCoders - Stock Tracker') self.setGeometry(100, 100, 800, 600) # Add label and text box self.label1 = QLabel('Enter Stock Symbol:', self) self.label1.move(50, 50) self.textbox = QLineEdit(self) self.textbox.move(200, 50) self.textbox.resize(100, 25) # Add button self.button = QPushButton('Fetch Stock Data', self) self.button.move(350, 50) self.button.clicked.connect(self.fetch_stock_data) # Add table widget for stock data self.table = QTableWidget(self) self.table.move(50, 100) self.table.resize(700, 400) def fetch_stock_data(self): # Get stock symbol from text box stock_symbol = self.textbox.text() # Fetch stock data stock_data = yf.Ticker(stock_symbol).history(period='max') # Set number of rows and columns in table widget self.table.setRowCount(len(stock_data.index)) self.table.setColumnCount(len(stock_data.columns)) # Set column headers column_headers = stock_data.columns.tolist() self.table.setHorizontalHeaderLabels(column_headers) # Set column widths for i in range(len(column_headers)): self.table.setColumnWidth(i, 200) # Add data to table widget for i in range(len(stock_data.index)): for j in range(len(stock_data.columns)): self.table.setItem(i, j, QTableWidgetItem(str(stock_data.iloc[i, j]))) if __name__ == '__main__': app = QApplication(sys.argv) stock_tracker = StockTrackerApp() stock_tracker.show() sys.exit(app.exec()) |
This code defines PyQt6 application for fetching and displaying stock data from the Yahoo Finance API using yfinance library. StockTrackerApp class extends from QMainWindow class and defines the GUI components of the application, including a label and text box for entering a stock symbol, a button for fetching the stock data, and a table widget for displaying the data.
fetch_stock_data method is called when the user clicks Fetch Stock Data button. this method gets the stock symbol from the text box, fetches the stock data using yf.Ticker method, and then populates the table widget with the fetched data.
setRowCount and setColumnCount methods are used to set the number of rows and columns in the table widget. setHorizontalHeaderLabels method is used to set the column headers to the names of the columns in fetched data. setColumnWidth method is used to set the width of each column in the table widget.
setItem method is used to add each item in the fetched data to the appropriate cell in the table widget. str function is used to convert each item to string before adding it to table widget.
and lastly if __name__ == ‘__main__’ block creates QApplication object, creates an instance of StockTrackerApp class, shows the GUI window and starts the application event loop using app.exec().
So now run the code and in here we want to use Microsoft symbol and that is MSFT
Learn More on Python GUI
- Qt Designer Tool in PyQt6
- How to Use Stylesheets in Python PyQt6
- Event Handling in Python and PyQt6
- Responsive Applications with PyQt6 Multithreading
- How to Deploy PyQt6 Applications
- How to Build Web Applications with PyQt6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6