In this tutorial, we will guide it through the construction of an advanced tool for financial data reports in Google Colab through the combination of multiple python libraries. You will learn to scrape live financial data from web pages, you will recover data from historical actions using Yfinance and visualize trends with Matplootlib. In addition, the tutorial demonstrates how to integrate an interactive user interface using ipywidgets, culminating in a dynamic PDF report generated with FPDF.
!pip install fpdf beautifulsoup4 yfinance ipywidgets
First, we install the necessary libraries for our project: FPDF to generate PDF reports, Beautifyup4 for web scraping, Yfinance to recover historical financial data and ipywidgets to create interactive user interface elements in the notebook.
import requests
from bs4 import BeautifulSoup
from fpdf import FPDF
import yfinance as yf
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display, FileLink
Here, we import a range of libraries to create a comprehensive financial data tool.
def generate_report(b):
symbol = symbol_text.value.upper().strip()
start_date = start_date_picker.value
end_date = end_date_picker.value
output_area.clear_output() # Clear previous outputs
if not (symbol and start_date and end_date):
with output_area:
print("Please provide valid inputs for stock symbol and both dates.")
return
with output_area:
print(f"Generating report for {symbol} from {start_date} to {end_date}...")
# 1. Retrieve current price using yfinance
try:
stock = yf.Ticker(symbol)
current_price = stock.info.get('regularMarketPrice', 'N/A')
except Exception as e:
current_price = "Error retrieving price"
with output_area:
print("Error retrieving current price:", e)
# 2. Fetch historical data using yfinance
try:
hist = stock.history(start=start_date, end=end_date)
except Exception as e:
hist = None
with output_area:
print("Error fetching historical data:", e)
# 3. Plot historical closing prices
if hist is not None and not hist.empty:
plt.figure(figsize=(10, 5))
plt.plot(hist.index, hist('Close'), marker="o", linestyle="-", label="Close Price")
plt.title(f"{symbol} Historical Closing Prices")
plt.xlabel("Date")
plt.ylabel("Close Price (USD)")
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
graph_filename = "graph.png"
plt.savefig(graph_filename)
plt.show()
else:
graph_filename = None
with output_area:
print("No historical data available for the selected date range.")
# 4. Create a PDF report using FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", "B", 16)
pdf.cell(0, 10, f"Financial Report for {symbol}", ln=True, align="C")
pdf.ln(10)
pdf.set_font("Arial", size=12)
pdf.cell(0, 10, f"Current Price: {current_price}", ln=True)
pdf.cell(0, 10, f"Date Range: {start_date} to {end_date}", ln=True)
pdf.ln(10)
if graph_filename:
pdf.cell(0, 10, "Historical Closing Prices:", ln=True)
# Adjust the image width to fit the page layout
pdf.image(graph_filename, w=180)
pdf_filename = "financial_report.pdf"
pdf.output(pdf_filename)
# 5. Display the download link for the PDF report
with output_area:
print(f"PDF Report generated: {pdf_filename}")
display(FileLink(pdf_filename))
With the previous function, we recover the users' entries for the stock symbol and the range of dates, then it scrapes the current financial data of Yahoo Finance while obtaining historical data through Yfinance. Trace the historical closing prices using Matpletlib, generates a PDF report that embeds the scraped data and the graph using FPDF, and finally shows a discharge link for the PDF report.
# Create UI widgets
symbol_text = widgets.Text(
value="AAPL",
description="Stock Symbol:",
placeholder="e.g., AAPL"
)
start_date_picker = widgets.DatePicker(
description='Start Date'
)
end_date_picker = widgets.DatePicker(
description='End Date'
)
generate_button = widgets.Button(
description="Generate Report",
button_style="success"
)
output_area = widgets.Output()
generate_button.on_click(generate_report)
display(widgets.VBox((symbol_text, start_date_picker, end_date_picker, generate_button, output_area)))
Finally, this code block establishes an interactive user interface using ipywidgets. Create input fields for a stock symbol, date collection for a start and completion date, and a button to activate the generation of reports. The elements of the user interface are organized vertically using a VBOX design, and an output area is provided to show comments and the generated PDF discharge link.
PDF output and sample
In conclusion, following this tutorial, the web scraping, data analysis, the design of the interactive user interface and the generation of PDF reports in a single Google Colab notebook has successfully integrated. This step -by -step process illustrates how to take advantage of the power of the various Python libraries to create a robust and easy -to -use financial data tool.
Here is the Colab notebook For the previous project. Besides, don't forget to follow us <a target="_blank" href="https://x.com/intent/follow?screen_name=marktechpost” target=”_blank” rel=”noreferrer noopener”>twitter and join our Telegram channel and LINKEDIN GRsplash. Do not forget to join our 80k+ ml subject.
Recommended Reading Reading IA Research Liberations: An advanced system that integrates the ai system and data compliance standards to address legal concerns in IA data sets

Asif Razzaq is the CEO of Marktechpost Media Inc .. as a visionary entrepreneur and engineer, Asif undertakes to take advantage of the potential of artificial intelligence for the social good. Its most recent effort is the launch of an artificial intelligence media platform, Marktechpost, which stands out for its deep coverage of automatic learning and deep learning news that is technically solid and easily understandable by a broad audience. The platform has more than 2 million monthly views, illustrating its popularity among the public.