import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import base64 import yfinance as yf import streamlit as st st.set_option('deprecation.showPyplotGlobalUse', False) st.title('Scrapping Yahoo Finance App') st.markdown(""" This app retrieves the list of the S&P 500 (from Wikipedia) and its corresponding stock closing price (year-to-date)! * Python libraries: base64, pandas, streamlit, numpy, matplotlib, seaborn * Data source: [Wikipedia](https://en.wikipedia.org/wiki/List_of_S%26P_500_companies). """) st.sidebar.header('User Input Features') #Scrappage des donnees sur Wikipedia @st.cache_data def load_data(): url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies" html = pd.read_html(url,header= 0) df = html[0] return df df = load_data() sector = df.groupby('GICS Sector') #Sidebar - Sector Selection sorted_sector_unique = sorted(df['GICS Sector'].unique()) selected_sector = st.sidebar.multiselect('Sector',sorted_sector_unique) #Filtering datas df_selected_sector = df[(df['GICS Sector'].isin(selected_sector))] st.header('Display companies in Selected sector') st.write('Data Dimension: ' + str(df_selected_sector.shape[0]) + ' rows and ' + str(df_selected_sector.shape[1]) + ' columns.') st.dataframe(df_selected_sector) #Download des datas selectionnees dans le sidebar def filedownload(df): csv = df.to_csv(index=False) b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions href = f'Download CSV File' return href st.markdown(filedownload(df_selected_sector), unsafe_allow_html=True) #Download datas correspondantes de yahoo finance data = yf.download( tickers = list(df_selected_sector[:10].Symbol), period="ytd", interval="1d", group_by="ticker", auto_adjust=True, prepost=True, threads=True, proxy=None ) #Plot Closing price of Selected Symbols def price_plot(symbol): df = pd.DataFrame(data[symbol].Close) df['Date'] = df.index plt.fill_between(df.Date, df.Close, color='skyblue', alpha=0.3) plt.plot(df.Date, df.Close, color='skyblue', alpha=0.8) plt.xticks(rotation=90) plt.title(symbol, fontweight='bold') plt.xlabel('Date', fontweight='bold') plt.ylabel('Closing Price', fontweight='bold') return st.pyplot() num_company = st.sidebar.slider('Number of companies',1,5) if st.button('Show plots'): st.header('Show Closing price') for i in list(df_selected_sector.Symbol)[:num_company]: price_plot(i)