prakharg24 commited on
Commit
93a1da5
·
verified ·
1 Parent(s): 0733035

Update my_pages/information_loss.py

Browse files
Files changed (1) hide show
  1. my_pages/information_loss.py +47 -22
my_pages/information_loss.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
  from utils import go_to
3
  import random
 
4
 
5
- # Sample list of possible features
6
  ALL_FEATURES = [
7
  "Credit Score", "Annual Income", "Loan Amount Requested",
8
  "Number of Previous Loans", "Debt-to-Income Ratio",
@@ -13,53 +13,78 @@ ALL_FEATURES = [
13
  def render():
14
  st.title("Information Loss Demo")
15
 
16
- # Session state setup
17
  if "available_features" not in st.session_state:
18
  st.session_state.available_features = ALL_FEATURES.copy()
19
  if "selected_features" not in st.session_state:
20
  st.session_state.selected_features = []
 
 
 
 
 
 
21
 
22
- # --- TOP HALF ---
23
- col1, col2 = st.columns([1, 1])
24
 
25
- # Top left: loan applicant icon/image
26
  with col1:
27
  st.image(
28
- "https://cdn-icons-png.flaticon.com/512/1048/1048949.png",
29
- caption="Loan Applicant",
30
  width=200
31
  )
32
 
33
- # Top right: clickable "feature cloud"
34
  with col2:
35
- st.markdown("### Available Features (click to add approximation)")
36
- random.shuffle(st.session_state.available_features) # scatter order each run
 
 
37
 
38
  for feature in st.session_state.available_features:
39
- if st.button(feature, key=feature):
40
- # Show popup / message
41
- with st.spinner(f"Adding approximation for '{feature}'..."):
42
- st.session_state.selected_features.append(feature + " (approx)")
43
- st.session_state.available_features.remove(feature)
44
- st.session_state.show_message = f"Sorry, '{feature}' cannot be precisely collected. Adding approximation instead."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Show popup message in center if exists
47
  if "show_message" in st.session_state:
48
  st.markdown(
49
- f"<div style='text-align:center; color:red; font-weight:bold;'>{st.session_state.show_message}</div>",
50
  unsafe_allow_html=True
51
  )
52
- del st.session_state.show_message # remove after showing
53
 
54
  st.markdown("---")
55
 
56
  # --- BOTTOM HALF: Dataset table ---
57
  st.markdown("### Current Dataset")
58
  if st.session_state.selected_features:
59
- # Create table with placeholder values
60
- import pandas as pd
61
  cols = st.session_state.selected_features + ["Target Label"]
62
  df = pd.DataFrame(columns=cols)
63
  st.dataframe(df, use_container_width=True)
64
  else:
65
- st.info("No features added yet. Click a feature above to add it to the dataset.")
 
1
  import streamlit as st
2
  from utils import go_to
3
  import random
4
+ import pandas as pd
5
 
 
6
  ALL_FEATURES = [
7
  "Credit Score", "Annual Income", "Loan Amount Requested",
8
  "Number of Previous Loans", "Debt-to-Income Ratio",
 
13
  def render():
14
  st.title("Information Loss Demo")
15
 
16
+ # --- SESSION STATE ---
17
  if "available_features" not in st.session_state:
18
  st.session_state.available_features = ALL_FEATURES.copy()
19
  if "selected_features" not in st.session_state:
20
  st.session_state.selected_features = []
21
+ if "cloud_positions" not in st.session_state:
22
+ # Pre-generate random positions for each feature
23
+ st.session_state.cloud_positions = {
24
+ feature: (random.randint(0, 80), random.randint(0, 80))
25
+ for feature in ALL_FEATURES
26
+ }
27
 
28
+ # --- LAYOUT ---
29
+ col1, col2 = st.columns([1, 2])
30
 
31
+ # TOP LEFT: Loan applicant icon
32
  with col1:
33
  st.image(
34
+ "https://cdn-icons-png.flaticon.com/512/1048/1048949.png",
35
+ caption="Loan Applicant",
36
  width=200
37
  )
38
 
39
+ # TOP RIGHT: Feature cloud
40
  with col2:
41
+ st.markdown("### Available Features")
42
+
43
+ # Create a fixed-height area for the cloud
44
+ cloud_html = "<div style='position: relative; height: 300px; border: 1px dashed #ccc;'>"
45
 
46
  for feature in st.session_state.available_features:
47
+ top, left = st.session_state.cloud_positions[feature]
48
+ # Button styled as text link in random position
49
+ button_html = f"""
50
+ <form action="" method="get" style="position: absolute; top: {top}%; left: {left}%; transform: translate(-50%, -50%);">
51
+ <button name="feature_click" value="{feature}" style="background:none; border:none; color:blue; text-decoration:underline; cursor:pointer;">
52
+ {feature}
53
+ </button>
54
+ </form>
55
+ """
56
+ cloud_html += button_html
57
+
58
+ cloud_html += "</div>"
59
+ st.markdown(cloud_html, unsafe_allow_html=True)
60
+
61
+ # --- CLICK HANDLING ---
62
+ clicked_feature = st.query_params.get("feature_click")
63
+ if clicked_feature and clicked_feature in st.session_state.available_features:
64
+ st.session_state.selected_features.append(clicked_feature + " (approx)")
65
+ st.session_state.available_features.remove(clicked_feature)
66
+ st.session_state.show_message = (
67
+ f"Sorry, '{clicked_feature}' cannot be precisely collected. Adding approximation instead."
68
+ )
69
+ # Clear the query param to avoid re-adding on refresh
70
+ st.query_params.clear()
71
+ st.experimental_rerun()
72
 
73
+ # --- MESSAGE ---
74
  if "show_message" in st.session_state:
75
  st.markdown(
76
+ f"<div style='text-align:center; color:red; font-weight:bold; margin:20px 0;'>{st.session_state.show_message}</div>",
77
  unsafe_allow_html=True
78
  )
79
+ del st.session_state.show_message
80
 
81
  st.markdown("---")
82
 
83
  # --- BOTTOM HALF: Dataset table ---
84
  st.markdown("### Current Dataset")
85
  if st.session_state.selected_features:
 
 
86
  cols = st.session_state.selected_features + ["Target Label"]
87
  df = pd.DataFrame(columns=cols)
88
  st.dataframe(df, use_container_width=True)
89
  else:
90
+ st.info("No features added yet. Click a feature above to add it to the dataset.")