Skip to content

Commit c81ea95

Browse files
committed
conditional example skeleton
1 parent 03b5be8 commit c81ea95

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

examples/conditional.py

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,63 @@
1-
# This feels like it's more contingent on setting up a cron job or automated
2-
# workflow. The logic on whether or not to send (or who to sent to, etc.) is
3-
# outside of the email workflow in my eyes. So this example feel better suited for
4-
# a set of OS directives or other script with somewhere to have a conditional send action.
5-
#
6-
# In the context of Quarto and Connect, if it existed in the output metadata, that seems
7-
# to be only because Connect expects to fire an email, so you have to actively trigger the
8-
# "no email" state.
1+
import os
2+
from dotenv import load_dotenv
3+
import redmail
4+
import random
5+
6+
import sys
7+
8+
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + "/.."))
9+
from data_polars import sp500
10+
11+
12+
load_dotenv()
13+
gmail_address = os.environ["GMAIL_ADDRESS"]
14+
gmail_app_password = os.environ["GMAIL_APP_PASSWORD"]
15+
16+
17+
email_subject = "Report on Cars"
18+
19+
# Randomly select 10 sequential days
20+
num_rows = sp500.shape[0]
21+
if num_rows < 10:
22+
raise ValueError("sp500 must have at least 10 rows")
23+
start_idx = random.randint(0, num_rows - 10)
24+
df_slice = sp500.slice(start_idx, 10).reverse()
25+
26+
# Compare first and last closing prices
27+
first_close = df_slice[0, "close"]
28+
last_close = df_slice[-1, "close"]
29+
bg_color = "#fbeaea" if first_close > last_close else "#e6f4ea" # red if first > last, green otherwise
30+
31+
intro = """
32+
<div style='font-family: sans-serif; font-size: 1.05em; color: #222; margin-bottom: 1em;'>
33+
<h2 style='color: #0074D9; margin-top: 0;'>Welcome to Your S&P 500 Mini Report!</h2>
34+
<p>Hi there,<br><br>
35+
Here's a quick look at 10 sequential days from the S&P 500. The table background color reflects whether the closing price increased (green) or decreased (red) over the period.</p>
36+
</div>
37+
"""
38+
salutation = """
39+
<div style='font-family: sans-serif; font-size: 1em; color: #444; margin-top: 2em;'>
40+
<p>Thanks for reading!<br>Best regards,<br>Jules</p>
41+
</div>
42+
"""
43+
44+
# Render table with background color
45+
table_html = (
46+
df_slice.style
47+
.tab_options(table_background_color=bg_color)
48+
.as_raw_html(inline_css=True)
49+
)
50+
51+
email_body = intro + table_html + salutation
52+
53+
# This is here to emphasize the sender does not have to be the same as the receiver
54+
email_receiver = gmail_address
55+
56+
redmail.gmail.username = gmail_address
57+
redmail.gmail.password = gmail_app_password
58+
59+
redmail.gmail.send(
60+
subject=email_subject,
61+
receivers=["jules.walzergoldfeld@posit.co"],
62+
html=email_body,
63+
)

0 commit comments

Comments
 (0)