-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
237 lines (189 loc) · 9.28 KB
/
main.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
# import streamlit as st
# import requests
# # Your backend functions
# def jina(url):
# base_url = "https://r.jina.ai/"
# url = base_url + url
# response = requests.get(url)
# return response.text
# def price_cities(url):
# text = jina(url)
# pos1 = text.find('**')
# new = text[:pos1]
# twentytwok = new[int(new.find('22K')):int(new.find('24K'))]
# value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:]
# twentyfourk = new[int(new.find('24K')):int(new.find('18K'))]
# value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:]
# eighteenk = new[int(new.find('18K')):]
# value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:]
# return value_22k, value_24k, value_18k
# # City list
# cities = ['Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai',
# 'Coimbatore', 'Delhi', 'Hyderabad', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow',
# 'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna',
# 'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam']
# # Streamlit app
# st.title('Gold Rates in Indian States')
# # Dropdown for selecting the city
# selected_city = st.selectbox('Select a City', cities)
# # Fetch and display gold rates when a city is selected
# if selected_city:
# city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
# # Fetch the prices using your backend function
# try:
# x, y, z = price_cities(city_url)
# st.write(f"Gold rates in {selected_city}:")
# st.write(f"22K Gold: ₹{x}")
# st.write(f"24K Gold: ₹{y}")
# st.write(f"18K Gold: ₹{z}")
# except:
# st.error("Could not fetch the gold rates. Please try again.")
# import streamlit as st
# import requests
# import pandas as pd
# # Backend functions
# def jina(url):
# base_url = "https://r.jina.ai/"
# url = base_url + url
# response = requests.get(url)
# return response.text
# def price_cities(url):
# text = jina(url)
# pos1 = text.find('**')
# new = text[:pos1]
# twentytwok = new[int(new.find('22K')):int(new.find('24K'))]
# value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:]
# twentyfourk = new[int(new.find('24K')):int(new.find('18K'))]
# value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:]
# eighteenk = new[int(new.find('18K')):]
# value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:]
# return float(value_22k.replace(',', '')), float(value_24k.replace(',', '')), float(value_18k.replace(',', ''))
# # City list
# cities = ['Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai',
# 'Coimbatore', 'Delhi', 'Hyderabad', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow',
# 'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna',
# 'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam']
# # Streamlit UI
# st.set_page_config(page_title="Gold Rates in Indian States", layout="wide")
# # Sidebar with project and developer info
# st.sidebar.title("Project Info")
# st.sidebar.write("This project fetches current gold rates for 24K, 22K, and 18K gold from GoodReturns for 28 Indian states. The rates for 1g, 8g, and 10g are displayed.")
# st.sidebar.write("**Developed by:**")
# st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)")
# st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
# # Dropdown for selecting a city
# st.title('Gold Rates in Indian States')
# selected_city = st.selectbox('Select a City', cities)
# Fetch and display gold rates when a city is selected
# if selected_city:
# city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
# try:
# # Fetch the gold prices using your backend function
# price_22k, price_24k, price_18k = price_cities(city_url)
# # Prepare data for table
# data = {
# 'Karat': ['22K', '24K', '18K'],
# '1g (₹)': [price_22k, price_24k, price_18k],
# '8g (₹)': [price_22k * 8, price_24k * 8, price_18k * 8],
# '10g (₹)': [price_22k * 10, price_24k * 10, price_18k * 10]
# }
# df = pd.DataFrame(data)
# # Display the table in a styled format
# st.write(f"### Gold rates in {selected_city}:")
# st.table(df.style.format({'1g (₹)': '₹{:.2f}', '8g (₹)': '₹{:.2f}', '10g (₹)': '₹{:.2f}'}).background_gradient(cmap='YlOrRd'))
# except Exception as e:
# st.error(f"Could not fetch the gold rates. Please try again later.{e}")
import streamlit as st
import requests
import pandas as pd
# Your backend functions
def jina(url):
base_url = "https://r.jina.ai/"
url = base_url + url
response = requests.get(url)
return response.text
def price_cities(url):
text = jina(url)
pos1 = text.find('**')
new = text[:pos1]
twentytwok = new[int(new.find('22K')):int(new.find('24K'))]
value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:]
value_22k = value_22k.split('\n')[0]
twentyfourk = new[int(new.find('24K')):int(new.find('18K'))]
value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:]
value_24k = value_24k.split('\n')[0]
eighteenk = new[int(new.find('18K')):]
value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:]
value_18k = value_18k.split('\n')[0]
return value_24k, value_22k, value_18k
# List of cities
cities = ['Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai',
'Coimbatore', 'Delhi', 'Hyderabad', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow',
'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna',
'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam']
# Sidebar content
st.sidebar.title("About the Project")
# st.sidebar.write("This project, developed by [Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/) "
# "and [Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/), "
# "fetches and displays the latest gold rates for different cities in India. "
# "You can view rates for 1g, 8g, and 10g for 22K, 24K, and 18K gold.")
# st.sidebar.write("Use the dropdown to select a city and view the gold rates accordingly.")
# Sidebar with project and developer info
# st.sidebar.title("Project Info")
st.sidebar.write("This project fetches current gold rates for 24K, 22K, and 18K gold from GoodReturns for 28 Indian states. The rates for 1g, 8g, and 10g are displayed.")
st.sidebar.write("**Developed by:**")
st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)")
st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
# Main UI
st.title('Gold Rates in Indian Cities')
st.subheader('Select a city to view the current gold rates')
# Dropdown for city selection
selected_city = st.selectbox('Select a City', cities)
# Fetch and display gold rates
if selected_city:
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
# Fetch the prices using your backend function
try:
value_24k, value_22k, value_18k = price_cities(city_url)
# Convert string values to float for calculation
value_22k = round(float(value_22k.replace(',', '')),2)
value_24k = round(float(value_24k.replace(',', '')),2)
value_18k = round(float(value_18k.replace(',', '')),2)
# Prepare data for table
data = {
# 'SNO': [1, 2, 3],
'Gold Purity': ['24K', '22K', '18K'],
'1g Price (₹)': [value_24k, value_22k, value_18k],
'8g Price (₹)': [value_24k * 8, value_22k * 8, value_18k * 8],
'10g Price (₹)': [value_24k * 10, value_22k * 10, value_18k * 10]
}
# Create a DataFrame for display
df = pd.DataFrame(data,index=[1, 2, 3])
# Display the DataFrame as a table
st.write(f"Gold rates in {selected_city}:")
# st.dataframe(df.style.set_properties(**{
# 'background-color': 'black',
# 'color': 'white',
# 'border-color': 'ash'
# }))
st.dataframe(df.style.format(precision=2).set_properties(**{
'background-color': 'black',
'color': 'white',
'border-color': 'ash'
}))
except Exception as e:
st.error(f"Could not fetch the gold rates. Please try again.{e}")
# except:
# st.error("Could not fetch the gold rates. Please try again.")
st.markdown("<br><hr><center><p style='color: grey;'>© 2024 All Rights Reserved</p></center><br>", unsafe_allow_html=True)
# # Custom CSS to improve UI appearance
# st.markdown("""
# <style>
# .stSidebar {
# background-color: #f4f4f4;
# }
# .stApp {
# background-color: #000000;
# }
# </style>
# """, unsafe_allow_html=True)