Skip to content

Commit 7ca3a77

Browse files
committed
Separate data creation into reusable code chunk
1 parent def6189 commit 7ca3a77

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

extensions/app-canary/app-canary.qmd

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if show_instructions:
5555
results = []
5656
df = pd.DataFrame() # Empty DataFrame
5757
check_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
58+
gt_tbl = None # Define gt_tbl as None to avoid errors
5859
else:
5960
# Continue with normal execution
6061
# Headers for Connect API
@@ -147,30 +148,11 @@ else:
147148
check_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
148149
```
149150

150-
::: {.card .table-card .content-visible when-format="dashboard"}
151151
```{python}
152152
#| echo: false
153153
154-
# Display instructions if setup failed
155-
if show_instructions:
156-
from IPython.display import display, HTML
157-
html_content = """
158-
<h2>Setup Instructions</h2>
159-
<ul>
160-
"""
161-
for instruction in instructions:
162-
html_content += f"<li>{instruction}</li>"
163-
164-
html_content += """
165-
</ul>
166-
<p>Posit Connect documentation for Vars (environment variables): <a href="https://docs.posit.co/connect/user/content-settings/#content-vars" target="_blank">https://docs.posit.co/connect/user/content-settings/#content-vars</a></p>
167-
"""
168-
display(HTML(html_content))
169-
170-
171-
# Display table with basic styling
154+
# Create a shared table that will be used by both dashboard and email formats
172155
if not show_instructions and not df.empty:
173-
174156
# First create links for name and guid columns
175157
df_display = df.copy()
176158
@@ -199,24 +181,51 @@ if not show_instructions and not df.empty:
199181
locations=loc.body(columns="status", rows=lambda df: df["status"] == "FAIL")
200182
))
201183
202-
# Display the table
203-
gt_tbl
204-
```
205-
:::
184+
# Create HTML version with inline CSS for email
185+
gt_html = gt_tbl.as_raw_html(inline_css=True)
186+
else:
187+
gt_tbl = None
188+
gt_html = "<p>No applications are currently being monitored.</p>"
206189
190+
# Compute if we should send an email, only send if at least one app has a failure
191+
send_email = not df.empty and (df['status'] == 'FAIL').any() if 'df' in locals() and 'status' in df.columns else False
192+
```
207193

194+
::: {.card .table-card .content-visible when-format="dashboard"}
208195
```{python}
209196
#| echo: false
210197
211-
# Computer if we should send an email, only send if at least one app has a failure
212-
send_email = not df.empty and (df['status'] == 'FAIL').any()
198+
# Display instructions if setup failed
199+
if show_instructions:
200+
from IPython.display import display, HTML
201+
html_content = """
202+
<h2>Setup Instructions</h2>
203+
<ul>
204+
"""
205+
for instruction in instructions:
206+
html_content += f"<li>{instruction}</li>"
207+
208+
html_content += """
209+
</ul>
210+
<p>Posit Connect documentation for Vars (environment variables): <a href="https://docs.posit.co/connect/user/content-settings/#content-vars" target="_blank">https://docs.posit.co/connect/user/content-settings/#content-vars</a></p>
211+
"""
212+
display(HTML(html_content))
213+
214+
# Display the table (now using the pre-created gt_tbl from above)
215+
elif gt_tbl is not None:
216+
gt_tbl
217+
else:
218+
display(HTML("<p>No applications are currently being monitored.</p>"))
213219
```
220+
:::
214221

215222

216223
::: {.email .content-visible when-format="email"}
217224

218225
::: {.email-scheduled}
219-
`{python} send_email`
226+
```{python}
227+
send_email
228+
```
220229
:::
221230

222231
::: {.subject}
@@ -234,8 +243,7 @@ One or more of the apps you are monitoring have failed.
234243
```{python}
235244
#| echo: false
236245
237-
HTML(gt_tbl.as_raw_html(inline_css=True))
246+
# Use the pre-generated HTML with inline CSS
247+
HTML(gt_html)
238248
```
239-
240-
241249
:::

0 commit comments

Comments
 (0)