joaomorossini commited on
Commit
80ffcb0
·
1 Parent(s): 5531fc6

Enhance Notion iframe integration with toggle and refresh functionality; use environment variable for URL and add cache busting

Browse files
Files changed (1) hide show
  1. app/demo.py +62 -10
app/demo.py CHANGED
@@ -97,16 +97,25 @@ class NotionAgency(Agency):
97
  except ImportError:
98
  raise Exception("Please install gradio: pip install gradio")
99
 
100
- # Use the specific Notion embed URL provided by the user
101
  notion_embed_url = os.getenv("NOTION_DB_URL")
102
 
103
- # Create iframe with the exact attributes provided by the user
104
- iframe_html = f"""
105
- <iframe src="{notion_embed_url}" width="100%" height="400" frameborder="1" allowfullscreen></iframe>
106
- <div style="text-align: center; margin-top: 5px; font-size: 12px; color: #888;">
107
- If the Notion board doesn't appear, please ensure your Notion page is shared publicly with "Share to web" enabled.
108
- </div>
109
- """
 
 
 
 
 
 
 
 
 
110
 
111
  js = """function () {
112
  gradioURL = window.location.href
@@ -128,12 +137,27 @@ class NotionAgency(Agency):
128
  recipient_agent_names = [agent.name for agent in self.main_recipients]
129
  recipient_agent = self.main_recipients[0]
130
 
 
 
 
131
  with gr.Blocks(js=js) as demo:
132
  chatbot_queue = queue.Queue()
133
 
134
- # Add the iframe at the top, taking up space above the chatbot
 
 
 
135
  with gr.Row():
136
- iframe = gr.HTML(iframe_html)
 
 
 
 
 
 
 
 
 
137
 
138
  # Original components from Agency.demo_gradio
139
  chatbot = gr.Chatbot(height=height)
@@ -149,6 +173,34 @@ class NotionAgency(Agency):
149
  file_upload = gr.Files(label="OpenAI Files", type="filepath")
150
  button = gr.Button(value="Send", variant="primary")
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  def handle_dropdown_change(selected_option):
153
  nonlocal recipient_agent
154
  recipient_agent = self._get_agent_by_name(selected_option)
 
97
  except ImportError:
98
  raise Exception("Please install gradio: pip install gradio")
99
 
100
+ # Use the Notion embed URL from environment variables
101
  notion_embed_url = os.getenv("NOTION_DB_URL")
102
 
103
+ # Function to generate iframe HTML with timestamp for cache busting
104
+ def generate_iframe_html(ts=None):
105
+ # Add timestamp parameter to force refresh
106
+ url = notion_embed_url
107
+ if ts:
108
+ separator = "&" if "?" in url else "?"
109
+ url = f"{url}{separator}ts={ts}"
110
+
111
+ return f"""
112
+ <div id="iframe-container" style="width: 100%;">
113
+ <iframe src="{url}" width="100%" height="400" frameborder="1" allowfullscreen id="notion-iframe"></iframe>
114
+ <div style="text-align: center; margin-top: 5px; font-size: 12px; color: #888;">
115
+ If the Notion board doesn't appear, please ensure your Notion page is shared publicly with "Share to web" enabled.
116
+ </div>
117
+ </div>
118
+ """
119
 
120
  js = """function () {
121
  gradioURL = window.location.href
 
137
  recipient_agent_names = [agent.name for agent in self.main_recipients]
138
  recipient_agent = self.main_recipients[0]
139
 
140
+ # Track iframe visibility state
141
+ iframe_visible = True
142
+
143
  with gr.Blocks(js=js) as demo:
144
  chatbot_queue = queue.Queue()
145
 
146
+ # Create state for iframe visibility
147
+ iframe_state = gr.State(value=True)
148
+
149
+ # Add toggle button and refresh button at the top
150
  with gr.Row():
151
+ toggle_button = gr.Button(
152
+ value="Hide Notion Board", elem_id="toggle-button"
153
+ )
154
+ refresh_button = gr.Button(
155
+ value="Refresh Notion Board", elem_id="refresh-button"
156
+ )
157
+
158
+ # Row for iframe with initial HTML
159
+ with gr.Row() as iframe_row:
160
+ iframe = gr.HTML(value=generate_iframe_html())
161
 
162
  # Original components from Agency.demo_gradio
163
  chatbot = gr.Chatbot(height=height)
 
173
  file_upload = gr.Files(label="OpenAI Files", type="filepath")
174
  button = gr.Button(value="Send", variant="primary")
175
 
176
+ # Function to toggle iframe visibility
177
+ def toggle_iframe(state):
178
+ new_state = not state
179
+ return {
180
+ iframe_row: gr.update(visible=new_state),
181
+ toggle_button: (
182
+ "Show Notion Board" if not new_state else "Hide Notion Board"
183
+ ),
184
+ iframe_state: new_state,
185
+ }
186
+
187
+ # Function to refresh iframe with timestamp
188
+ def refresh_iframe():
189
+ import time
190
+
191
+ # Generate new iframe HTML with current timestamp
192
+ new_html = generate_iframe_html(int(time.time()))
193
+ return gr.update(value=new_html)
194
+
195
+ # Connect buttons to functions
196
+ toggle_button.click(
197
+ toggle_iframe,
198
+ inputs=[iframe_state],
199
+ outputs=[iframe_row, toggle_button, iframe_state],
200
+ )
201
+
202
+ refresh_button.click(refresh_iframe, outputs=[iframe])
203
+
204
  def handle_dropdown_change(selected_option):
205
  nonlocal recipient_agent
206
  recipient_agent = self._get_agent_by_name(selected_option)