1
+ import streamlit as st
2
+ import random
3
+ import datetime
4
+ import time
5
+ import json
6
+ from datetime import datetime , timedelta
7
+
8
+ class AffirmationWidget :
9
+ def __init__ (self ):
10
+ # Dictionary of affirmations by theme
11
+ self .affirmations = {
12
+ "confidence" : [
13
+ "I am capable of achieving anything I set my mind to." ,
14
+ "I trust my abilities and inner wisdom." ,
15
+ "I radiate confidence, self-respect, and inner harmony." ,
16
+ "I am worthy of respect and acceptance." ,
17
+ "My potential to succeed is infinite." ,
18
+ "I choose to be confident and self-assured." ,
19
+ "I am enough, just as I am."
20
+ ],
21
+ "relaxation" : [
22
+ "I choose to feel calm and peaceful." ,
23
+ "I release all tension and embrace tranquility." ,
24
+ "I am surrounded by peaceful energy." ,
25
+ "My mind is calm, my body is relaxed." ,
26
+ "I breathe in peace and exhale stress." ,
27
+ "I deserve to rest and feel at peace." ,
28
+ "Tranquility flows through me with each breath."
29
+ ],
30
+ "productivity" : [
31
+ "I am focused and productive in all I do." ,
32
+ "I complete my tasks efficiently and effectively." ,
33
+ "I use my time wisely and purposefully." ,
34
+ "I am motivated and driven to achieve my goals." ,
35
+ "I take action towards my dreams." ,
36
+ "I am organized and accomplish my priorities." ,
37
+ "My productivity increases each day."
38
+ ]
39
+ }
40
+
41
+ def get_affirmation (self , theme ):
42
+ """Get a random affirmation from the selected theme"""
43
+ return random .choice (self .affirmations [theme ])
44
+
45
+ def display_affirmation_widget ():
46
+ st .markdown ("""
47
+ <style>
48
+ .affirmation-container {
49
+ padding: 20px;
50
+ border-radius: 10px;
51
+ background-color: #f0f8ff;
52
+ margin: 10px 0;
53
+ text-align: center;
54
+ }
55
+ .affirmation-text {
56
+ font-size: 24px;
57
+ color: #2c3e50;
58
+ font-weight: bold;
59
+ margin: 20px 0;
60
+ }
61
+ .theme-selector {
62
+ margin: 20px 0;
63
+ }
64
+ .refresh-interval {
65
+ margin: 20px 0;
66
+ }
67
+ </style>
68
+ """ , unsafe_allow_html = True )
69
+
70
+ st .markdown ("## 🌟 Daily Affirmations" )
71
+
72
+ # Initialize the widget
73
+ if 'affirmation_widget' not in st .session_state :
74
+ st .session_state .affirmation_widget = AffirmationWidget ()
75
+
76
+ # Initialize last update time
77
+ if 'last_update' not in st .session_state :
78
+ st .session_state .last_update = datetime .now ()
79
+
80
+ # Theme selection
81
+ theme = st .selectbox (
82
+ "Choose your affirmation theme:" ,
83
+ ["confidence" , "relaxation" , "productivity" ],
84
+ key = "theme_selector"
85
+ )
86
+
87
+ # Refresh interval selection
88
+ refresh_interval = st .slider (
89
+ "Select refresh interval (minutes):" ,
90
+ min_value = 1 ,
91
+ max_value = 60 ,
92
+ value = 5 ,
93
+ key = "refresh_interval"
94
+ )
95
+
96
+ # Enable notifications
97
+ notifications_enabled = st .checkbox ("Enable push notifications" , value = False )
98
+
99
+ # Get current affirmation
100
+ if 'current_affirmation' not in st .session_state :
101
+ st .session_state .current_affirmation = st .session_state .affirmation_widget .get_affirmation (theme )
102
+
103
+ # Check if it's time to refresh
104
+ current_time = datetime .now ()
105
+ time_difference = current_time - st .session_state .last_update
106
+ if time_difference .total_seconds () >= (refresh_interval * 60 ):
107
+ st .session_state .current_affirmation = st .session_state .affirmation_widget .get_affirmation (theme )
108
+ st .session_state .last_update = current_time
109
+
110
+ # Display affirmation
111
+ st .markdown (
112
+ f"""
113
+ <div class="affirmation-container">
114
+ <div class="affirmation-text">
115
+ "{ st .session_state .current_affirmation } "
116
+ </div>
117
+ </div>
118
+ """ ,
119
+ unsafe_allow_html = True
120
+ )
121
+
122
+ # Manual refresh button
123
+ if st .button ("↻ New Affirmation" ):
124
+ st .session_state .current_affirmation = st .session_state .affirmation_widget .get_affirmation (theme )
125
+ st .session_state .last_update = current_time
126
+
127
+ # Display last update time
128
+ st .markdown (
129
+ f"<div style='text-align: center; color: #666;'>Last updated: { st .session_state .last_update .strftime ('%I:%M %p' )} </div>" ,
130
+ unsafe_allow_html = True
131
+ )
132
+
133
+ # Save preferences
134
+ if st .button ("Save Preferences" ):
135
+ preferences = {
136
+ "theme" : theme ,
137
+ "refresh_interval" : refresh_interval ,
138
+ "notifications_enabled" : notifications_enabled
139
+ }
140
+ st .success ("Preferences saved successfully!" )
141
+
142
+ # To use this widget in your Streamlit app:
143
+ if __name__ == "__main__" :
144
+ display_affirmation_widget ()
0 commit comments