Fixed an error
Browse files
app.py
CHANGED
|
@@ -1,227 +1,88 @@
|
|
| 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 |
-
# st.sidebar.title("Project Info")
|
| 90 |
-
# 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.")
|
| 91 |
-
# st.sidebar.write("**Developed by:**")
|
| 92 |
-
# st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)")
|
| 93 |
-
# st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
|
| 94 |
-
|
| 95 |
-
# # Dropdown for selecting a city
|
| 96 |
-
# st.title('Gold Rates in Indian States')
|
| 97 |
-
# selected_city = st.selectbox('Select a City', cities)
|
| 98 |
-
|
| 99 |
-
# Fetch and display gold rates when a city is selected
|
| 100 |
-
# if selected_city:
|
| 101 |
-
# city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
|
| 102 |
-
|
| 103 |
-
# try:
|
| 104 |
-
# # Fetch the gold prices using your backend function
|
| 105 |
-
# price_22k, price_24k, price_18k = price_cities(city_url)
|
| 106 |
-
|
| 107 |
-
# # Prepare data for table
|
| 108 |
-
# data = {
|
| 109 |
-
# 'Karat': ['22K', '24K', '18K'],
|
| 110 |
-
# '1g (₹)': [price_22k, price_24k, price_18k],
|
| 111 |
-
# '8g (₹)': [price_22k * 8, price_24k * 8, price_18k * 8],
|
| 112 |
-
# '10g (₹)': [price_22k * 10, price_24k * 10, price_18k * 10]
|
| 113 |
-
# }
|
| 114 |
-
|
| 115 |
-
# df = pd.DataFrame(data)
|
| 116 |
-
|
| 117 |
-
# # Display the table in a styled format
|
| 118 |
-
# st.write(f"### Gold rates in {selected_city}:")
|
| 119 |
-
# st.table(df.style.format({'1g (₹)': '₹{:.2f}', '8g (₹)': '₹{:.2f}', '10g (₹)': '₹{:.2f}'}).background_gradient(cmap='YlOrRd'))
|
| 120 |
-
|
| 121 |
-
# except Exception as e:
|
| 122 |
-
# st.error(f"Could not fetch the gold rates. Please try again later.{e}")
|
| 123 |
-
import streamlit as st
|
| 124 |
-
import requests
|
| 125 |
-
import pandas as pd
|
| 126 |
-
|
| 127 |
-
# Your backend functions
|
| 128 |
-
def jina(url):
|
| 129 |
-
base_url = "https://r.jina.ai/"
|
| 130 |
-
url = base_url + url
|
| 131 |
-
response = requests.get(url)
|
| 132 |
-
return response.text
|
| 133 |
-
|
| 134 |
-
def price_cities(url):
|
| 135 |
-
text = jina(url)
|
| 136 |
-
pos1 = text.find('**')
|
| 137 |
-
new = text[:pos1]
|
| 138 |
-
|
| 139 |
-
twentytwok = new[int(new.find('22K')):int(new.find('24K'))]
|
| 140 |
-
value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:]
|
| 141 |
-
|
| 142 |
-
twentyfourk = new[int(new.find('24K')):int(new.find('18K'))]
|
| 143 |
-
value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:]
|
| 144 |
-
|
| 145 |
-
eighteenk = new[int(new.find('18K')):]
|
| 146 |
-
value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:]
|
| 147 |
-
|
| 148 |
-
return value_24k, value_22k, value_18k
|
| 149 |
-
|
| 150 |
-
# List of cities
|
| 151 |
-
cities = ['Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai',
|
| 152 |
-
'Coimbatore', 'Delhi', 'Hyderabad', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow',
|
| 153 |
-
'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna',
|
| 154 |
-
'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam']
|
| 155 |
-
|
| 156 |
-
# Sidebar content
|
| 157 |
-
st.sidebar.title("About the Project")
|
| 158 |
-
# st.sidebar.write("This project, developed by [Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/) "
|
| 159 |
-
# "and [Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/), "
|
| 160 |
-
# "fetches and displays the latest gold rates for different cities in India. "
|
| 161 |
-
# "You can view rates for 1g, 8g, and 10g for 22K, 24K, and 18K gold.")
|
| 162 |
-
|
| 163 |
-
# st.sidebar.write("Use the dropdown to select a city and view the gold rates accordingly.")
|
| 164 |
-
# Sidebar with project and developer info
|
| 165 |
-
# st.sidebar.title("Project Info")
|
| 166 |
-
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.")
|
| 167 |
-
st.sidebar.write("**Developed by:**")
|
| 168 |
-
st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)")
|
| 169 |
-
st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
|
| 170 |
-
|
| 171 |
-
# Main UI
|
| 172 |
-
st.title('Gold Rates in Indian Cities')
|
| 173 |
-
st.subheader('Select a city to view the current gold rates')
|
| 174 |
-
|
| 175 |
-
# Dropdown for city selection
|
| 176 |
-
selected_city = st.selectbox('Select a City', cities)
|
| 177 |
-
|
| 178 |
-
# Fetch and display gold rates
|
| 179 |
-
if selected_city:
|
| 180 |
-
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
|
| 181 |
-
|
| 182 |
-
# Fetch the prices using your backend function
|
| 183 |
-
try:
|
| 184 |
-
value_24k, value_22k, value_18k = price_cities(city_url)
|
| 185 |
-
|
| 186 |
-
# Convert string values to float for calculation
|
| 187 |
-
value_22k = float(value_22k.replace(',', ''))
|
| 188 |
-
value_24k = float(value_24k.replace(',', ''))
|
| 189 |
-
value_18k = float(value_18k.replace(',', ''))
|
| 190 |
-
|
| 191 |
-
# Prepare data for table
|
| 192 |
-
data = {
|
| 193 |
-
# 'SNO': [1, 2, 3],
|
| 194 |
-
'Gold Purity': ['24K', '22K', '18K'],
|
| 195 |
-
'1g Price (₹)': [value_24k, value_22k, value_18k],
|
| 196 |
-
'8g Price (₹)': [value_24k * 8, value_22k * 8, value_18k * 8],
|
| 197 |
-
'10g Price (₹)': [value_24k * 10, value_22k * 10, value_18k * 10]
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
# Create a DataFrame for display
|
| 201 |
-
df = pd.DataFrame(data,index=[1, 2, 3])
|
| 202 |
-
|
| 203 |
-
# Display the DataFrame as a table
|
| 204 |
-
st.write(f"Gold rates in {selected_city}:")
|
| 205 |
-
st.dataframe(df.style.set_properties(**{
|
| 206 |
-
'background-color': 'black',
|
| 207 |
-
'color': 'white',
|
| 208 |
-
'border-color': 'ash'
|
| 209 |
-
}))
|
| 210 |
-
|
| 211 |
-
except:
|
| 212 |
-
st.error("Could not fetch the gold rates. Please try again.")
|
| 213 |
-
st.markdown("<br><hr><center><p style='color: grey;'>© 2024 All Rights Reserved</p></center><br>", unsafe_allow_html=True)
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
# # Custom CSS to improve UI appearance
|
| 217 |
-
# st.markdown("""
|
| 218 |
-
# <style>
|
| 219 |
-
# .stSidebar {
|
| 220 |
-
# background-color: #f4f4f4;
|
| 221 |
-
# }
|
| 222 |
-
# .stApp {
|
| 223 |
-
# background-color: #000000;
|
| 224 |
-
# }
|
| 225 |
-
# </style>
|
| 226 |
-
# """, unsafe_allow_html=True)
|
| 227 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Your backend functions
|
| 6 |
+
def jina(url):
|
| 7 |
+
base_url = "https://r.jina.ai/"
|
| 8 |
+
url = base_url + url
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
return response.text
|
| 11 |
+
|
| 12 |
+
def price_cities(url):
|
| 13 |
+
text = jina(url)
|
| 14 |
+
pos1 = text.find('**')
|
| 15 |
+
new = text[:pos1]
|
| 16 |
+
|
| 17 |
+
twentytwok = new[int(new.find('22K')):int(new.find('24K'))]
|
| 18 |
+
value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:]
|
| 19 |
+
value_22k = value_22k.split('\n')[0]
|
| 20 |
+
|
| 21 |
+
twentyfourk = new[int(new.find('24K')):int(new.find('18K'))]
|
| 22 |
+
value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:]
|
| 23 |
+
value_24k = value_24k.split('\n')[0]
|
| 24 |
+
|
| 25 |
+
eighteenk = new[int(new.find('18K')):]
|
| 26 |
+
value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:]
|
| 27 |
+
value_18k = value_18k.split('\n')[0]
|
| 28 |
+
|
| 29 |
+
return value_24k, value_22k, value_18k
|
| 30 |
+
# List of cities
|
| 31 |
+
cities = ['Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai',
|
| 32 |
+
'Coimbatore', 'Delhi', 'Hyderabad', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow',
|
| 33 |
+
'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna',
|
| 34 |
+
'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam']
|
| 35 |
+
|
| 36 |
+
# Sidebar content
|
| 37 |
+
st.sidebar.title("About the Project")
|
| 38 |
+
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.")
|
| 39 |
+
st.sidebar.write("**Developed by:**")
|
| 40 |
+
st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)")
|
| 41 |
+
st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)")
|
| 42 |
+
|
| 43 |
+
# Main UI
|
| 44 |
+
st.title('Gold Rates in Indian Cities')
|
| 45 |
+
st.subheader('Select a city to view the current gold rates')
|
| 46 |
+
|
| 47 |
+
# Dropdown for city selection
|
| 48 |
+
selected_city = st.selectbox('Select a City', cities)
|
| 49 |
+
|
| 50 |
+
# Fetch and display gold rates
|
| 51 |
+
if selected_city:
|
| 52 |
+
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
|
| 53 |
+
|
| 54 |
+
# Fetch the prices using your backend function
|
| 55 |
+
try:
|
| 56 |
+
value_24k, value_22k, value_18k = price_cities(city_url)
|
| 57 |
+
|
| 58 |
+
# Convert string values to float for calculation
|
| 59 |
+
value_22k = round(float(value_22k.replace(',', '')),2)
|
| 60 |
+
value_24k = round(float(value_24k.replace(',', '')),2)
|
| 61 |
+
value_18k = round(float(value_18k.replace(',', '')),2)
|
| 62 |
+
# Prepare data for table
|
| 63 |
+
data = {
|
| 64 |
+
# 'SNO': [1, 2, 3],
|
| 65 |
+
'Gold Purity': ['24K', '22K', '18K'],
|
| 66 |
+
'1g Price (₹)': [value_24k, value_22k, value_18k],
|
| 67 |
+
'8g Price (₹)': [value_24k * 8, value_22k * 8, value_18k * 8],
|
| 68 |
+
'10g Price (₹)': [value_24k * 10, value_22k * 10, value_18k * 10]
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Create a DataFrame for display
|
| 72 |
+
df = pd.DataFrame(data,index=[1, 2, 3])
|
| 73 |
+
|
| 74 |
+
# Display the DataFrame as a table
|
| 75 |
+
st.write(f"Gold rates in {selected_city}:")
|
| 76 |
+
st.dataframe(df.style.format(precision=2).set_properties(**{
|
| 77 |
+
'background-color': 'black',
|
| 78 |
+
'color': 'white',
|
| 79 |
+
'border-color': 'ash'
|
| 80 |
+
}))
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"Could not fetch the gold rates. Please try again.{e}")
|
| 85 |
+
|
| 86 |
+
st.markdown("<br><hr><center><p style='color: grey;'>© 2024 All Rights Reserved</p></center><br>", unsafe_allow_html=True)
|
| 87 |
+
|
| 88 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|