This Python Flask web application allows users to download historical stock data in Excel format using the Yahoo Finance API (yfinance). The user simply enters a stock ticker symbol and selects a date range, and the app fetches the stock data for that range, converting it into an Excel file ready for download.
- Stock Data Download: Allows users to enter a stock ticker, a start date, and an end date to retrieve historical stock data.
- Excel Export: The stock data is exported in an Excel format for easy offline analysis.
- User-Friendly Interface: Simple web interface for entering the stock ticker and date range.
Before running the application, ensure that you have the following installed:
- Python 3.x
- Flask
- yfinance
- pandas
- openpyxl
To install the required dependencies, run:
pip install flask yfinance pandas openpyxlThe project consists of two main components:
- Flask App (Python): This file contains the logic to handle user input, fetch stock data, and generate the Excel file.
- HTML Template (index.html): This is the front-end form where users can input stock tickers and select date ranges.
stock-data-downloader/
│
├── app.py # Python Flask application file
├── templates/
│ └── index.html # HTML template for the user interface
└── requirements.txt # List of dependencies
- Clone or download the repository.
- Navigate to the project directory.
- Run the Flask application using the following command:
python app.py- Open your web browser and go to
http://127.0.0.1:5000/. - Enter a stock ticker, start date, and end date, then click Download Data to get the stock data in Excel format.
-
Imports:
Flask,render_template,request, andsend_filefromflaskhandle web server functionality.yfinancefetches stock data from Yahoo Finance.pandasis used to convert the data into an Excel file format.BytesIOis used to handle the in-memory byte stream for sending the file.
-
Routes:
/: The homepage where the user can input the stock ticker and date range./download: A POST route that handles the data download by fetching the stock data and converting it into an Excel file.
-
Logic:
- The
downloadroute processes the form input (stock ticker, start date, and end date). - It fetches the stock data using
yfinanceand checks if the data is empty. - If data is available, it is converted into an Excel file using
pandas.ExcelWriter. - The file is then sent to the user as a downloadable Excel file.
- The
The HTML template provides a simple form with the following inputs:
- Stock Ticker: A text input for the user to enter the stock symbol (e.g., "AAPL").
- Start Date: A date input for selecting the start date.
- End Date: A date input for selecting the end date.
When the form is submitted, the data is sent to the /download route in a POST request.
- Muntaqim Asbuch
- GitHub: @shemilael
- Instagram: @shemilael
This project is licensed under the MIT License - see the LICENSE file for details.