abdouramandalil commited on
Commit
4a55bf3
1 Parent(s): 47730a5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ import base64
6
+ import yfinance as yf
7
+ import streamlit as st
8
+ st.set_option('deprecation.showPyplotGlobalUse', False)
9
+
10
+ st.title('Scrapping Yahoo Finance App')
11
+ st.markdown("""
12
+ This app retrieves the list of the S&P 500 (from Wikipedia) and its corresponding stock closing price (year-to-date)!
13
+ * Python libraries: base64, pandas, streamlit, numpy, matplotlib, seaborn
14
+ * Data source: [Wikipedia](https://en.wikipedia.org/wiki/List_of_S%26P_500_companies).
15
+ """)
16
+ st.sidebar.header('User Input Features')
17
+
18
+ #Scrappage des donnees sur Wikipedia
19
+ @st.cache_data
20
+ def load_data():
21
+ url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
22
+ html = pd.read_html(url,header= 0)
23
+ df = html[0]
24
+ return df
25
+ df = load_data()
26
+ sector = df.groupby('GICS Sector')
27
+
28
+ #Sidebar - Sector Selection
29
+ sorted_sector_unique = sorted(df['GICS Sector'].unique())
30
+ selected_sector = st.sidebar.multiselect('Sector',sorted_sector_unique)
31
+
32
+
33
+ #Filtering datas
34
+ df_selected_sector = df[(df['GICS Sector'].isin(selected_sector))]
35
+ st.header('Display companies in Selected sector')
36
+ st.write('Data Dimension: ' + str(df_selected_sector.shape[0]) + ' rows and ' + str(df_selected_sector.shape[1]) + ' columns.')
37
+ st.dataframe(df_selected_sector)
38
+
39
+ #Download des datas selectionnees dans le sidebar
40
+ def filedownload(df):
41
+ csv = df.to_csv(index=False)
42
+ b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
43
+ href = f'<a href="data:file/csv;base64,{b64}" download="SP500.csv">Download CSV File</a>'
44
+ return href
45
+
46
+ st.markdown(filedownload(df_selected_sector), unsafe_allow_html=True)
47
+
48
+ #Download datas correspondantes de yahoo finance
49
+ data = yf.download(
50
+ tickers = list(df_selected_sector[:10].Symbol),
51
+ period="ytd",
52
+ interval="1d",
53
+ group_by="ticker",
54
+ auto_adjust=True,
55
+ prepost=True,
56
+ threads=True,
57
+ proxy=None
58
+ )
59
+ #Plot Closing price of Selected Symbols
60
+ def price_plot(symbol):
61
+ df = pd.DataFrame(data[symbol].Close)
62
+ df['Date'] = df.index
63
+ plt.fill_between(df.Date, df.Close, color='skyblue', alpha=0.3)
64
+ plt.plot(df.Date, df.Close, color='skyblue', alpha=0.8)
65
+ plt.xticks(rotation=90)
66
+ plt.title(symbol, fontweight='bold')
67
+ plt.xlabel('Date', fontweight='bold')
68
+ plt.ylabel('Closing Price', fontweight='bold')
69
+ return st.pyplot()
70
+
71
+ num_company = st.sidebar.slider('Number of companies',1,5)
72
+
73
+ if st.button('Show plots'):
74
+ st.header('Show Closing price')
75
+ for i in list(df_selected_sector.Symbol)[:num_company]:
76
+ price_plot(i)