-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_tb.py
More file actions
112 lines (74 loc) · 3.58 KB
/
create_tb.py
File metadata and controls
112 lines (74 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pandas as pd
from novem import Plot
from novem.colors import StaticColor as SC, DynamicColor as DC
from novem.table import Selector as S
# grab our example numbers from novem data
df = pd.read_csv('https://data.novem.io/v1/examples/plot/nei_rgn_tb.csv', index_col=0)
# let's convert our Contribution to bps, Basis points)
df['Contribution'] *= 10000
# Create a novem e-mail table `mtable`
tb = Plot('nei_rgn_tb', type='table')
# add our data
df.pipe(tb)
# right align our nmbers
tb.cell.align = S(df.loc[:, "NAV":], ">", df)
# add some spacing to the left and right of numbers
tb.cell.padding = S(df.loc[:, :], "l 2", df)
tb.cell.padding += S(df.loc[:, :], "r 1", df)
# add some padding below the header
tb.cell.padding += S(df.iloc[0, :], "t 2", df, c=":")
# add some padding between the top and bottom performers
tb.cell.padding += S(df.iloc[9, :], "b 4", df, c=":")
# let's format our numbers, first set NAV and all following
# numbers as decimal, then override Return as percent
tb.cell.format = S(df.loc[:, "NAV":], ",.1f", df)
tb.cell.format += S(df.loc[:, "Return"], ",.1%", df)
# use index colors
tb.colors.type = 'ix'
# let's highlight rows with an individual stock return above 10%
tb.colors = S(df.loc[df["Return"] > 0.1, :], SC("bg", "green-100"), df, c=":")
# let's highlight rows with an individual stock return below -10%
tb.colors += S(df.loc[df["Return"] < -0.1, :], SC("bg", "red-100"), df, c=":")
# Let's create a foreground, green, heatmap for our top 10 performers,
# we crate an individual heatmap for Returns and Contributions
tb.colors += S(df.iloc[:10, -1], DC("fg", min="green-300", max="green-600"), df)
tb.colors += S(df.iloc[:10, -2], DC("fg", min="green-300", max="green-600"), df)
# repeat the above exercise but for our detractors
tb.colors += S(df.iloc[10:, -1], DC("fg", min="red-600", max="red-300"), df)
tb.colors += S(df.iloc[10:, -2], DC("fg", min="red-600", max="red-300"), df)
# finaly let's bold the text and add a bottom border to the top row, we'll add
# this manually as pandas slicing doens't make it easy to slice for the index
# row
tb.cell.border = '0 : b 1 inverse' # row 0, all columns, bottom 1 wide border
# with the "inverse" color
tb.cell.text = '0 : b' # row 0, all columns, bold text
# add a caption to the table
tb.caption = """The above table shows the top 10 and bottom 10 contributors
to the Novem Example Index performance on the 10th of November 2025.
The return numbers are the individual stocks DTD return whereas the contribution
number is its individual contribution to the overall index expressen in basis points,
NAV is in million USD."""
# Let's add a discriptive name
tb.name = "Novem Example Index - Top & Bottom Contributors\n"
# and a short summary
tb.summary = "Example table of top 10 and bottom 10 contributors to the novem example index\n"
# share it with the world
# tb.shared += 'public'
# share it with ai
# tb.shared += 'chat'
# let's print our url
print(tb.url)
tb.type = 'mtable'
with open(__file__,'r') as f:
ctnt = f.read().replace('````','```') # turtles
desc = f"""
# Novem Example Index top and bottom performers
This table is an example of how you can highlight top and bottom performers
for a fictive equity index or portfolio. The code with comments is available below.
Curious about the data side? [Check out how we created the sample data.](https://novem.io/blog/creating-the-novem-example-index/)
Want to learn more about novem and pandas? [We have an article for that.](https://novem.io/blog/novem-pandas/)
````
{ctnt}
````
"""
tb.description = desc