In this Python Kivy tutorial we are going to learn about How to Integrate Yahoo Finance with Python Kivy, Kivy is Python framework used for building cross platform applications. it provides graphical user interface (GUI) toolkit that makes it easy to create and customize GUI elements such as buttons, labels and text fields. in this article we want to show you how to use Kivy to create a simple application that retrieves real time stock prices using the Yahoo Finance API.
How to Integrate Yahoo Finance with Python Kivy
First of all we need to install required libraries.
1 |
pip install kivy yfinance |
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 |
import kivy from kivy.app import App from kivy.uix.label import Label import yfinance as yf class StockPriceApp(App): def build(self): # Define the stock symbol stock_symbol = "MSFT" # Get the stock data stock_data = yf.Ticker(stock_symbol) # Get the stock price stock_price = stock_data.info["regularMarketPrice"] # Create a label to display the stock price label = Label(text=f"Current stock price for {stock_symbol} is {stock_price}", font_size=30) return label if __name__ == "__main__": StockPriceApp().run() |
In the above example we have imported Kivy and the necessary modules from Kivy to create the app. after that we imports yfinance library. inside the build method of the StockPriceApp class, we have defined stock symbol as MSFT, which is the stock symbol for Microsoft. and then we have used yf.Ticker() method to retrieve the stock data for Microsoft.
Next we use the info attribute of the stock_data object to retrieve the current stock price, which is stored in regularMarketPrice key. and after we creates a label to display the stock price using f-strings to include the stock symbol and stock price in the text of the label.
and lastly we return the label from the build method, which will display the stock price when the app is run. you can modify this code to retrieve stock prices for other publicly traded companies by changing the stock_symbol variable to the appropriate stock symbol. You can also customize the label or add additional UI elements to the app as desired.
Run the code and this will be the result
Learn More on Python GUI
- How to Create Print Dialog in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- 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