Spaces:
Sleeping
Sleeping
File size: 9,813 Bytes
bbbb8d4 |
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 |
import streamlit as st
import plotly.graph_objects as go
from pymongo import MongoClient
from datetime import datetime, timedelta
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import certifi
import json
import os
mongo_uri = "mongodb+srv://Agripredict:TjXSvMhOis49qH8E@cluster0.gek7n.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
if not mongo_uri:
st.error("MongoDB URI is not set!")
st.stop()
else:
# Connect to MongoDB with SSL certificate validation
client = MongoClient(mongo_uri, tlsCAFile=certifi.where())
db = client["AgriPredict"]
collection = db["WhiteSesame"]
# CSS to increase the width of the container
st.markdown("""
<style>
/* Adjust the width of the main container */
.main {
max-width: 1200px; /* Increase the width */
margin: 0 auto; /* Center the container */
}
/* Main background */
body {
background-color: #f9f9f9;
}
/* Title styling */
h1 {
color: #4CAF50;
font-family: 'Arial Black', sans-serif;
}
/* Buttons */
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 14px;
border-radius: 8px;
padding: 10px 20px;
margin: 5px;
white-space: nowrap;
}
.stButton>button:hover {
background-color: #45a049;
}
/* Selectbox styling */
.stSelectbox>div {
padding: 10px;
background-color: #ffffff;
border: 1px solid #e6e6e6;
border-radius: 8px;
}
/* Checkbox styling */
.stCheckbox>label {
font-size: 14px;
color: #555;
}
/* Containers */
.stContainer {
border-radius: 12px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* Chart area */
.plotly-graph-div {
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* Footer */
footer {
font-size: 12px;
text-align: center;
color: #888;
padding: 10px;
}
</style>
""", unsafe_allow_html=True)
st.title("🌾 AgriPredict Dashboard")
# Load the state-market dictionary from the JSON file
with open('all_state_market_dict.json', 'r') as file:
state_market_dict = json.load(file)
# UI for Dashboard
with st.container():
with st.expander("AgriPredict Dashboard", expanded=True):
# Adjust the columns to fit more elements within the container
col1, col2, col3, col4, col5, col6, col7 = st.columns([1.5, 1.5, 1.5, 1.5, 1.5, 3, 3])
# Buttons for periods
with col1:
if st.button('2W', key='2_weeks'):
st.session_state.selected_period = 14
with col2:
if st.button('1M', key='1_month'):
st.session_state.selected_period = 30
with col3:
if st.button('3M', key='3_months'):
st.session_state.selected_period = 90
with col4:
if st.button('1Y', key='1_year'):
st.session_state.selected_period = 365
with col5:
if st.button('5Y', key='5_year'):
st.session_state.selected_period = 1825
# Dropdown for states
with col6:
states = list(state_market_dict.keys())
selected_state = st.selectbox(
"Choose a state",
states,
key="state_selectbox",
index=0
)
# Dropdown for selecting between Price, Volume, or Both
with col7:
data_type = st.selectbox(
"Select Data Type",
["Price", "Volume", "Both"]
)
# Checkbox for market-wise analysis
st.write("")
with st.container():
market_wise = st.checkbox("Market wise", key="market_checkbox")
if market_wise:
# Get markets for the selected state
markets = state_market_dict.get(selected_state, [])
selected_market = st.selectbox(
"Choose a market",
markets,
key="market_selectbox",
index=0
)
query_filter = {"state": selected_state, "Market Name": selected_market}
else:
query_filter = {"state": selected_state}
# Add date filtering based on selected period
if 'selected_period' in st.session_state:
days_period = st.session_state.selected_period
query_filter["Reported Date"] = {
"$gte": datetime.now() - timedelta(days=days_period)
}
# Fetch data from MongoDB
try:
cursor = collection.find(query_filter)
data = list(cursor)
if data:
# Convert MongoDB data to a DataFrame
df = pd.DataFrame(data)
df['Reported Date'] = pd.to_datetime(df['Reported Date'])
# Group by Reported Date
df_grouped = (
df.groupby('Reported Date', as_index=False)
.agg({
'Arrivals (Tonnes)': 'sum',
'Modal Price (Rs./Quintal)': 'mean'
})
)
# Create a complete date range
date_range = pd.date_range(
start=df_grouped['Reported Date'].min(),
end=df_grouped['Reported Date'].max()
)
df_grouped = df_grouped.set_index('Reported Date').reindex(date_range).rename_axis('Reported Date').reset_index()
# Fill missing values
df_grouped['Arrivals (Tonnes)'] = df_grouped['Arrivals (Tonnes)'].fillna(
method='ffill').fillna(method='bfill')
df_grouped['Modal Price (Rs./Quintal)'] = df_grouped['Modal Price (Rs./Quintal)'].fillna(
method='ffill').fillna(method='bfill')
st.subheader(f"📈 Trend Graph for {selected_state} ({'Market: ' + selected_market if market_wise else 'State'})")
if data_type == "Both":
# Min-Max Scaling
scaler = MinMaxScaler()
df_grouped[['Scaled Price', 'Scaled Arrivals']] = scaler.fit_transform(
df_grouped[['Modal Price (Rs./Quintal)', 'Arrivals (Tonnes)']]
)
fig = go.Figure()
# Plot Scaled Price with actual values on hover
fig.add_trace(go.Scatter(
x=df_grouped['Reported Date'],
y=df_grouped['Scaled Price'],
mode='lines',
name='Scaled Price',
line=dict(width=1, color='green'),
text=df_grouped['Modal Price (Rs./Quintal)'], # Actual Modal Price values
hovertemplate='Date: %{x}<br>Scaled Price: %{y:.2f}<br>Actual Price: %{text:.2f}<extra></extra>'
))
# Plot Scaled Arrivals with actual values on hover
fig.add_trace(go.Scatter(
x=df_grouped['Reported Date'],
y=df_grouped['Scaled Arrivals'],
mode='lines',
name='Scaled Arrivals',
line=dict(width=1, color='blue'),
text=df_grouped['Arrivals (Tonnes)'], # Actual Arrivals values
hovertemplate='Date: %{x}<br>Scaled Arrivals: %{y:.2f}<br>Actual Arrivals: %{text:.2f}<extra></extra>'
))
fig.update_layout(
title="Price and Arrivals Trend",
xaxis_title='Date',
yaxis_title='Scaled Values',
template='plotly_white'
)
st.plotly_chart(fig)
elif data_type == "Price":
# Plot Modal Price
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_grouped['Reported Date'],
y=df_grouped['Modal Price (Rs./Quintal)'],
mode='lines',
name='Modal Price',
line=dict(width=1, color='green')
))
fig.update_layout(title="Modal Price Trend", xaxis_title='Date', yaxis_title='Price', template='plotly_white')
st.plotly_chart(fig)
elif data_type == "Volume":
# Plot Arrivals (Tonnes)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df_grouped['Reported Date'],
y=df_grouped['Arrivals (Tonnes)'],
mode='lines',
name='Arrivals',
line=dict(width=1, color='blue')
))
fig.update_layout(title="Arrivals Trend", xaxis_title='Date', yaxis_title='Volume', template='plotly_white')
st.plotly_chart(fig)
else:
st.warning("⚠️ No relevant data found for the selected options.")
else:
st.warning("⚠️ No data found for the selected filters.")
except Exception as e:
st.error(f"❌ Error fetching data: {e}") |