generated from streamlit/streamlit-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHello.py
287 lines (251 loc) · 10 KB
/
Hello.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
""" APP """
import json
import base64
import streamlit as st
# from PIL import Image
import pandas as pd
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import requests
# import time
# ---------------------------------#
# New feature (make sure to upgrade your streamlit library)
# pip install --upgrade streamlit
# ---------------------------------#
# Page layout
## Page expands to full width
st.set_page_config(layout="wide")
# ---------------------------------#
# Title
# image = Image.open("logo.jpg")
# st.image(image, width=500)
st.sidebar.header("KRİPTO FİYAT LİSTESİ")
st.title("Crypto Price App")
st.markdown(
"""
Bu uygulama **CoinMarketCap**'ten en iyi 100 kripto para birimi için kripto para birimi fiyatlarını alır!
"""
)
# ---------------------------------#
# About
expander_bar = st.expander("About")
expander_bar.markdown(
"""
* **Python libraries:** base64, pandas, streamlit, numpy, matplotlib, seaborn, BeautifulSoup, requests, json, time
* **Data source:** [CoinMarketCap](http://coinmarketcap.com).
"""
)
# ---------------------------------#
# Page layout (continued)
## Divide page to 3 columns (col1 = sidebar, col2 and col3 = page contents)
col1 = st.sidebar
col2, col3 = st.columns((2, 1))
# ---------------------------------#
# Sidebar + Main panel
col1.header("Ayarlar")
## Sidebar - Currency price unit
currency_price_unit = col1.selectbox(
" Fiyat için para birimini seçin", ("USD", "BTC", "")
)
# Web scraping of CoinMarketCap data
# @st.cache_data
def load_data():
"""Fetch data from CoinMarketCap"""
try:
cmc = requests.get("https://coinmarketcap.com", timeout=10)
soup = BeautifulSoup(cmc.content, "html.parser")
data = soup.find("script", id="__NEXT_DATA__", type="application/json")
# print(data)
coin_data = json.loads(data.contents[0])
coin_data1 = json.loads(coin_data["props"]["initialState"])
listings = coin_data1["cryptocurrency"]["listingLatest"]["data"]
st.json(listings)
except requests.exceptions.RequestException as e:
print(e)
return None
name_indis = listings[0]["keysArr"].index("name")
symbol_indis = listings[0]["keysArr"].index("symbol")
price_indis = listings[0]["keysArr"].index("quote.USD.price")
change_1h_indis = listings[0]["keysArr"].index("quote.USD.percentChange1h")
change_24h_indis = listings[0]["keysArr"].index("quote.USD.percentChange24h")
change_7d_indis = listings[0]["keysArr"].index("quote.USD.percentChange7d")
market_cap_indis = listings[0]["keysArr"].index("quote.USD.marketCap")
volume_24h_indis = listings[0]["keysArr"].index("quote.USD.volume24h")
change_30d_indis = listings[0]["keysArr"].index("quote.USD.percentChange30d")
change_60d_indis = listings[0]["keysArr"].index("quote.USD.percentChange60d")
change_90d_indis = listings[0]["keysArr"].index("quote.USD.percentChange90d")
change_1y_indis = listings[0]["keysArr"].index("quote.USD.percentChange1y")
change_ytd_indis = listings[0]["keysArr"].index(
"quote.USD.ytdPriceChangePercentage"
)
return pd.DataFrame(
{
"coin_name": [i[name_indis] for i in listings[1:]],
"coin_symbol": [i[symbol_indis] for i in listings[1:]],
"price": [i[price_indis] for i in listings[1:]],
"percent_change_1h": [i[change_1h_indis] for i in listings[1:]],
"percent_change_24h": [i[change_24h_indis] for i in listings[1:]],
"percent_change_7d": [i[change_7d_indis] for i in listings[1:]],
"percent_change_30d": [i[change_30d_indis] for i in listings[1:]],
"percent_change_60d": [i[change_60d_indis] for i in listings[1:]],
"percent_change_90d": [i[change_90d_indis] for i in listings[1:]],
"percent_change_1y": [i[change_1y_indis] for i in listings[1:]],
"percent_change_ytd": [i[change_ytd_indis] for i in listings[1:]],
"market_cap": [i[market_cap_indis] for i in listings[1:]],
"volume_24h": [i[volume_24h_indis] for i in listings[1:]],
}
)
df = load_data()
## Sidebar - Cryptocurrency selections
sorted_coin = sorted(df["coin_symbol"])
selected_coin = col1.multiselect("Cryptocurrency", sorted_coin, sorted_coin)
df_selected_coin = df[(df["coin_symbol"].isin(selected_coin))] # Filtering data
## Sidebar - Number of coins to display
num_coin = col1.slider("Kaç Coin Gösterilsin", 1, 100, 100)
df_coins = df_selected_coin[:num_coin]
## Sidebar - Percent change timeframe
percent_timeframe = col1.selectbox(
"Zaman aralığı", ["7d", "24h", "1h", "30d", "60d", "90d", "1y", "ytd"]
)
percent_dict = {
"7d": "percent_change_7d",
"24h": "percent_change_24h",
"1h": "percent_change_1h",
"30d": "percent_change_30d",
"60d": "percent_change_60d",
"90d": "percent_change_90d",
"1y": "percent_change_1y",
"ytd": "percent_change_ytd",
}
selected_percent_timeframe = percent_dict[percent_timeframe]
## Sidebar - Sorting values
sort_values = col1.selectbox("Sort values?", ["Yes", "No"])
col2.subheader("Price Data of Selected Cryptocurrency")
col2.write(
"Data Dimension: "
+ str(df_selected_coin.shape[0])
+ " rows and "
+ str(df_selected_coin.shape[1])
+ " columns."
)
col2.dataframe(df_coins)
# Download CSV data
# https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806
def filedownload(data):
"""download csv file"""
csv = data.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions
return f'<a href="data:file/csv;base64,{b64}" download="crypto.csv">Download CSV File</a>'
col2.markdown(filedownload(df_selected_coin), unsafe_allow_html=True)
# ---------------------------------#
# Preparing data for Bar plot of % Price change
col2.subheader("Table of % Price Change")
df_change = pd.concat(
[
df_coins.coin_symbol,
df_coins.percent_change_1h,
df_coins.percent_change_24h,
df_coins.percent_change_7d,
df_coins.percent_change_30d,
df_coins.percent_change_60d,
df_coins.percent_change_90d,
df_coins.percent_change_1y,
df_coins.percent_change_ytd,
],
axis=1,
)
df_change = df_change.set_index("coin_symbol")
# print(type(df_change["percent_change_90d"]))
# print(df_change["percent_change_90d"])
df_change["positive_percent_change_1h"] = df_change["percent_change_1h"] > 0
df_change["positive_percent_change_24h"] = df_change["percent_change_24h"] > 0
df_change["positive_percent_change_7d"] = df_change["percent_change_7d"] > 0
df_change["positive_percent_change_30d"] = df_change["percent_change_30d"] > 0
df_change["positive_percent_change_60d"] = df_change["percent_change_60d"] > 0
df_change["positive_percent_change_90d"] = df_change["percent_change_90d"] > 0
df_change["positive_percent_change_1y"] = df_change["percent_change_1y"] > 0
df_change["positive_percent_change_ytd"] = df_change["percent_change_ytd"] > 0
col2.dataframe(df_change)
# Conditional creation of Bar plot (time frame)
col3.subheader("Bar plot of % Price Change")
if percent_timeframe == "7d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_7d"])
col3.write("*7 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_7d"].plot(
kind="barh",
color=df_change.positive_percent_change_7d.map({True: "g", False: "r"}),
)
elif percent_timeframe == "24h":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_24h"])
col3.write("*24 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_24h"].plot(
kind="barh",
color=df_change.positive_percent_change_24h.map({True: "g", False: "r"}),
)
elif percent_timeframe == "30d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_30d"])
col3.write("*30 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_30d"].plot(
kind="barh",
color=df_change.positive_percent_change_30d.map({True: "g", False: "r"}),
)
elif percent_timeframe == "60d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_60d"])
col3.write("*60 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_60d"].plot(
kind="barh",
color=df_change.positive_percent_change_60d.map({True: "g", False: "r"}),
)
elif percent_timeframe == "90d":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_90d"])
col3.write("*90 days period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_90d"].plot(
kind="barh",
color=df_change.positive_percent_change_90d.map({True: "g", False: "r"}),
)
elif percent_timeframe == "1y":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_1y"])
col3.write("*1 year period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_1y"].plot(
kind="barh",
color=df_change.positive_percent_change_1y.map({True: "g", False: "r"}),
)
elif percent_timeframe == "ytd":
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_ytd"])
col3.write("*Year to date period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_ytd"].plot(
kind="barh",
color=df_change.positive_percent_change_ytd.map({True: "g", False: "r"}),
)
else:
if sort_values == "Yes":
df_change = df_change.sort_values(by=["percent_change_1h"])
col3.write("*1 hour period*")
plt.figure(figsize=(5, 25))
plt.subplots_adjust(top=1, bottom=0)
df_change["percent_change_1h"].plot(
kind="barh",
color=df_change.positive_percent_change_1h.map({True: "g", False: "r"}),
)
col3.pyplot(plt)