-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (54 loc) · 1.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from flask import Flask, render_template
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from io import BytesIO
import base64
from bs4 import BeautifulSoup
import requests
#don't change this
matplotlib.use('Agg')
app = Flask(__name__) #do not change this
#insert the scrapping here
url_get = requests.get('https://www.coingecko.com/en/coins/ethereum/historical_data/usd?start_date=2020-01-01&end_date=2021-06-30#panel')
soup = BeautifulSoup(url_get.content,"html.parser")
#find your right key here
table = soup.find('table', attrs={'class':'table table-striped text-sm text-lg-normal'})
date_row = table.find_all('th', attrs={'class':'font-semibold text-center'})
date_row_length = len(date_row)
temp = [] #initiating a list
for i in range(0, date_row_length):
#insert the scrapping process here
# get date
date = table.find_all('th', attrs={'class':'font-semibold text-center'})[i].text
# get volume
# clean the ' ', '\n', ',', and '$' while at it
vol = table.find_all('td', attrs={'class':'text-center'})[(i*4)+1].text.replace("\n","").replace(",","").replace("$","").replace(" ","")
temp.append((date,vol))
temp = temp[::-1]
#change into dataframe
eth = pd.DataFrame(temp, columns = ('Date','Volume'))
#insert data wrangling here
eth = eth.astype({"Date": "datetime64",
"Volume":"int64"})
eth = eth.set_index('Date')
#end of data wranggling
@app.route("/")
def index():
card_data = f'{eth["Volume"].mean().round(2)}' #be careful with the " and '
# generate plot
ax = eth.plot(figsize = (20,9))
# Rendering plot
# Do not change this
figfile = BytesIO()
plt.savefig(figfile, format='png', transparent=True)
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
plot_result = str(figdata_png)[2:-1]
# render to html
return render_template('index.html',
card_data = card_data,
plot_result=plot_result
)
if __name__ == "__main__":
app.run(debug=True)