<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Tech&#39;n&#39;Take</title>
<link>https://tech.ycwu.space/</link>
<atom:link href="https://tech.ycwu.space/index.xml" rel="self" type="application/rss+xml"/>
<description>This is Jerry Wu&#39;s Tech&#39;n&#39;take.</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Sun, 10 May 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Rust Notes #3: Iterator collect and Result short-circuiting</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-3/20260510.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-3/rust_notes_3.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 3"></p>
</figure>
</div>
<p>Today, while reading the Rust documentation for the <code>Iterator</code> trait, I came across an interesting note in the <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect"><code>collect</code></a> method section.</p>
<p>It explains that <code>collect()</code> is more flexible than it might first appear:</p>
<blockquote class="blockquote">
<p><code>collect()</code> can also create instances of types that are not typical collections. For example, a <code>String</code> can be built from <code>char</code>s, and an iterator of <code>Result&lt;T, E&gt;</code> items can be collected into <code>Result&lt;Collection&lt;T&gt;, E&gt;</code>.</p>
</blockquote>
<p>This behavior is particularly useful when you want either:</p>
<ul>
<li>to short-circuit immediately when the first error occurs, or</li>
<li>all operations to succeed and collect the results.</li>
</ul>
<p>For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"not 42"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span> _<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{r1:?}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Err(ParseIntError { kind: InvalidDigit })</span></span>
<span id="cb1-5"></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"42"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"43"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span> _<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{r2:?}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Ok([42, 43])</span></span>
<span id="cb1-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>In <code>r1</code>, the string <code>"not 42"</code> fails to parse as an <code>i32</code>, so the entire computation short-circuits and returns the first error (<code>ParseIntError</code>). The subsequent value <code>"43"</code> is never processed.</p>
<p>In contrast, <code>r2</code> contains only valid numeric strings, so all elements are successfully parsed and collected into a <code>Vec&lt;i32&gt;</code> wrapped in <code>Ok</code>.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-3/20260510.html</guid>
  <pubDate>Sun, 10 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-3/rust_notes_3.png" medium="image" type="image/png" height="33" width="144"/>
</item>
<item>
  <title>Rust Notes #2: Trait-Based Conversions in Iterators</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-2/20260509.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-2/rust_notes_2.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 2"></p>
</figure>
</div>
<p>Today I read some code in <a href="https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html">Rust by Example</a> and came across an interesting usage of iterator combinators.</p>
<p>Typically, I would pass a closure to <code>Iterator::map()</code>, but a more elegant approach is to use a trait function pointer directly. For example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"42"</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Alternative <code>r1</code> implementation using <code>String::from()</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>An alternative implementation for <code>r1</code> is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&amp;</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(s))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>collect()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>Using <code>.to_string()</code> or <code>String::from()</code> here is mostly a matter of coding style.</p>
<p>The item type produced by <code>v.iter()</code> is <code>&amp;&amp;str</code>. In the first version, we rely on Rust’s auto-deref feature with <code>|s| s.to_string()</code>. In the second version, we manually destructure the extra reference layer using <code>|&amp;s|</code>, converting the item from <code>&amp;&amp;str</code> to <code>&amp;str</code>, which can then be passed directly to <code>String::from()</code>.</p>
</div>
</div>
</div>
<p>For <code>r1</code>, we provide a closure that is applied to each item of the iterator. For <code>r2</code>, however, we directly pass the <code>String::from</code> function.</p>
<p>This technique becomes especially useful when type constraints are already clear from context. Instead of explicitly referring to a concrete constructor like <code>String::from</code>, Rust allows us to rely on more general trait-based conversions. For example, the book demonstrates that <code>.map_err(From::from)</code> leverages the <code>From::from</code> function as a generic conversion mechanism, letting the compiler infer the appropriate target error type from the surrounding <code>Result</code> context. This makes the code more flexible and reduces the need to commit to a specific concrete type at the point of transformation.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-2/20260509.html</guid>
  <pubDate>Sat, 09 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-2/rust_notes_2.png" medium="image" type="image/png" height="23" width="144"/>
</item>
<item>
  <title>Rust Notes #1: Loop Scope vs Python</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/rust-notes-1/20260311.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/rust-notes-1/rust_notes_1.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Rust Notes 1"></p>
</figure>
</div>
<p>For the last couple of months, I’ve been busy learning Rust and suddenly realized that I haven’t written a blog post in a while. So I’d like to share an interesting pattern I encountered while learning Rust from the perspective of a Python developer.</p>
<p>In the following Rust code, I initially expected to see <code>idx=2</code> and <code>n=3</code>, but the compiler complained.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">vec!</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> n) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>iter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-4">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{idx}, {n}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-6">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{idx}, {n}"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// panic!</span></span>
<span id="cb1-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>What I had in mind was that the Rust code would translate conceptually into something like the following Python code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb2-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> idx, n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(v):</span>
<span id="cb2-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>idx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>idx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2, 3</span></span></code></pre></div></div>
<p>Frankly speaking, I was puzzled for a while before realizing that <code>(idx, n)</code> in the Rust <code>for</code> loop is actually a pattern. The variables <code>idx</code> and <code>n</code> exist only within the scope of the loop body, so they are not available outside the <code>for</code> block.</p>
<p>Learning Rust keeps things refreshing and interesting every time I come across a pattern that differs from Python. I hope to continue sharing short Rust notes like this in the future.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>rust</category>
  <category>python</category>
  <guid>https://tech.ycwu.space/posts/rust-notes-1/20260311.html</guid>
  <pubDate>Wed, 11 Mar 2026 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/rust-notes-1/rust_notes_1.png" medium="image" type="image/png" height="55" width="144"/>
</item>
<item>
  <title>Notes on Turtle Island</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/turtle-island-notes/20250721.html</link>
  <description><![CDATA[ 






<div class="quarto-figure quarto-figure-left">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/turtle-island-notes/turtle_island_docs.png" class="img-fluid quarto-figure quarto-figure-left figure-img" alt="Turtle Island docs"></p>
</figure>
</div>
<p>This is a short post documenting some things I often forget while building <a href="https://github.com/jrycw/turtle-island">Turtle Island</a>.</p>
<section id="output-column-names" class="level2">
<h2 class="anchored" data-anchor-id="output-column-names">Output Column Names</h2>
<p>In Polars, if an explicit name isn’t provided, the output column will take the name of the first expression. This default behavior is generally helpful, but it can lead to unexpected results—especially when the first expression doesn’t directly represent a column. For example, if the first expression is <code>pl.lit()</code>, the resulting column name will be <code>"literal"</code>.</p>
<p>This isn’t a big issue when dealing with a single column, but it becomes problematic when using expressions like <code>pl.all()</code> or <code>pl.col("*")</code>. In such cases, logic that depends on column names often breaks.</p>
<p>My current workaround is to tweak the internal logic so that the returned expression doesn’t start with something like <code>pl.lit()</code>, especially in functions that aim to support wildcard expressions.</p>
</section>
<section id="expression-context-vs-dataframe-context" class="level2">
<h2 class="anchored" data-anchor-id="expression-context-vs-dataframe-context">Expression Context vs DataFrame Context</h2>
<p>Since Polars expressions are designed to be evaluated later (not immediately), we cannot rely on any runtime properties of the DataFrame when building them. That means things like the shape or number of rows in the DataFrame are not available during expression construction.</p>
<p>However, some built-in Polars functions help bridge this gap. For instance, <code>pl.len()</code> returns an expression that can be used to represent the number of rows in the future evaluation context. This is useful for writing general-purpose logic in <strong>Turtle Island</strong>.</p>
<p>A major caveat is that Polars expressions can’t be evaluated directly in Python. You can’t write something like <code>if pl.len()</code> or <code>pl.len() + 1</code> as regular Python code. These expressions must be used inside other Polars expressions or functions—such as <code>pl.int_range(0, pl.len() + 1)</code>—where they’ll be correctly interpreted and evaluated within the Polars execution context.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>ti</category>
  <guid>https://tech.ycwu.space/posts/turtle-island-notes/20250721.html</guid>
  <pubDate>Mon, 21 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/turtle-island-notes/turtle_island_docs.png" medium="image" type="image/png" height="56" width="144"/>
</item>
<item>
  <title>Turtle Island: A Utility Kit for Polars Expressions</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/turtle-island-intro/20250706.html</link>
  <description><![CDATA[ 






<p>This weekend, I started building a Python package called <a href="https://github.com/jrycw/turtle-island">Turtle Island</a>, which collects some of my ideas for helper functions aimed at reducing boilerplate when writing Polars expressions.</p>
<p>Currently, Turtle Island offers the following nine utilities:</p>
<ul>
<li><strong><code>case_when()</code></strong> – A cleaner and more readable way to construct chained conditional logic in Polars.</li>
<li><strong><code>make_index()</code></strong> – Creates a virtual row index as a Polars expression, without materializing it as a column.</li>
<li><strong><code>bucketize_lit()</code></strong> – Assigns literal values to rows in a round-robin pattern based on their index.</li>
<li><strong><code>bucketize()</code></strong> – A more general version of <code>bucketize_lit()</code> that cycles through multiple Polars expressions, enabling advanced use cases with dynamic column values.</li>
<li><strong><code>is_every_nth_row()</code></strong> – Identifies every <em>n</em>-th row using a modulo operation on the row index.</li>
<li><strong><code>move_cols_to_start()</code></strong> – Reorders selected columns to appear at the beginning of the DataFrame.</li>
<li><strong><code>move_cols_to_end()</code></strong> – Reorders selected columns to appear at the end of the DataFrame.</li>
<li><strong><code>make_hyperlink()</code></strong> – Generates HTML anchor (<code>&lt;a&gt;</code>) tags from <code>text</code> and <code>url</code> columns for rendering clickable links.</li>
<li><strong><code>make_tooltip()</code></strong> – Creates HTML tooltips from <code>label</code> and <code>tooltip</code> columns for hoverable text display.</li>
</ul>
<p>Here’s a quick example that uses <code>ti.is_every_nth_row()</code> to build a Polars expression suitable for styling tables interactively with Great Tables:</p>
<div id="d7c109e5" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> countrypops</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> turtle_island <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ti</span>
<span id="cb1-6"></span>
<span id="cb1-7">df_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> countrypops.sample(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).loc[:, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>]]</span>
<span id="cb1-8">df_pl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.from_pandas(df_pd)</span>
<span id="cb1-9">row_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ti.is_every_nth_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">(</span>
<span id="cb1-12">    GT(df_pl)</span>
<span id="cb1-13">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_expr))</span>
<span id="cb1-14">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=~</span>row_expr))</span>
<span id="cb1-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb1-16">)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/turtle-island-intro/turtle_island_table_styling.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Styling table using Turtle Island"></p>
</figure>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>ti</category>
  <guid>https://tech.ycwu.space/posts/turtle-island-intro/20250706.html</guid>
  <pubDate>Sun, 06 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/turtle-island-intro/turtle_island_table_styling.png" medium="image" type="image/png" height="189" width="144"/>
</item>
<item>
  <title>Creating an Integrated marimo UI Explorer with Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-gt-ui-reference/20250704.html</link>
  <description><![CDATA[ 






<p>While working with marimo widgets, I often find myself wanting a quick glance at what each widget looks like or which parameters it accepts. That’s when I realized—this was a perfect opportunity to build an interactive UI cheatsheet using Great Tables inside a marimo notebook.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-gt-ui-reference/marimo_gt_ui_refrence.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="marimo UI explorer with Great Tables"></p>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>This interactive table includes four columns:</p>
<ol type="1">
<li>A link to the documentation.</li>
<li>A live widget preview.</li>
<li>Its current reactive value.</li>
<li>A code snippet with an accordion that reveals the widget’s signature.</li>
</ol>
<p>To keep things playful, I added a touch of randomness—so the table appears slightly different each time it loads.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0A%0Aimport%20random%0Afrom%20collections.abc%20import%20Iterable%0A%0Aimport%20pandas%20as%20pd%0Afrom%20great_tables%20import%20GT%2C%20html</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20render_widget(widget)%3A%0A%20%20%20%20if%20hasattr(widget%2C%20%22_display_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_display_%22%0A%20%20%20%20elif%20hasattr(widget%2C%20%22_repr_html_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_repr_html_%22%0A%20%20%20%20elif%20hasattr(widget%2C%20%22_mime_%22)%3A%0A%20%20%20%20%20%20%20%20render_method%20%3D%20%22_mime_%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20raise%20ValueError(%22The%20object%20does%20not%20have%20a%20valid%20render%20method.%22)%0A%20%20%20%20return%20getattr(widget%2C%20render_method)()%0A%0Adef%20render_widgets(widgets)%3A%0A%20%20%20%20if%20not%20isinstance(widgets%2C%20Iterable)%3A%0A%20%20%20%20%20%20%20%20widgets%20%3D%20%5Bwidgets%5D%0A%20%20%20%20return%20%5Brender_widget(widget)%20for%20widget%20in%20widgets%5D%0A%0Adef%20strify_widget_value(widget)%3A%0A%20%20%20%20return%20str(widget.value)%0A%0Adef%20strify_widget_values(widgets)%3A%0A%20%20%20%20if%20not%20isinstance(widgets%2C%20Iterable)%3A%0A%20%20%20%20%20%20%20%20widgets%20%3D%20%5Bwidgets%5D%0A%20%20%20%20return%20%5Bstrify_widget_value(w)%20for%20w%20in%20widgets%5D</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">switch%20%3D%20mo.ui.switch()%0Acheckbox%20%3D%20mo.ui.checkbox(label%3D%22check%20me%22)%0Adate%20%3D%20mo.ui.date()%0Arun_botton%20%3D%20mo.ui.run_button(label%3D%22Run%22)%0Abutton%20%3D%20mo.ui.button(%0A%20%20%20%20value%3D0%2C%20on_click%3Dlambda%20value%3A%20value%20%2B%201%2C%20label%3D%22increment%22%0A)%0Anumber%20%3D%20mo.ui.number(1%2C%2010)%0Aslider%20%3D%20mo.ui.slider(1%2C%2010%2C%201)%0Arange_slider%20%3D%20mo.ui.range_slider(1%2C%2010%2C%202%2C%20value%3D%5B2%2C%206%5D)%0Aradio%20%3D%20mo.ui.radio(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)%0Adropdown%20%3D%20mo.ui.dropdown(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)%0Amultiselect%20%3D%20mo.ui.multiselect(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D)%0Atext%20%3D%20mo.ui.text(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)%0Atext_area%20%3D%20mo.ui.text_area(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)%0A%0Awidgets%20%3D%20mo.ui.array(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20switch%2C%0A%20%20%20%20%20%20%20%20checkbox%2C%0A%20%20%20%20%20%20%20%20date%2C%0A%20%20%20%20%20%20%20%20run_botton%2C%0A%20%20%20%20%20%20%20%20button%2C%0A%20%20%20%20%20%20%20%20number%2C%0A%20%20%20%20%20%20%20%20slider%2C%0A%20%20%20%20%20%20%20%20range_slider%2C%0A%20%20%20%20%20%20%20%20radio%2C%0A%20%20%20%20%20%20%20%20dropdown%2C%0A%20%20%20%20%20%20%20%20multiselect%2C%0A%20%20%20%20%20%20%20%20text%2C%0A%20%20%20%20%20%20%20%20text_area%2C%0A%20%20%20%20%5D%0A)%0A%0Acol_widget_link%20%3D%20%5B%0A%20%20%20%20mo.md(%22%5BSwitch%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fswitch%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BCheckBox%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fcheckbox%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BDate%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fdates%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BRun%20Button%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Frun_button%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BButton%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fbutton%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BNumber%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fnumber%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BSlider%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fslider%2F)%22)%2C%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%5BRange%20Slider%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Frange_slider%2F)%22%0A%20%20%20%20)%2C%0A%20%20%20%20mo.md(%22%5BRadio%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fradio%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BDropdown%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fdropdown%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BMultiSelect%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Fmultiselect%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BText%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Ftext%2F)%22)%2C%0A%20%20%20%20mo.md(%22%5BText%20Area%5D(https%3A%2F%2Fdocs.marimo.io%2Fapi%2Finputs%2Ftext_area%2F)%22)%2C%0A%5D%0A%0Acol_code%20%3D%20%5B%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22switch%20%3D%20mo.ui.switch()%22%3A%20%22%60switch(value%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20disabled%3A%20bool%20%3D%20False%2C%20on_change%3A%20Optional%5BCallable%5B%5Bbool%5D%2C%20None%5D%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'checkbox%20%3D%20mo.ui.checkbox(label%3D%22check%20me%22)'%3A%20%22%60checkbox(value%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20disabled%3A%20bool%20%3D%20False%2C%20on_change%3A%20Optional%5BCallable%5B%5Bbool%5D%2C%20None%5D%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22date%20%3D%20mo.ui.date()%22%3A%20%22%60date(start%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20stop%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20value%3A%20Optional%5Bdate%20%7C%20str%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bdate%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'run_botton%20%3D%20mo.ui.run_button(label%3D%22Run%22)'%3A%20%22%60run_button(kind%3A%20Literal%5B'neutral'%2C%20'success'%2C%20'warn'%2C%20'danger'%5D%20%3D%20'neutral'%2C%20disabled%3A%20bool%20%3D%20False%2C%20tooltip%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20'click%20to%20run'%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20keyboard_shortcut%3A%20Optional%5Bstr%5D%20%3D%20None)%60%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'button%20%3D%20mo.ui.button(value%3D0%2C%20on_click%3Dlambda%20value%3A%20value%20%2B%201%2C%20label%3D%22increment%22)'%3A%20%22%60button(on_click%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20Any%5D%5D%20%3D%20None%2C%20value%3A%20Optional%5BAny%5D%20%3D%20None%2C%20kind%3A%20Literal%5B'neutral'%2C%20'success'%2C%20'warn'%2C%20'danger'%5D%20%3D%20'neutral'%2C%20disabled%3A%20bool%20%3D%20False%2C%20tooltip%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20'click%20here'%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20keyboard_shortcut%3A%20Optional%5Bstr%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22number%20%3D%20mo.ui.number(1%2C%2010)%22%3A%20%22%60number(start%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20stop%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20step%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20value%3A%20Optional%5Bfloat%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BOptional%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22slider%20%3D%20mo.ui.slider(1%2C%2010%2C%201)%22%3A%20%22%60slider(start%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20stop%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20step%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20value%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False%2C%20orientation%3A%20Literal%5B'horizontal'%2C%20'vertical'%5D%20%3D%20'horizontal'%2C%20show_value%3A%20bool%20%3D%20False%2C%20include_input%3A%20bool%20%3D%20False%2C%20steps%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BOptional%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22range_slider%20%3D%20mo.ui.range_slider(1%2C%2010%2C%202%2C%20value%3D%5B2%2C%206%5D)%22%3A%20%22%60range_slider(start%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20stop%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20step%3A%20Optional%5BNumeric%5D%20%3D%20None%2C%20value%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20debounce%3A%20bool%20%3D%20False%2C%20orientation%3A%20Literal%5B'horizontal'%2C%20'vertical'%5D%20%3D%20'horizontal'%2C%20show_value%3A%20bool%20%3D%20False%2C%20steps%3A%20Optional%5BSequence%5BNumeric%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BSequence%5BNumeric%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'radio%20%3D%20mo.ui.radio(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)'%3A%20%22%60radio(options%3A%20Sequence%5Bstr%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5Bstr%5D%20%3D%20None%2C%20inline%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'dropdown%20%3D%20mo.ui.dropdown(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D%2C%20value%3D%22Apples%22)'%3A%20%22%60dropdown(options%3A%20Sequence%5BAny%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5BAny%5D%20%3D%20None%2C%20allow_select_none%3A%20Optional%5Bbool%5D%20%3D%20None%2C%20searchable%3A%20bool%20%3D%20False%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5BAny%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'multiselect%20%3D%20mo.ui.multiselect(options%3D%5B%22Apples%22%2C%20%22Oranges%22%5D)'%3A%20%22%60multiselect(options%3A%20Sequence%5BAny%5D%20%7C%20dict%5Bstr%2C%20Any%5D%2C%20value%3A%20Optional%5BSequence%5BAny%5D%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Blist%5Bobject%5D%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False%2C%20max_selections%3A%20Optional%5Bint%5D%20%3D%20None)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'text%20%3D%20mo.ui.text(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)'%3A%20%22%60text(value%3A%20str%20%3D%20''%2C%20placeholder%3A%20str%20%3D%20''%2C%20kind%3A%20Literal%5B'text'%2C%20'password'%2C%20'email'%2C%20'url'%5D%20%3D%20'text'%2C%20max_length%3A%20Optional%5Bint%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False%2C%20debounce%3A%20bool%20%7C%20int%20%3D%20True%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bstr%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20mo.accordion(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20'text_area%20%3D%20mo.ui.text_area(placeholder%3D%22placeholder...%22%2C%20debounce%3DFalse)'%3A%20%22%60text_area(value%3A%20str%20%3D%20''%2C%20placeholder%3A%20str%20%3D%20''%2C%20max_length%3A%20Optional%5Bint%5D%20%3D%20None%2C%20disabled%3A%20bool%20%3D%20False%2C%20debounce%3A%20bool%20%7C%20int%20%3D%20True%2C%20rows%3A%20Optional%5Bint%5D%20%3D%20None%2C%20*%2C%20label%3A%20str%20%3D%20''%2C%20on_change%3A%20Optional%5BCallable%5B%5Bstr%5D%2C%20None%5D%5D%20%3D%20None%2C%20full_width%3A%20bool%20%3D%20False)%60%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%5D%0A%0A%0A%23%20table%20styling%0A_style_number_start%2C%20_style_number_end%20%3D%201%2C%206%0Astyle_widget%20%3D%20mo.ui.slider(%0A%20%20%20%20_style_number_start%2C%0A%20%20%20%20_style_number_end%2C%0A%20%20%20%20value%3Drandom.randint(_style_number_start%2C%20_style_number_end)%2C%0A%20%20%20%20label%3D%22Style%20Number%22%2C%0A)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%0A%20%20%20%20value%3Drandom.choice(_colors)%2C%0A%20%20%20%20label%3D%22Style%20Color%22%2C%0A%20%20%20%20inline%3DTrue%2C%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">col_widget%20%3D%20render_widgets(widgets)%0Acol_value%20%3D%20strify_widget_values(widgets)%0A%0Adata%20%3D%20%7B%0A%20%20%20%20%22link%22%3A%20col_widget_link%2C%0A%20%20%20%20%22widget%22%3A%20col_widget%2C%0A%20%20%20%20%22value%22%3A%20col_value%2C%0A%20%20%20%20%22code%22%3A%20col_code%2C%0A%7D%0A%0Adf%20%3D%20pd.DataFrame(data)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">gt%20%3D%20(%0A%20%20%20%20GT(df)%0A%20%20%20%20.cols_align(%22left%22)%0A%20%20%20%20.opt_all_caps()%0A%20%20%20%20.tab_header(html(style_widget)%2C%20html(color_widget))%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="PKri" data-reactive="true">
            <marimo-cell-output>
            <div id="kppnbvgksy" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#kppnbvgksy table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#kppnbvgksy thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#kppnbvgksy p { margin: 0; padding: 0; }
 #kppnbvgksy .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #D5D5D5; border-right-style: solid; border-right-width: 3px; border-right-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: #D5D5D5; border-left-style: solid; border-left-width: 3px; border-left-color: #D5D5D5; }
 #kppnbvgksy .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #kppnbvgksy .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #kppnbvgksy .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #kppnbvgksy .gt_heading { background-color: #FFFFFF; text-align: left; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #kppnbvgksy .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #kppnbvgksy .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #kppnbvgksy .gt_col_heading { color: #FFFFFF; background-color: #99195F; font-size: 80%; font-weight: bolder; text-transform: uppercase; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #kppnbvgksy .gt_column_spanner_outer { color: #FFFFFF; background-color: #99195F; font-size: 80%; font-weight: bolder; text-transform: uppercase; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #kppnbvgksy .gt_column_spanner_outer:first-child { padding-left: 0; }
 #kppnbvgksy .gt_column_spanner_outer:last-child { padding-right: 0; }
 #kppnbvgksy .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #kppnbvgksy .gt_spanner_row { border-bottom-style: hidden; }
 #kppnbvgksy .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 80%; font-weight: bolder; text-transform: uppercase; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #kppnbvgksy .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 80%; font-weight: bolder; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: middle; }
 #kppnbvgksy .gt_from_md> :first-child { margin-top: 0; }
 #kppnbvgksy .gt_from_md> :last-child { margin-bottom: 0; }
 #kppnbvgksy .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D5D5D5; border-left-style: solid; border-left-width: 1px; border-left-color: #D5D5D5; border-right-style: solid; border-right-width: 1px; border-right-color: #D5D5D5; vertical-align: middle; overflow-x: hidden; }
 #kppnbvgksy .gt_stub { color: #333333; background-color: #929292; font-size: 80%; font-weight: bolder; text-transform: uppercase; border-right-style: solid; border-right-width: 2px; border-right-color: #D5D5D5; padding-left: 5px; padding-right: 5px; }
 #kppnbvgksy .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #kppnbvgksy .gt_row_group_first td { border-top-width: 2px; }
 #kppnbvgksy .gt_row_group_first th { border-top-width: 2px; }
 #kppnbvgksy .gt_striped { color: #333333; background-color: #F4F4F4; }
 #kppnbvgksy .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #kppnbvgksy .gt_grand_summary_row { color: #333333; background-color: #929292; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #kppnbvgksy .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #kppnbvgksy .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #kppnbvgksy .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #kppnbvgksy .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #kppnbvgksy .gt_left { text-align: left; }
 #kppnbvgksy .gt_center { text-align: center; }
 #kppnbvgksy .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #kppnbvgksy .gt_font_normal { font-weight: normal; }
 #kppnbvgksy .gt_font_bold { font-weight: bold; }
 #kppnbvgksy .gt_font_italic { font-style: italic; }
 #kppnbvgksy .gt_super { font-size: 65%; }
 #kppnbvgksy .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #kppnbvgksy .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table style="table-layout: fixed;" class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<colgroup>
  <col>
  <col style="width:20%;">
  <col>
  <col>
</colgroup>
<thead>
  <tr class="gt_heading">
    <td colspan="4" class="gt_heading gt_title gt_font_normal"><marimo-ui-element object-id="vblA-27" random-id="be7ec8c4-eb08-12b2-0eba-852267efc5cf"><marimo-slider data-initial-value="5" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Style Number</span></span>&quot;" data-start="1" data-stop="6" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element></td>
  </tr>
  <tr class="gt_heading">
    <td colspan="4" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border"><marimo-ui-element object-id="vblA-28" random-id="0a96fcfe-463c-e74b-87fa-94699cfff2f0"><marimo-radio data-initial-value="&quot;pink&quot;" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Style Color</span></span>&quot;" data-options="[&quot;blue&quot;,&quot;cyan&quot;,&quot;pink&quot;,&quot;green&quot;,&quot;red&quot;,&quot;gray&quot;]" data-inline="true" data-disabled="false"></marimo-radio></marimo-ui-element></td>
  </tr>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="link">link</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="widget">widget</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="value">value</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="code">code</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_left">[Switch](https://docs.marimo.io/api/inputs/switch/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-13" random-id="d9591fef-c12d-f844-a3d3-a6d1d0b2cad9"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">False</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>switch = mo.ui.switch()</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>switch(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[CheckBox](https://docs.marimo.io/api/inputs/checkbox/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-14" random-id="6e760863-c3d4-5395-0f1a-f6fbbc5294c1"><marimo-checkbox data-initial-value="false" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>check me</span></span>&quot;" data-disabled="false"></marimo-checkbox></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">False</td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>checkbox = mo.ui.checkbox(label=\&quot;check me\&quot;)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>checkbox(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[Date](https://docs.marimo.io/api/inputs/dates/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-15" random-id="f8c0000f-5f87-72ab-5401-6f58aaa09fac"><marimo-date data-initial-value="&quot;2026-05-10&quot;" data-label="null" data-start="&quot;0001-01-01&quot;" data-stop="&quot;9999-12-31&quot;" data-full-width="false" data-disabled="false"></marimo-date></marimo-ui-element></td>
    <td class="gt_row gt_left">2026-05-10</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>date = mo.ui.date()</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>date(start: Optional[date | str] = None, stop: Optional[date | str] = None, value: Optional[date | str] = None, *, label: str = '', on_change: Optional[Callable[[date], None]] = None, full_width: bool = False, disabled: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[Run Button](https://docs.marimo.io/api/inputs/run_button/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-16" random-id="abb25cdf-3bae-42c6-6b3d-d9f35d74a2a1"><marimo-button data-initial-value="0" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Run</span></span>&quot;" data-kind="&quot;neutral&quot;" data-disabled="false" data-full-width="false"></marimo-button></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">False</td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>run_botton = mo.ui.run_button(label=\&quot;Run\&quot;)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>run_button(kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click to run', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[Button](https://docs.marimo.io/api/inputs/button/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-17" random-id="b80d1df3-2cbb-66fa-6d1f-fbb28939c1de"><marimo-button data-initial-value="0" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>increment</span></span>&quot;" data-kind="&quot;neutral&quot;" data-disabled="false" data-full-width="false"></marimo-button></marimo-ui-element></td>
    <td class="gt_row gt_left">0</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>button = mo.ui.button(value=0, on_click=lambda value: value + 1, label=\&quot;increment\&quot;)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>button(on_click: Optional[Callable[[Any], Any]] = None, value: Optional[Any] = None, kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click here', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[Number](https://docs.marimo.io/api/inputs/number/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-18" random-id="1847990d-3494-ef4d-2062-08574c46a380"><marimo-number data-initial-value="1" data-label="null" data-start="1" data-stop="10" data-debounce="false" data-full-width="false" data-disabled="false"></marimo-number></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">1</td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>number = mo.ui.number(1, 10)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>number(start: Optional[float] = None, stop: Optional[float] = None, step: Optional[float] = None, value: Optional[float] = None, debounce: bool = False, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[Slider](https://docs.marimo.io/api/inputs/slider/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-19" random-id="8ebbc91e-59d5-f1e1-4bb5-ef2e2faab2f6"><marimo-slider data-initial-value="1" data-label="null" data-start="1" data-stop="10" data-step="1" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element></td>
    <td class="gt_row gt_left">1</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>slider = mo.ui.slider(1, 10, 1)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Numeric] = None, debounce: bool = False, disabled: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, include_input: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[Range Slider](https://docs.marimo.io/api/inputs/range_slider/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-20" random-id="ba57fd24-f0a4-81ae-6894-8ff1e7c1bff2"><marimo-range-slider data-initial-value="[2,6]" data-label="null" data-start="1" data-stop="10" data-step="2" data-steps="[]" data-debounce="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-full-width="false" data-disabled="false"></marimo-range-slider></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">[2, 6]</td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>range_slider = mo.ui.range_slider(1, 10, 2, value=[2, 6])</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>range_slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Sequence[Numeric]] = None, debounce: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Sequence[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[Radio](https://docs.marimo.io/api/inputs/radio/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-21" random-id="de767ee1-e72a-347e-a27d-ad442d29d7c6"><marimo-radio data-initial-value="&quot;Apples&quot;" data-label="null" data-options="[&quot;Apples&quot;,&quot;Oranges&quot;]" data-inline="false" data-disabled="false"></marimo-radio></marimo-ui-element></td>
    <td class="gt_row gt_left">Apples</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>radio = mo.ui.radio(options=[\&quot;Apples\&quot;, \&quot;Oranges\&quot;], value=\&quot;Apples\&quot;)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>radio(options: Sequence[str] | dict[str, Any], value: Optional[str] = None, inline: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, disabled: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[Dropdown](https://docs.marimo.io/api/inputs/dropdown/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-22" random-id="b1600d22-dc89-fcd4-332c-1ec98425e91a"><marimo-dropdown data-initial-value="[&quot;Apples&quot;]" data-label="null" data-options="[&quot;Apples&quot;,&quot;Oranges&quot;]" data-allow-select-none="false" data-searchable="false" data-full-width="false"></marimo-dropdown></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Apples</td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>dropdown = mo.ui.dropdown(options=[\&quot;Apples\&quot;, \&quot;Oranges\&quot;], value=\&quot;Apples\&quot;)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>dropdown(options: Sequence[Any] | dict[str, Any], value: Optional[Any] = None, allow_select_none: Optional[bool] = None, searchable: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[MultiSelect](https://docs.marimo.io/api/inputs/multiselect/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-23" random-id="37ad9221-f94f-f6a5-2c56-5bafe4c800e9"><marimo-multiselect data-initial-value="[]" data-label="null" data-options="[&quot;Apples&quot;,&quot;Oranges&quot;]" data-full-width="false"></marimo-multiselect></marimo-ui-element></td>
    <td class="gt_row gt_left">[]</td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>multiselect = mo.ui.multiselect(options=[\&quot;Apples\&quot;, \&quot;Oranges\&quot;])</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>multiselect(options: Sequence[Any] | dict[str, Any], value: Optional[Sequence[Any]] = None, *, label: str = '', on_change: Optional[Callable[[list[object]], None]] = None, full_width: bool = False, max_selections: Optional[int] = None)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped">[Text](https://docs.marimo.io/api/inputs/text/)</td>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-24" random-id="cafe2672-341a-9e0b-a8da-12c3011579b4"><marimo-text data-initial-value="&quot;&quot;" data-label="null" data-placeholder="&quot;placeholder...&quot;" data-kind="&quot;text&quot;" data-full-width="false" data-disabled="false" data-debounce="false"></marimo-text></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped"></td>
    <td class="gt_row gt_left gt_striped"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>text = mo.ui.text(placeholder=\&quot;placeholder...\&quot;, debounce=False)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>text(value: str = '', placeholder: str = '', kind: Literal['text', 'password', 'email', 'url'] = 'text', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
  <tr>
    <td class="gt_row gt_left">[Text Area](https://docs.marimo.io/api/inputs/text_area/)</td>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-25" random-id="a9b7aca3-b5fe-0695-f066-b08fd9cac56c"><marimo-text-area data-initial-value="&quot;&quot;" data-label="null" data-placeholder="&quot;placeholder...&quot;" data-disabled="false" data-debounce="false" data-full-width="false"></marimo-text-area></marimo-ui-element></td>
    <td class="gt_row gt_left"></td>
    <td class="gt_row gt_left"><marimo-accordion data-labels="[&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>text_area = mo.ui.text_area(placeholder=\&quot;placeholder...\&quot;, debounce=False)</span></span>&quot;]" data-multiple="false"><div><span class="markdown prose dark:prose-invert contents"><span class="paragraph"><code>text_area(value: str = '', placeholder: str = '', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, rows: Optional[int] = None, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)</code></span></span></div></marimo-accordion></td>
  </tr>
</tbody>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">(%0A%20%20%20%20gt.opt_stylize(style%3Dstyle_widget.value%2C%20color%3Dcolor_widget.value)%0A%20%20%20%20.opt_align_table_header(%22left%22)%0A%20%20%20%20.cols_width(%7B%22widget%22%3A%20%2220%25%22%7D)%0A)</marimo-cell-code>
        </marimo-island>
<p>One interesting discovery: the new state of the <a href="https://docs.marimo.io/api/inputs/run_button/">mo.ui.run_button</a> doesn’t persist as expected. This may be resolved by more effectively using marimo’s <a href="https://docs.marimo.io/guides/state/?h=state">reactive state</a>, which I plan to explore further.</p>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_zeHb8T4sPPfbjokeEJnEn6/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.10"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb1-12"></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb1-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> collections.abc <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Iterable</span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-20">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb1-21"></span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, Iterable, html, pd, random</span>
<span id="cb1-23"></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(Iterable):</span>
<span id="cb1-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render_widget(widget):</span>
<span id="cb1-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb1-29">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb1-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb1-31">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb1-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(widget, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb1-33">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb1-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb1-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span>)</span>
<span id="cb1-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(widget, render_method)()</span>
<span id="cb1-37"></span>
<span id="cb1-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> render_widgets(widgets):</span>
<span id="cb1-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(widgets, Iterable):</span>
<span id="cb1-40">            widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [widgets]</span>
<span id="cb1-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [render_widget(widget) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> widget <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> widgets]</span>
<span id="cb1-42"></span>
<span id="cb1-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> strify_widget_value(widget):</span>
<span id="cb1-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(widget.value)</span>
<span id="cb1-45"></span>
<span id="cb1-46">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> strify_widget_values(widgets):</span>
<span id="cb1-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(widgets, Iterable):</span>
<span id="cb1-48">            widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [widgets]</span>
<span id="cb1-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [strify_widget_value(w) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> w <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> widgets]</span>
<span id="cb1-50"></span>
<span id="cb1-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> render_widgets, strify_widget_values</span>
<span id="cb1-52"></span>
<span id="cb1-53"></span>
<span id="cb1-54"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-55"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, random):</span>
<span id="cb1-56">    switch <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch()</span>
<span id="cb1-57">    checkbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.checkbox(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"check me"</span>)</span>
<span id="cb1-58">    date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.date()</span>
<span id="cb1-59">    run_botton <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.run_button(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run"</span>)</span>
<span id="cb1-60">    button <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.button(</span>
<span id="cb1-61">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, on_click<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> value: value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"increment"</span></span>
<span id="cb1-62">    )</span>
<span id="cb1-63">    number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.number(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-64">    slider <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-65">    range_slider <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.range_slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])</span>
<span id="cb1-66">    radio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>], value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>)</span>
<span id="cb1-67">    dropdown <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.dropdown(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>], value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>)</span>
<span id="cb1-68">    multiselect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.multiselect(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apples"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oranges"</span>])</span>
<span id="cb1-69">    text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.text(placeholder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"placeholder..."</span>, debounce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-70">    text_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.text_area(placeholder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"placeholder..."</span>, debounce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-71"></span>
<span id="cb1-72">    widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.array(</span>
<span id="cb1-73">        [</span>
<span id="cb1-74">            switch,</span>
<span id="cb1-75">            checkbox,</span>
<span id="cb1-76">            date,</span>
<span id="cb1-77">            run_botton,</span>
<span id="cb1-78">            button,</span>
<span id="cb1-79">            number,</span>
<span id="cb1-80">            slider,</span>
<span id="cb1-81">            range_slider,</span>
<span id="cb1-82">            radio,</span>
<span id="cb1-83">            dropdown,</span>
<span id="cb1-84">            multiselect,</span>
<span id="cb1-85">            text,</span>
<span id="cb1-86">            text_area,</span>
<span id="cb1-87">        ]</span>
<span id="cb1-88">    )</span>
<span id="cb1-89"></span>
<span id="cb1-90">    col_widget_link <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-91">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Switch](https://docs.marimo.io/api/inputs/switch/)"</span>),</span>
<span id="cb1-92">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[CheckBox](https://docs.marimo.io/api/inputs/checkbox/)"</span>),</span>
<span id="cb1-93">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Date](https://docs.marimo.io/api/inputs/dates/)"</span>),</span>
<span id="cb1-94">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Run Button](https://docs.marimo.io/api/inputs/run_button/)"</span>),</span>
<span id="cb1-95">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Button](https://docs.marimo.io/api/inputs/button/)"</span>),</span>
<span id="cb1-96">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Number](https://docs.marimo.io/api/inputs/number/)"</span>),</span>
<span id="cb1-97">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Slider](https://docs.marimo.io/api/inputs/slider/)"</span>),</span>
<span id="cb1-98">        mo.md(</span>
<span id="cb1-99">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Range Slider](https://docs.marimo.io/api/inputs/range_slider/)"</span></span>
<span id="cb1-100">        ),</span>
<span id="cb1-101">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Radio](https://docs.marimo.io/api/inputs/radio/)"</span>),</span>
<span id="cb1-102">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Dropdown](https://docs.marimo.io/api/inputs/dropdown/)"</span>),</span>
<span id="cb1-103">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[MultiSelect](https://docs.marimo.io/api/inputs/multiselect/)"</span>),</span>
<span id="cb1-104">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Text](https://docs.marimo.io/api/inputs/text/)"</span>),</span>
<span id="cb1-105">        mo.md(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[Text Area](https://docs.marimo.io/api/inputs/text_area/)"</span>),</span>
<span id="cb1-106">    ]</span>
<span id="cb1-107"></span>
<span id="cb1-108">    col_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-109">        mo.accordion(</span>
<span id="cb1-110">            {</span>
<span id="cb1-111">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"switch = mo.ui.switch()"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`switch(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)`"</span></span>
<span id="cb1-112">            }</span>
<span id="cb1-113">        ),</span>
<span id="cb1-114">        mo.accordion(</span>
<span id="cb1-115">            {</span>
<span id="cb1-116">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'checkbox = mo.ui.checkbox(label="check me")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`checkbox(value: bool = False, *, label: str = '', disabled: bool = False, on_change: Optional[Callable[[bool], None]] = None)`"</span></span>
<span id="cb1-117">            }</span>
<span id="cb1-118">        ),</span>
<span id="cb1-119">        mo.accordion(</span>
<span id="cb1-120">            {</span>
<span id="cb1-121">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date = mo.ui.date()"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`date(start: Optional[date | str] = None, stop: Optional[date | str] = None, value: Optional[date | str] = None, *, label: str = '', on_change: Optional[Callable[[date], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-122">            }</span>
<span id="cb1-123">        ),</span>
<span id="cb1-124">        mo.accordion(</span>
<span id="cb1-125">            {</span>
<span id="cb1-126">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'run_botton = mo.ui.run_button(label="Run")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`run_button(kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click to run', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)`"</span>,</span>
<span id="cb1-127">            }</span>
<span id="cb1-128">        ),</span>
<span id="cb1-129">        mo.accordion(</span>
<span id="cb1-130">            {</span>
<span id="cb1-131">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'button = mo.ui.button(value=0, on_click=lambda value: value + 1, label="increment")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`button(on_click: Optional[Callable[[Any], Any]] = None, value: Optional[Any] = None, kind: Literal['neutral', 'success', 'warn', 'danger'] = 'neutral', disabled: bool = False, tooltip: Optional[str] = None, *, label: str = 'click here', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False, keyboard_shortcut: Optional[str] = None)`"</span></span>
<span id="cb1-132">            }</span>
<span id="cb1-133">        ),</span>
<span id="cb1-134">        mo.accordion(</span>
<span id="cb1-135">            {</span>
<span id="cb1-136">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"number = mo.ui.number(1, 10)"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`number(start: Optional[float] = None, stop: Optional[float] = None, step: Optional[float] = None, value: Optional[float] = None, debounce: bool = False, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-137">            }</span>
<span id="cb1-138">        ),</span>
<span id="cb1-139">        mo.accordion(</span>
<span id="cb1-140">            {</span>
<span id="cb1-141">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"slider = mo.ui.slider(1, 10, 1)"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Numeric] = None, debounce: bool = False, disabled: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, include_input: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Optional[Numeric]], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-142">            }</span>
<span id="cb1-143">        ),</span>
<span id="cb1-144">        mo.accordion(</span>
<span id="cb1-145">            {</span>
<span id="cb1-146">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"range_slider = mo.ui.range_slider(1, 10, 2, value=[2, 6])"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`range_slider(start: Optional[Numeric] = None, stop: Optional[Numeric] = None, step: Optional[Numeric] = None, value: Optional[Sequence[Numeric]] = None, debounce: bool = False, orientation: Literal['horizontal', 'vertical'] = 'horizontal', show_value: bool = False, steps: Optional[Sequence[Numeric]] = None, *, label: str = '', on_change: Optional[Callable[[Sequence[Numeric]], None]] = None, full_width: bool = False, disabled: bool = False)`"</span></span>
<span id="cb1-147">            }</span>
<span id="cb1-148">        ),</span>
<span id="cb1-149">        mo.accordion(</span>
<span id="cb1-150">            {</span>
<span id="cb1-151">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'radio = mo.ui.radio(options=["Apples", "Oranges"], value="Apples")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`radio(options: Sequence[str] | dict[str, Any], value: Optional[str] = None, inline: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, disabled: bool = False)`"</span></span>
<span id="cb1-152">            }</span>
<span id="cb1-153">        ),</span>
<span id="cb1-154">        mo.accordion(</span>
<span id="cb1-155">            {</span>
<span id="cb1-156">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dropdown = mo.ui.dropdown(options=["Apples", "Oranges"], value="Apples")'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`dropdown(options: Sequence[Any] | dict[str, Any], value: Optional[Any] = None, allow_select_none: Optional[bool] = None, searchable: bool = False, *, label: str = '', on_change: Optional[Callable[[Any], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-157">            }</span>
<span id="cb1-158">        ),</span>
<span id="cb1-159">        mo.accordion(</span>
<span id="cb1-160">            {</span>
<span id="cb1-161">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'multiselect = mo.ui.multiselect(options=["Apples", "Oranges"])'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`multiselect(options: Sequence[Any] | dict[str, Any], value: Optional[Sequence[Any]] = None, *, label: str = '', on_change: Optional[Callable[[list[object]], None]] = None, full_width: bool = False, max_selections: Optional[int] = None)`"</span></span>
<span id="cb1-162">            }</span>
<span id="cb1-163">        ),</span>
<span id="cb1-164">        mo.accordion(</span>
<span id="cb1-165">            {</span>
<span id="cb1-166">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text = mo.ui.text(placeholder="placeholder...", debounce=False)'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`text(value: str = '', placeholder: str = '', kind: Literal['text', 'password', 'email', 'url'] = 'text', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-167">            }</span>
<span id="cb1-168">        ),</span>
<span id="cb1-169">        mo.accordion(</span>
<span id="cb1-170">            {</span>
<span id="cb1-171">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text_area = mo.ui.text_area(placeholder="placeholder...", debounce=False)'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"`text_area(value: str = '', placeholder: str = '', max_length: Optional[int] = None, disabled: bool = False, debounce: bool | int = True, rows: Optional[int] = None, *, label: str = '', on_change: Optional[Callable[[str], None]] = None, full_width: bool = False)`"</span></span>
<span id="cb1-172">            }</span>
<span id="cb1-173">        ),</span>
<span id="cb1-174">    ]</span>
<span id="cb1-175"></span>
<span id="cb1-176">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># table styling</span></span>
<span id="cb1-177">    _style_number_start, _style_number_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-178">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(</span>
<span id="cb1-179">        _style_number_start,</span>
<span id="cb1-180">        _style_number_end,</span>
<span id="cb1-181">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>random.randint(_style_number_start, _style_number_end),</span>
<span id="cb1-182">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Number"</span>,</span>
<span id="cb1-183">    )</span>
<span id="cb1-184"></span>
<span id="cb1-185">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-186">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-187">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors,</span>
<span id="cb1-188">        value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>random.choice(_colors),</span>
<span id="cb1-189">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Color"</span>,</span>
<span id="cb1-190">        inline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb1-191">    )</span>
<span id="cb1-192">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> col_code, col_widget_link, color_widget, style_widget, widgets</span>
<span id="cb1-193"></span>
<span id="cb1-194"></span>
<span id="cb1-195"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-196"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(</span>
<span id="cb1-197">    col_code,</span>
<span id="cb1-198">    col_widget_link,</span>
<span id="cb1-199">    pd,</span>
<span id="cb1-200">    render_widgets,</span>
<span id="cb1-201">    strify_widget_values,</span>
<span id="cb1-202">    widgets,</span>
<span id="cb1-203">):</span>
<span id="cb1-204">    col_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> render_widgets(widgets)</span>
<span id="cb1-205">    col_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strify_widget_values(widgets)</span>
<span id="cb1-206"></span>
<span id="cb1-207">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-208">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"link"</span>: col_widget_link,</span>
<span id="cb1-209">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"widget"</span>: col_widget,</span>
<span id="cb1-210">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>: col_value,</span>
<span id="cb1-211">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code"</span>: col_code,</span>
<span id="cb1-212">    }</span>
<span id="cb1-213"></span>
<span id="cb1-214">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(data)</span>
<span id="cb1-215">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-216"></span>
<span id="cb1-217"></span>
<span id="cb1-218"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-219"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, color_widget, df, html, style_widget):</span>
<span id="cb1-220">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-221">        GT(df)</span>
<span id="cb1-222">        .cols_align(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-223">        .opt_all_caps()</span>
<span id="cb1-224">        .tab_header(html(style_widget), html(color_widget))</span>
<span id="cb1-225">    )</span>
<span id="cb1-226">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (gt,)</span>
<span id="cb1-227"></span>
<span id="cb1-228"></span>
<span id="cb1-229"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-230"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(color_widget, gt, style_widget):</span>
<span id="cb1-231">    (</span>
<span id="cb1-232">        gt.opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value)</span>
<span id="cb1-233">        .opt_align_table_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-234">        .cols_width({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"widget"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20%"</span>})</span>
<span id="cb1-235">    )</span>
<span id="cb1-236">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-237"></span>
<span id="cb1-238"></span>
<span id="cb1-239"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-240">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/marimo-gt-ui-reference/20250704.html</guid>
  <pubDate>Fri, 04 Jul 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-gt-ui-reference/marimo_gt_ui_refrence.png" medium="image" type="image/png" height="124" width="144"/>
</item>
<item>
  <title>Time Machine for Great Tables in marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-gt-time-machine/20250626.html</link>
  <description><![CDATA[ 






<p>This post demonstrates how to build a “time machine” that lets you easily navigate through different stages of table construction using Great Tables in a marimo notebook.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-gt-time-machine/marimo_gt_time_machine.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Time Machine for Great Tables"></p>
</figure>
</div>
<section id="building-the-time-machine-core" class="level2">
<h2 class="anchored" data-anchor-id="building-the-time-machine-core">Building the Time Machine Core</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>The goal is to create a <code>WigGT</code> object that behaves like a <code>GT</code> object from Great Tables. As we progressively build the table, we maintain two versions:</p>
<ul>
<li>The original (non-interactive) table</li>
<li>An interactive version that includes a marimo widget (in this case, a slider)</li>
</ul>
<p>This setup allows us to move back and forth through the table-building steps and see how each method transforms the output — effectively creating a table “time machine.”</p>
<p>Let’s break it down.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20copy%0Aimport%20inspect%0Afrom%20functools%20import%20wraps%0Afrom%20typing%20import%20Callable%2C%20Self%0A%0Aimport%20pandas%20as%20pd%0Afrom%20great_tables%20import%20GT%2C%20html%0Afrom%20great_tables.data%20import%20airquality</marimo-cell-code>
</marimo-island>
<section id="getting-the-allowed-method-names" class="level3">
<h3 class="anchored" data-anchor-id="getting-the-allowed-method-names">Getting the Allowed Method Names</h3>
<p>First, we need a list of Great Tables methods that should be wrapped and recorded. For now, this list is constructed manually, but ideally, Great Tables could expose this for easier third-party use.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_allowed_member_names() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]:</span>
<span id="cb1-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_number"</span>, ...]</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20get_allowed_member_names()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20return%20%5B%0A%20%20%20%20%20%20%20%20%22fmt%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_number%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_integer%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_percent%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_scientific%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_currency%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_bytes%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_roman%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_date%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_time%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_datetime%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_markdown%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_image%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_icon%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_flag%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_units%22%2C%0A%20%20%20%20%20%20%20%20%22fmt_nanoplot%22%2C%0A%20%20%20%20%20%20%20%20%22data_color%22%2C%0A%20%20%20%20%20%20%20%20%22sub_missing%22%2C%0A%20%20%20%20%20%20%20%20%22sub_zero%22%2C%0A%20%20%20%20%20%20%20%20%22opt_stylize%22%2C%0A%20%20%20%20%20%20%20%20%22opt_align_table_header%22%2C%0A%20%20%20%20%20%20%20%20%22opt_all_caps%22%2C%0A%20%20%20%20%20%20%20%20%22opt_footnote_marks%22%2C%0A%20%20%20%20%20%20%20%20%22opt_row_striping%22%2C%0A%20%20%20%20%20%20%20%20%22opt_vertical_padding%22%2C%0A%20%20%20%20%20%20%20%20%22opt_horizontal_padding%22%2C%0A%20%20%20%20%20%20%20%20%22opt_table_outline%22%2C%0A%20%20%20%20%20%20%20%20%22opt_table_font%22%2C%0A%20%20%20%20%20%20%20%20%22cols_align%22%2C%0A%20%20%20%20%20%20%20%20%22cols_width%22%2C%0A%20%20%20%20%20%20%20%20%22cols_label%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move_to_start%22%2C%0A%20%20%20%20%20%20%20%20%22cols_move_to_end%22%2C%0A%20%20%20%20%20%20%20%20%22cols_hide%22%2C%0A%20%20%20%20%20%20%20%20%22cols_unhide%22%2C%0A%20%20%20%20%20%20%20%20%22tab_header%22%2C%0A%20%20%20%20%20%20%20%20%22tab_source_note%22%2C%0A%20%20%20%20%20%20%20%20%22tab_spanner%22%2C%0A%20%20%20%20%20%20%20%20%22tab_stubhead%22%2C%0A%20%20%20%20%20%20%20%20%22tab_style%22%2C%0A%20%20%20%20%20%20%20%20%22tab_options%22%2C%0A%20%20%20%20%20%20%20%20%22row_group_order%22%2C%0A%20%20%20%20%20%20%20%20%22tab_stub%22%2C%0A%20%20%20%20%20%20%20%20%22with_id%22%2C%0A%20%20%20%20%20%20%20%20%22with_locale%22%2C%0A%20%20%20%20%20%20%20%20%22save%22%2C%0A%20%20%20%20%20%20%20%20%22show%22%2C%0A%20%20%20%20%20%20%20%20%22as_raw_html%22%2C%0A%20%20%20%20%20%20%20%20%22write_raw_html%22%2C%0A%20%20%20%20%20%20%20%20%22as_latex%22%2C%0A%20%20%20%20%20%20%20%20%22pipe%22%2C%0A%20%20%20%20%5D</marimo-cell-code>
</marimo-island>
</section>
<section id="the-lazify-decorator" class="level3">
<h3 class="anchored" data-anchor-id="the-lazify-decorator">The <code>lazify</code> Decorator</h3>
<p>The <code>lazify</code> decorator wraps each method call and stores it in a pipeline. This allows us to defer execution until we’re ready to materialize the final table using <code>.collect()</code>. Only the selected methods from Great Tables will be decorated.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lazify(cls: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb2-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_to_pipeline(func: Callable[..., GT]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>:</span>
<span id="cb2-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@wraps</span>(func)</span>
<span id="cb2-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb2-5">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> inner(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb2-6">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> func(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb2-7"></span>
<span id="cb2-8">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># container for storing callable objects</span></span>
<span id="cb2-9">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.append(inner)</span>
<span id="cb2-10">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb2-11"></span>
<span id="cb2-12">        wrapper.__signature__ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inspect.signature(func)</span>
<span id="cb2-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb2-14"></span>
<span id="cb2-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> member_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> get_allowed_member_names():</span>
<span id="cb2-16">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">setattr</span>(</span>
<span id="cb2-17">            cls, member_name, add_to_pipeline(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(GT, member_name))</span>
<span id="cb2-18">        )</span>
<span id="cb2-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20lazify(cls%3A%20GT)%20-%3E%20GT%3A%0A%20%20%20%20def%20add_to_pipeline(func%3A%20Callable%5B...%2C%20GT%5D)%20-%3E%20callable%3A%0A%20%20%20%20%20%20%20%20%40wraps(func)%0A%20%20%20%20%20%20%20%20def%20wrapper(self%2C%20*args%2C%20**kwargs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20inner(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20func(self%2C%20*args%2C%20**kwargs)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20container%20for%20storing%20callable%20objects%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline.append(inner)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self%0A%0A%20%20%20%20%20%20%20%20wrapper.__signature__%20%3D%20inspect.signature(func)%0A%20%20%20%20%20%20%20%20return%20wrapper%0A%0A%20%20%20%20for%20member_name%20in%20get_allowed_member_names()%3A%0A%20%20%20%20%20%20%20%20setattr(%0A%20%20%20%20%20%20%20%20%20%20%20%20cls%2C%20member_name%2C%20add_to_pipeline(getattr(GT%2C%20member_name))%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20cls</marimo-cell-code>
</marimo-island>
</section>
<section id="designing-the-wiggt-class" class="level3">
<h3 class="anchored" data-anchor-id="designing-the-wiggt-class">Designing the <code>WigGT</code> Class</h3>
<p>Here are the main ideas behind the design:</p>
<ol type="1">
<li>Original tables are stored in <code>self._tables</code>, accessible via the <code>.tables</code> property.</li>
<li>Interactive versions with embedded marimo widgets are stored in <code>self._wtables</code>. These aren’t exposed directly — interaction is done through the marimo UI.</li>
<li>The rendering method follows <a href="https://docs.marimo.io/guides/integrating_with_marimo/displaying_objects/">marimo’s convention</a>, trying <code>_display_</code>, then <code>_repr_html_</code>, and finally <code>_mime_</code>, in that order.</li>
</ol>
<p>With this setup, <code>WigGT</code> tracks the full transformation pipeline and allows easy replay of each step through a slider.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@lazify</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> WigGT:</span>
<span id="cb3-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, widget, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb3-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> args</span>
<span id="cb3-5">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> widget</span>
<span id="cb3-6">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kwargs</span>
<span id="cb3-7">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_to_init()</span>
<span id="cb3-8"></span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@property</span></span>
<span id="cb3-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tables(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[GT]:</span>
<span id="cb3-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return a new list</span></span>
<span id="cb3-12"></span>
<span id="cb3-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _widgetify(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, obj: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb3-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> obj.tab_source_note(html(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget))</span>
<span id="cb3-15"></span>
<span id="cb3-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> collect(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb3-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect:</span>
<span id="cb3-18">            new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't use self._wtables[0]</span></span>
<span id="cb3-19">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline:</span>
<span id="cb3-20">                new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>copy.copy(new_obj))</span>
<span id="cb3-21">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(new_obj)</span>
<span id="cb3-22">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(new_obj))</span>
<span id="cb3-23">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb3-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb3-25"></span>
<span id="cb3-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _repr_html_(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb3-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb3-28">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget.value]</span>
<span id="cb3-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">IndexError</span>:</span>
<span id="cb3-30">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-31"></span>
<span id="cb3-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb3-33">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb3-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb3-35">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb3-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb3-37">            render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb3-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-39">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>(</span>
<span id="cb3-40">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span></span>
<span id="cb3-41">            )</span>
<span id="cb3-42">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(obj, render_method)()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remember to invoke</span></span>
<span id="cb3-43"></span>
<span id="cb3-44">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_to_init(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb3-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pipeline"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-46">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-48">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.clear()</span>
<span id="cb3-49"></span>
<span id="cb3-50">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_tables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-51">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-53">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.clear()</span>
<span id="cb3-54"></span>
<span id="cb3-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_wtables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb3-56">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-58">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.clear()</span>
<span id="cb3-59"></span>
<span id="cb3-60">        obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._make_gt()</span>
<span id="cb3-61">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(obj)</span>
<span id="cb3-62">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(obj))</span>
<span id="cb3-63">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb3-64"></span>
<span id="cb3-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _make_gt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb3-66">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs)</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">%40lazify%0Aclass%20WigGT%3A%0A%20%20%20%20def%20__init__(self%2C%20*args%2C%20widget%2C%20**kwargs)%3A%0A%20%20%20%20%20%20%20%20self._args%20%3D%20args%0A%20%20%20%20%20%20%20%20self._widget%20%3D%20widget%0A%20%20%20%20%20%20%20%20self._kwargs%20%3D%20kwargs%0A%20%20%20%20%20%20%20%20self.set_to_init()%0A%0A%20%20%20%20%40property%0A%20%20%20%20def%20tables(self)%20-%3E%20list%5BGT%5D%3A%0A%20%20%20%20%20%20%20%20return%20list(self._tables)%20%20%23%20return%20a%20new%20list%0A%0A%20%20%20%20def%20_widgetify(self%2C%20obj%3A%20GT)%20-%3E%20GT%3A%0A%20%20%20%20%20%20%20%20return%20obj.tab_source_note(html(self._widget))%0A%0A%20%20%20%20def%20collect(self)%20-%3E%20Self%3A%0A%20%20%20%20%20%20%20%20if%20not%20self._is_collect%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20new_obj%20%3D%20self._tables%5B0%5D%20%20%23%20don't%20use%20self._wtables%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20f%20in%20self._pipeline%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20new_obj%20%3D%20f(self%3Dcopy.copy(new_obj))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self._tables.append(new_obj)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self._wtables.append(self._widgetify(new_obj))%0A%20%20%20%20%20%20%20%20%20%20%20%20self._is_collect%20%3D%20True%0A%20%20%20%20%20%20%20%20return%20self%0A%0A%20%20%20%20def%20_repr_html_(self)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20obj%20%3D%20self._wtables%5Bself._widget.value%5D%0A%20%20%20%20%20%20%20%20except%20IndexError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20obj%20%3D%20self._wtables%5B-1%5D%0A%0A%20%20%20%20%20%20%20%20if%20hasattr(obj%2C%20%22_display_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_display_%22%0A%20%20%20%20%20%20%20%20elif%20hasattr(obj%2C%20%22_repr_html_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_repr_html_%22%0A%20%20%20%20%20%20%20%20elif%20hasattr(obj%2C%20%22_mime_%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20render_method%20%3D%20%22_mime_%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20AttributeError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22The%20object%20does%20not%20have%20a%20valid%20render%20method.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20getattr(obj%2C%20render_method)()%20%20%23%20remember%20to%20invoke%0A%0A%20%20%20%20def%20set_to_init(self)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_pipeline%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline%3A%20list%5Bcallable%5D%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._pipeline.clear()%0A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_tables%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._tables%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._tables.clear()%0A%0A%20%20%20%20%20%20%20%20if%20not%20getattr(self%2C%20%22_wtables%22%2C%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._wtables%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self._wtables.clear()%0A%0A%20%20%20%20%20%20%20%20obj%20%3D%20self._make_gt()%0A%20%20%20%20%20%20%20%20self._tables.append(obj)%0A%20%20%20%20%20%20%20%20self._wtables.append(self._widgetify(obj))%0A%20%20%20%20%20%20%20%20self._is_collect%20%3D%20False%0A%0A%20%20%20%20def%20_make_gt(self)%20-%3E%20GT%3A%0A%20%20%20%20%20%20%20%20return%20GT(*self._args%2C%20**self._kwargs)</marimo-cell-code>
</marimo-island>
</section>
</section>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<p>Now we’re ready to <strong>time travel</strong> through the table-building process!</p>
<section id="create-a-slider-to-navigate-table-states" class="level3">
<h3 class="anchored" data-anchor-id="create-a-slider-to-navigate-table-states">Create a Slider to Navigate Table States</h3>
<p>We begin by creating a slider that represents each step in the build pipeline.</p>
<ul>
<li><code>Step 0</code> is the original <code>GT</code> object.</li>
<li><code>Step 1</code> applies the first method call, and so on.</li>
</ul>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Slider Range Limitation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Because all method calls are deferred, we can’t determine the total number of steps ahead of time. This means the developer needs to manually specify a reasonable range for the slider. If the selected index exceeds the available range, the last table will be shown by default.</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">time_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Step"</span>)</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">time_widget%20%3D%20mo.ui.slider(start%3D0%2C%20stop%3D6%2C%20step%3D1%2C%20value%3D0%2C%20label%3D%22Step%22)</marimo-cell-code>
</marimo-island>
</section>
<section id="build-the-lazy-table-sequence" class="level3">
<h3 class="anchored" data-anchor-id="build-the-lazy-table-sequence">Build the Lazy Table Sequence</h3>
<p>Next, we use an example from the <a href="https://posit-dev.github.io/great-tables/examples/">Great Tables documentation</a> to create a lazy <code>WigGT</code> object. This will internally store each method call but won’t execute them just yet.</p>
<p>You’ll see a slider widget embedded in the table’s source note — but it won’t be functional until we finalize the process.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">lazy_wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb5-2">    WigGT(airquality.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).assign(Year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973</span>), widget<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>time_widget)</span>
<span id="cb5-3">    .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-4">    .tab_header(</span>
<span id="cb5-5">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York Air Quality Measurements"</span>,</span>
<span id="cb5-6">        subtitle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Daily measurements in New York City (May 1-10, 1973)"</span>,</span>
<span id="cb5-7">    )</span>
<span id="cb5-8">    .tab_spanner(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb5-9">    .tab_spanner(</span>
<span id="cb5-10">        label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Measurement"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar_R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp"</span>]</span>
<span id="cb5-11">    )</span>
<span id="cb5-12">    .cols_move_to_start(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb5-13">    .cols_label(</span>
<span id="cb5-14">        Ozone<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone,&lt;br&gt;ppbV"</span>),</span>
<span id="cb5-15">        Solar_R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar R.,&lt;br&gt;cal/m&lt;sup&gt;2&lt;/sup&gt;"</span>),</span>
<span id="cb5-16">        Wind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind,&lt;br&gt;mph"</span>),</span>
<span id="cb5-17">        Temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp,&lt;br&gt;&amp;deg;F"</span>),</span>
<span id="cb5-18">    )</span>
<span id="cb5-19">)</span>
<span id="cb5-20">lazy_wig_gt</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="PKri" data-reactive="true">
            <marimo-cell-output>
            <div id="hrbouzumfw" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#hrbouzumfw table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#hrbouzumfw thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#hrbouzumfw p { margin: 0; padding: 0; }
 #hrbouzumfw .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #hrbouzumfw .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #hrbouzumfw .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #hrbouzumfw .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #hrbouzumfw .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #hrbouzumfw .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #hrbouzumfw .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #hrbouzumfw .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #hrbouzumfw .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #hrbouzumfw .gt_column_spanner_outer:first-child { padding-left: 0; }
 #hrbouzumfw .gt_column_spanner_outer:last-child { padding-right: 0; }
 #hrbouzumfw .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #hrbouzumfw .gt_spanner_row { border-bottom-style: hidden; }
 #hrbouzumfw .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #hrbouzumfw .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; }
 #hrbouzumfw .gt_from_md> :first-child { margin-top: 0; }
 #hrbouzumfw .gt_from_md> :last-child { margin-bottom: 0; }
 #hrbouzumfw .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; }
 #hrbouzumfw .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; }
 #hrbouzumfw .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #hrbouzumfw .gt_row_group_first td { border-top-width: 2px; }
 #hrbouzumfw .gt_row_group_first th { border-top-width: 2px; }
 #hrbouzumfw .gt_striped { color: #333333; background-color: #F4F4F4; }
 #hrbouzumfw .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #hrbouzumfw .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #hrbouzumfw .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #hrbouzumfw .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #hrbouzumfw .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #hrbouzumfw .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #hrbouzumfw .gt_left { text-align: left; }
 #hrbouzumfw .gt_center { text-align: center; }
 #hrbouzumfw .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #hrbouzumfw .gt_font_normal { font-weight: normal; }
 #hrbouzumfw .gt_font_bold { font-weight: bold; }
 #hrbouzumfw .gt_font_italic { font-style: italic; }
 #hrbouzumfw .gt_super { font-size: 65%; }
 #hrbouzumfw .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #hrbouzumfw .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Ozone">Ozone</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Solar_R">Solar_R</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Wind">Wind</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Temp">Temp</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Month">Month</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Day">Day</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Year">Year</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_right">41.0</td>
    <td class="gt_row gt_right">190.0</td>
    <td class="gt_row gt_right">7.4</td>
    <td class="gt_row gt_right">67</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">1</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">36.0</td>
    <td class="gt_row gt_right">118.0</td>
    <td class="gt_row gt_right">8.0</td>
    <td class="gt_row gt_right">72</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">2</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">12.0</td>
    <td class="gt_row gt_right">149.0</td>
    <td class="gt_row gt_right">12.6</td>
    <td class="gt_row gt_right">74</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">3</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">18.0</td>
    <td class="gt_row gt_right">313.0</td>
    <td class="gt_row gt_right">11.5</td>
    <td class="gt_row gt_right">62</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">4</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">14.3</td>
    <td class="gt_row gt_right">56</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">28.0</td>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">14.9</td>
    <td class="gt_row gt_right">66</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">6</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">23.0</td>
    <td class="gt_row gt_right">299.0</td>
    <td class="gt_row gt_right">8.6</td>
    <td class="gt_row gt_right">65</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">7</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">19.0</td>
    <td class="gt_row gt_right">99.0</td>
    <td class="gt_row gt_right">13.8</td>
    <td class="gt_row gt_right">59</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">8</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">8.0</td>
    <td class="gt_row gt_right">19.0</td>
    <td class="gt_row gt_right">20.1</td>
    <td class="gt_row gt_right">61</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">9</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">194.0</td>
    <td class="gt_row gt_right">8.6</td>
    <td class="gt_row gt_right">69</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">10</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
</tbody>
  <tfoot class="gt_sourcenotes">
  <tr>
    <td class="gt_sourcenote" colspan="7"><marimo-ui-element object-id="lEQa-0" random-id="b628d0c5-1e06-cbc4-bc87-1e4979732c60"><marimo-slider data-initial-value="0" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Step</span></span>&quot;" data-start="0" data-stop="6" data-step="1" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element></td>
  </tr>
</tfoot>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">lazy_wig_gt%20%3D%20(%0A%20%20%20%20WigGT(airquality.head(10).assign(Year%3D1973)%2C%20widget%3Dtime_widget)%0A%20%20%20%20.opt_stylize(color%3D%22pink%22%2C%20style%3D2)%0A%20%20%20%20.tab_header(%0A%20%20%20%20%20%20%20%20title%3D%22New%20York%20Air%20Quality%20Measurements%22%2C%0A%20%20%20%20%20%20%20%20subtitle%3D%22Daily%20measurements%20in%20New%20York%20City%20(May%201-10%2C%201973)%22%2C%0A%20%20%20%20)%0A%20%20%20%20.tab_spanner(label%3D%22Time%22%2C%20columns%3D%5B%22Year%22%2C%20%22Month%22%2C%20%22Day%22%5D)%0A%20%20%20%20.tab_spanner(%0A%20%20%20%20%20%20%20%20label%3D%22Measurement%22%2C%20columns%3D%5B%22Ozone%22%2C%20%22Solar_R%22%2C%20%22Wind%22%2C%20%22Temp%22%5D%0A%20%20%20%20)%0A%20%20%20%20.cols_move_to_start(columns%3D%5B%22Year%22%2C%20%22Month%22%2C%20%22Day%22%5D)%0A%20%20%20%20.cols_label(%0A%20%20%20%20%20%20%20%20Ozone%3Dhtml(%22Ozone%2C%3Cbr%3EppbV%22)%2C%0A%20%20%20%20%20%20%20%20Solar_R%3Dhtml(%22Solar%20R.%2C%3Cbr%3Ecal%2Fm%3Csup%3E2%3C%2Fsup%3E%22)%2C%0A%20%20%20%20%20%20%20%20Wind%3Dhtml(%22Wind%2C%3Cbr%3Emph%22)%2C%0A%20%20%20%20%20%20%20%20Temp%3Dhtml(%22Temp%2C%3Cbr%3E%26deg%3BF%22)%2C%0A%20%20%20%20)%0A)%0Alazy_wig_gt</marimo-cell-code>
        </marimo-island>
</section>
<section id="trigger-execution-with-.collect" class="level3">
<h3 class="anchored" data-anchor-id="trigger-execution-with-.collect">Trigger Execution with <code>.collect()</code></h3>
<p>To make the slider work, we call <code>.collect()</code> on the <code>WigGT</code> instance. This executes the stored methods and generates a list of tables, one for each step.</p>
<p>Once collected, you can interactively slide through each version of the table!</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lazy_wig_gt.collect()</span>
<span id="cb6-2">wig_gt</span></code></pre></div></div>
<marimo-island data-app-id="main" data-cell-id="Xref" data-reactive="true">
            <marimo-cell-output>
            <div id="jagvgrkioo" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#jagvgrkioo table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#jagvgrkioo thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#jagvgrkioo p { margin: 0; padding: 0; }
 #jagvgrkioo .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #jagvgrkioo .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #jagvgrkioo .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #jagvgrkioo .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #jagvgrkioo .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #jagvgrkioo .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #jagvgrkioo .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #jagvgrkioo .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #jagvgrkioo .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #jagvgrkioo .gt_column_spanner_outer:first-child { padding-left: 0; }
 #jagvgrkioo .gt_column_spanner_outer:last-child { padding-right: 0; }
 #jagvgrkioo .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #jagvgrkioo .gt_spanner_row { border-bottom-style: hidden; }
 #jagvgrkioo .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #jagvgrkioo .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; }
 #jagvgrkioo .gt_from_md> :first-child { margin-top: 0; }
 #jagvgrkioo .gt_from_md> :last-child { margin-bottom: 0; }
 #jagvgrkioo .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; }
 #jagvgrkioo .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; }
 #jagvgrkioo .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #jagvgrkioo .gt_row_group_first td { border-top-width: 2px; }
 #jagvgrkioo .gt_row_group_first th { border-top-width: 2px; }
 #jagvgrkioo .gt_striped { color: #333333; background-color: #F4F4F4; }
 #jagvgrkioo .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; }
 #jagvgrkioo .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #jagvgrkioo .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #jagvgrkioo .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #jagvgrkioo .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #jagvgrkioo .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #jagvgrkioo .gt_left { text-align: left; }
 #jagvgrkioo .gt_center { text-align: center; }
 #jagvgrkioo .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #jagvgrkioo .gt_font_normal { font-weight: normal; }
 #jagvgrkioo .gt_font_bold { font-weight: bold; }
 #jagvgrkioo .gt_font_italic { font-style: italic; }
 #jagvgrkioo .gt_super { font-size: 65%; }
 #jagvgrkioo .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #jagvgrkioo .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Ozone">Ozone</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Solar_R">Solar_R</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Wind">Wind</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Temp">Temp</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Month">Month</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Day">Day</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="Year">Year</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_right">41.0</td>
    <td class="gt_row gt_right">190.0</td>
    <td class="gt_row gt_right">7.4</td>
    <td class="gt_row gt_right">67</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">1</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">36.0</td>
    <td class="gt_row gt_right">118.0</td>
    <td class="gt_row gt_right">8.0</td>
    <td class="gt_row gt_right">72</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">2</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">12.0</td>
    <td class="gt_row gt_right">149.0</td>
    <td class="gt_row gt_right">12.6</td>
    <td class="gt_row gt_right">74</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">3</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">18.0</td>
    <td class="gt_row gt_right">313.0</td>
    <td class="gt_row gt_right">11.5</td>
    <td class="gt_row gt_right">62</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">4</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">14.3</td>
    <td class="gt_row gt_right">56</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">28.0</td>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">14.9</td>
    <td class="gt_row gt_right">66</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">6</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">23.0</td>
    <td class="gt_row gt_right">299.0</td>
    <td class="gt_row gt_right">8.6</td>
    <td class="gt_row gt_right">65</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">7</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">19.0</td>
    <td class="gt_row gt_right">99.0</td>
    <td class="gt_row gt_right">13.8</td>
    <td class="gt_row gt_right">59</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">8</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">8.0</td>
    <td class="gt_row gt_right">19.0</td>
    <td class="gt_row gt_right">20.1</td>
    <td class="gt_row gt_right">61</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">9</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
  <tr>
    <td class="gt_row gt_right"><na></na></td>
    <td class="gt_row gt_right">194.0</td>
    <td class="gt_row gt_right">8.6</td>
    <td class="gt_row gt_right">69</td>
    <td class="gt_row gt_right">5</td>
    <td class="gt_row gt_right">10</td>
    <td class="gt_row gt_right">1973</td>
  </tr>
</tbody>
  <tfoot class="gt_sourcenotes">
  <tr>
    <td class="gt_sourcenote" colspan="7"><marimo-ui-element object-id="lEQa-0" random-id="b628d0c5-1e06-cbc4-bc87-1e4979732c60"><marimo-slider data-initial-value="0" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Step</span></span>&quot;" data-start="0" data-stop="6" data-step="1" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element></td>
  </tr>
</tfoot>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">wig_gt%20%3D%20lazy_wig_gt.collect()%0Awig_gt</marimo-cell-code>
        </marimo-island>
</section>
<section id="access-all-built-tables" class="level3">
<h3 class="anchored" data-anchor-id="access-all-built-tables">Access All Built Tables</h3>
<p>Finally, you can retrieve all constructed tables using the <code>.tables</code> property, which returns a list of <code>GT</code> objects.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">wig_gt.tables</span></code></pre></div></div>
</section>
<section id="full-marimo-code" class="level3">
<h3 class="anchored" data-anchor-id="full-marimo-code">Full marimo code</h3>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_FfAE5bVJ9P6DWDDy4Gng2h/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb8-2"></span>
<span id="cb8-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.8"</span></span>
<span id="cb8-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb8-5"></span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb8-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb8-10"></span>
<span id="cb8-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb8-12"></span>
<span id="cb8-13"></span>
<span id="cb8-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb8-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> copy</span>
<span id="cb8-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> inspect</span>
<span id="cb8-18">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> functools <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> wraps</span>
<span id="cb8-19">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Callable, Self</span>
<span id="cb8-20"></span>
<span id="cb8-21">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb8-22">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb8-23">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> airquality</span>
<span id="cb8-24"></span>
<span id="cb8-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Callable, GT, Self, airquality, copy, html, inspect, wraps</span>
<span id="cb8-26"></span>
<span id="cb8-27"></span>
<span id="cb8-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(Callable, GT, Self, copy, html, inspect, wraps):</span>
<span id="cb8-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_allowed_member_names() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]:</span>
<span id="cb8-31">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb8-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        Manually constructing the list —</span></span>
<span id="cb8-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        it would be great if Great Tables exposed the available method names.</span></span>
<span id="cb8-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb8-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [</span>
<span id="cb8-36">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt"</span>,</span>
<span id="cb8-37">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_number"</span>,</span>
<span id="cb8-38">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_integer"</span>,</span>
<span id="cb8-39">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_percent"</span>,</span>
<span id="cb8-40">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_scientific"</span>,</span>
<span id="cb8-41">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_currency"</span>,</span>
<span id="cb8-42">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_bytes"</span>,</span>
<span id="cb8-43">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_roman"</span>,</span>
<span id="cb8-44">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_date"</span>,</span>
<span id="cb8-45">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_time"</span>,</span>
<span id="cb8-46">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_datetime"</span>,</span>
<span id="cb8-47">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_markdown"</span>,</span>
<span id="cb8-48">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_image"</span>,</span>
<span id="cb8-49">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_icon"</span>,</span>
<span id="cb8-50">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_flag"</span>,</span>
<span id="cb8-51">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_units"</span>,</span>
<span id="cb8-52">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fmt_nanoplot"</span>,</span>
<span id="cb8-53">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_color"</span>,</span>
<span id="cb8-54">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub_missing"</span>,</span>
<span id="cb8-55">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sub_zero"</span>,</span>
<span id="cb8-56">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_stylize"</span>,</span>
<span id="cb8-57">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_align_table_header"</span>,</span>
<span id="cb8-58">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_all_caps"</span>,</span>
<span id="cb8-59">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_footnote_marks"</span>,</span>
<span id="cb8-60">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_row_striping"</span>,</span>
<span id="cb8-61">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_vertical_padding"</span>,</span>
<span id="cb8-62">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_horizontal_padding"</span>,</span>
<span id="cb8-63">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_table_outline"</span>,</span>
<span id="cb8-64">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"opt_table_font"</span>,</span>
<span id="cb8-65">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_align"</span>,</span>
<span id="cb8-66">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_width"</span>,</span>
<span id="cb8-67">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_label"</span>,</span>
<span id="cb8-68">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move"</span>,</span>
<span id="cb8-69">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move_to_start"</span>,</span>
<span id="cb8-70">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_move_to_end"</span>,</span>
<span id="cb8-71">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_hide"</span>,</span>
<span id="cb8-72">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cols_unhide"</span>,</span>
<span id="cb8-73">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_header"</span>,</span>
<span id="cb8-74">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_source_note"</span>,</span>
<span id="cb8-75">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_spanner"</span>,</span>
<span id="cb8-76">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_stubhead"</span>,</span>
<span id="cb8-77">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_style"</span>,</span>
<span id="cb8-78">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_options"</span>,</span>
<span id="cb8-79">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row_group_order"</span>,</span>
<span id="cb8-80">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tab_stub"</span>,</span>
<span id="cb8-81">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with_id"</span>,</span>
<span id="cb8-82">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with_locale"</span>,</span>
<span id="cb8-83">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"save"</span>,</span>
<span id="cb8-84">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"show"</span>,</span>
<span id="cb8-85">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"as_raw_html"</span>,</span>
<span id="cb8-86">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"write_raw_html"</span>,</span>
<span id="cb8-87">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"as_latex"</span>,</span>
<span id="cb8-88">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pipe"</span>,</span>
<span id="cb8-89">        ]</span>
<span id="cb8-90"></span>
<span id="cb8-91">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> lazify(cls: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-92">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_to_pipeline(func: Callable[..., GT]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>:</span>
<span id="cb8-93">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@wraps</span>(func)</span>
<span id="cb8-94">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> wrapper(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb8-95">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> inner(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb8-96">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> func(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs)</span>
<span id="cb8-97"></span>
<span id="cb8-98">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># container for storing callable objects</span></span>
<span id="cb8-99">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.append(inner)</span>
<span id="cb8-100">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb8-101"></span>
<span id="cb8-102">            wrapper.__signature__ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inspect.signature(func)</span>
<span id="cb8-103">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> wrapper</span>
<span id="cb8-104"></span>
<span id="cb8-105">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> member_name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> get_allowed_member_names():</span>
<span id="cb8-106">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">setattr</span>(</span>
<span id="cb8-107">                cls, member_name, add_to_pipeline(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(GT, member_name))</span>
<span id="cb8-108">            )</span>
<span id="cb8-109">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cls</span>
<span id="cb8-110"></span>
<span id="cb8-111">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@lazify</span></span>
<span id="cb8-112">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> WigGT:</span>
<span id="cb8-113">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>args, widget, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kwargs):</span>
<span id="cb8-114">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> args</span>
<span id="cb8-115">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> widget</span>
<span id="cb8-116">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> kwargs</span>
<span id="cb8-117">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_to_init()</span>
<span id="cb8-118"></span>
<span id="cb8-119">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@property</span></span>
<span id="cb8-120">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tables(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[GT]:</span>
<span id="cb8-121">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return a new list</span></span>
<span id="cb8-122"></span>
<span id="cb8-123">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _widgetify(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, obj: GT) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-124">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> obj.tab_source_note(html(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget))</span>
<span id="cb8-125"></span>
<span id="cb8-126">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> collect(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Self:</span>
<span id="cb8-127">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect:</span>
<span id="cb8-128">                new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't use self._wtables[0]</span></span>
<span id="cb8-129">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline:</span>
<span id="cb8-130">                    new_obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>copy.copy(new_obj))</span>
<span id="cb8-131">                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(new_obj)</span>
<span id="cb8-132">                    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(new_obj))</span>
<span id="cb8-133">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb8-134">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span></span>
<span id="cb8-135"></span>
<span id="cb8-136">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _repr_html_(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb8-137">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb8-138">                obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widget.value]</span>
<span id="cb8-139">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">IndexError</span>:</span>
<span id="cb8-140">                obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb8-141"></span>
<span id="cb8-142">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span>):</span>
<span id="cb8-143">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_display_"</span></span>
<span id="cb8-144">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span>):</span>
<span id="cb8-145">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_repr_html_"</span></span>
<span id="cb8-146">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(obj, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span>):</span>
<span id="cb8-147">                render_method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_mime_"</span></span>
<span id="cb8-148">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-149">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AttributeError</span>(</span>
<span id="cb8-150">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The object does not have a valid render method."</span></span>
<span id="cb8-151">                )</span>
<span id="cb8-152">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(obj, render_method)()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remember to invoke</span></span>
<span id="cb8-153"></span>
<span id="cb8-154">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> set_to_init(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-155">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_pipeline"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-156">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">callable</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-157">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-158">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._pipeline.clear()</span>
<span id="cb8-159"></span>
<span id="cb8-160">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_tables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-161">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-162">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-163">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.clear()</span>
<span id="cb8-164"></span>
<span id="cb8-165">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">getattr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_wtables"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb8-166">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-167">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-168">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.clear()</span>
<span id="cb8-169"></span>
<span id="cb8-170">            obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._make_gt()</span>
<span id="cb8-171">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._tables.append(obj)</span>
<span id="cb8-172">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._wtables.append(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._widgetify(obj))</span>
<span id="cb8-173">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._is_collect <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb8-174"></span>
<span id="cb8-175">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _make_gt(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> GT:</span>
<span id="cb8-176">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._args, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._kwargs)</span>
<span id="cb8-177"></span>
<span id="cb8-178">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (WigGT,)</span>
<span id="cb8-179"></span>
<span id="cb8-180"></span>
<span id="cb8-181"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-182"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo):</span>
<span id="cb8-183">    time_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Step"</span>)</span>
<span id="cb8-184">    time_widget</span>
<span id="cb8-185">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (time_widget,)</span>
<span id="cb8-186"></span>
<span id="cb8-187"></span>
<span id="cb8-188"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-189"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(WigGT, airquality, html, time_widget):</span>
<span id="cb8-190">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The lazy_wig_gt object is not interactive until collect() is called.</span></span>
<span id="cb8-191">    lazy_wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb8-192">        WigGT(airquality.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>).assign(Year<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973</span>), widget<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>time_widget)</span>
<span id="cb8-193">        .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-194">        .tab_header(</span>
<span id="cb8-195">            title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New York Air Quality Measurements"</span>,</span>
<span id="cb8-196">            subtitle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Daily measurements in New York City (May 1-10, 1973)"</span>,</span>
<span id="cb8-197">        )</span>
<span id="cb8-198">        .tab_spanner(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Time"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb8-199">        .tab_spanner(</span>
<span id="cb8-200">            label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Measurement"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar_R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp"</span>]</span>
<span id="cb8-201">        )</span>
<span id="cb8-202">        .cols_move_to_start(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day"</span>])</span>
<span id="cb8-203">        .cols_label(</span>
<span id="cb8-204">            Ozone<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone,&lt;br&gt;ppbV"</span>),</span>
<span id="cb8-205">            Solar_R<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar R.,&lt;br&gt;cal/m&lt;sup&gt;2&lt;/sup&gt;"</span>),</span>
<span id="cb8-206">            Wind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind,&lt;br&gt;mph"</span>),</span>
<span id="cb8-207">            Temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>html(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp,&lt;br&gt;&amp;deg;F"</span>),</span>
<span id="cb8-208">        )</span>
<span id="cb8-209">    )</span>
<span id="cb8-210">    lazy_wig_gt</span>
<span id="cb8-211">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (lazy_wig_gt,)</span>
<span id="cb8-212"></span>
<span id="cb8-213"></span>
<span id="cb8-214"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-215"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(lazy_wig_gt):</span>
<span id="cb8-216">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The wig_gt object is now interactive</span></span>
<span id="cb8-217">    wig_gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lazy_wig_gt.collect()</span>
<span id="cb8-218">    wig_gt</span>
<span id="cb8-219">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (wig_gt,)</span>
<span id="cb8-220"></span>
<span id="cb8-221"></span>
<span id="cb8-222"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb8-223"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(wig_gt):</span>
<span id="cb8-224">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># retrieve all `gt` tables</span></span>
<span id="cb8-225">    wig_gt.tables</span>
<span id="cb8-226">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb8-227"></span>
<span id="cb8-228"></span>
<span id="cb8-229"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb8-230">    app.run()</span></code></pre></div></div>
</section>
</section>
<section id="in-closing" class="level2">
<h2 class="anchored" data-anchor-id="in-closing">In Closing</h2>
<p>marimo has turned out to be far more powerful than I expected — I’ve had a lot of fun experimenting with it.</p>
<p>I highly recommend checking out their <a href="https://www.youtube.com/@marimo-team">YouTube channel</a>, which is full of high-quality content across many Python topics.</p>
<p>This idea was heavily inspired by their videos — all credit goes to the amazing marimo team!</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/marimo-gt-time-machine/20250626.html</guid>
  <pubDate>Thu, 26 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-gt-time-machine/marimo_gt_time_machine.png" medium="image" type="image/png" height="213" width="144"/>
</item>
<item>
  <title>Interactive Django Deployment Checklist with marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-django-deployment-checklist/20250625.html</link>
  <description><![CDATA[ 






<p>I love Django—it covers pretty much everything I need in a web environment. However, when it comes time to deploy a project to production, there are always a bunch of pre-deployment checks, and I can never seem to remember them all. I find myself constantly revisiting the official Django deployment checklist page.</p>
<p>Today I realized I don’t need all the detailed information every time—just a simple reminder list is enough. So, I built an interactive Django deployment checklist using Great Tables in marimo and hosted it on <a href="https://marimo.app/?slug=loep4h">marimo.app</a>. Now I can interact with it whenever I need a quick double-check.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-django-deployment-checklist/marimo_django_deployment_checklist.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Django deployment checklist"></p>
</figure>
</div>
<section id="marimo" class="level3">
<h3 class="anchored" data-anchor-id="marimo">marimo</h3>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<ol type="1">
<li>I asked AI to generate a checklist and wrapped it in a Polars <code>DataFrame</code> called <code>df</code>.</li>
<li>I created 10 <a href="https://docs.marimo.io/api/inputs/switch/">switch widgets</a> and stacked them into an <a href="https://docs.marimo.io/api/inputs/array/">array widget</a> named <code>status_widgets</code> to represent the status of each checklist item.</li>
<li>I extracted the HTML representation of each widget via its <code>_repr_html_()</code> method and inserted it as a new <code>"Status"</code> column in <code>df</code>, which I then wrapped in a Great Tables <a href="https://posit-dev.github.io/great-tables/reference/GT.html#great_tables.GT">GT</a> object.</li>
<li>I added two source notes using <a href="https://posit-dev.github.io/great-tables/reference/GT.tab_source_note.html#great_tables.GT.tab_source_note">GT.tab_source_note()</a>—one to display progress, and another for a visual progress bar.</li>
<li>Finally, I gave the table a nice header with <a href="https://posit-dev.github.io/great-tables/reference/GT.tab_header.html#great_tables.GT.tab_header">GT.tab_header()</a> and applied some styling using <a href="https://posit-dev.github.io/great-tables/reference/GT.opt_stylize.html#great_tables.GT.opt_stylize">GT.opt_stylize()</a>.</li>
</ol>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20html%2C%20md</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">tasks%20%3D%20%5B%0A%20%20%20%20%22Set%20DEBUG%20%3D%20False%22%2C%0A%20%20%20%20%22Configure%20ALLOWED_HOSTS%22%2C%0A%20%20%20%20%22Set%20up%20a%20secret%20key%22%2C%0A%20%20%20%20%22Collect%20static%20files%22%2C%0A%20%20%20%20%22Apply%20database%20migrations%22%2C%0A%20%20%20%20%22Set%20up%20gunicorn%20or%20uWSGI%22%2C%0A%20%20%20%20%22Configure%20reverse%20proxy%20(e.g.%2C%20Nginx)%22%2C%0A%20%20%20%20%22Secure%20the%20database%22%2C%0A%20%20%20%20%22Set%20up%20HTTPS%20(SSL)%22%2C%0A%20%20%20%20%22Configure%20logging%20%26%20monitoring%22%2C%0A%5D%0A%0Anotes%20%3D%20%5B%0A%20%20%20%20%22Never%20deploy%20with%20DEBUG%20%3D%20True%20%E2%9A%A0%EF%B8%8F%22%2C%0A%20%20%20%20%22Include%20your%20domain(s)%20or%20IP%20address%20%F0%9F%8C%90%22%2C%0A%20%20%20%20%22Use%20a%20strong%2C%20secure%20key%20from%20an%20environment%20variable%20%F0%9F%94%90%22%2C%0A%20%20%20%20%22Run%20%60python%20manage.py%20collectstatic%60%20%F0%9F%93%A6%22%2C%0A%20%20%20%20%22Run%20%60python%20manage.py%20migrate%60%20%F0%9F%97%83%EF%B8%8F%22%2C%0A%20%20%20%20%22Use%20as%20a%20WSGI%20server%20in%20production%20%F0%9F%94%84%22%2C%0A%20%20%20%20%22Serve%20static%2Fmedia%20files%20and%20forward%20to%20WSGI%20server%20%F0%9F%A7%AD%22%2C%0A%20%20%20%20%22Use%20strong%20credentials%2C%20disable%20remote%20root%20login%20%F0%9F%9B%A1%EF%B8%8F%22%2C%0A%20%20%20%20%22Use%20Let's%20Encrypt%20or%20your%20own%20certificate%20%F0%9F%94%92%22%2C%0A%20%20%20%20%22Track%20errors%20and%20app%20performance%20%F0%9F%93%8A%22%2C%0A%5D%0A%0An_row%20%3D%20len(tasks)%0Astatus%20%3D%20%5B%22%E2%98%90%22%5D%20*%20n_row%0Adata%20%3D%20%7B%22Status%22%3A%20status%2C%20%22Task%22%3A%20tasks%2C%20%22Notes%22%3A%20notes%7D%0A%0Adf%20%3D%20pl.DataFrame(data)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">status_widget%20%3D%20mo.ui.switch()%0Astatus_widgets%20%3D%20mo.ui.array(%5Bstatus_widget%5D%20*%20n_row)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">def%20create_bar(%0A%20%20%20%20x%3A%20float%2C%0A%20%20%20%20max_width%3A%20int%2C%0A%20%20%20%20height%3A%20int%2C%0A%20%20%20%20background_color1%3A%20str%2C%0A%20%20%20%20background_color2%3A%20str%2C%0A)%20-%3E%20str%3A%0A%20%20%20%20width%20%3D%20round(max_width%20*%20x%2C%202)%0A%20%20%20%20px_width%20%3D%20f%22%7Bwidth%7Dpx%22%0A%20%20%20%20return%20f%22%22%22%5C%0A%20%20%20%20%3Cdiv%20style%3D%22width%3A%20%7Bmax_width%7Dpx%3B%20background-color%3A%20%7Bbackground_color1%7D%3B%22%3E%5C%0A%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22height%3A%7Bheight%7Dpx%3Bwidth%3A%7Bpx_width%7D%3Bbackground-color%3A%7Bbackground_color2%7D%3B%22%3E%3C%2Fdiv%3E%5C%0A%20%20%20%20%3C%2Fdiv%3E%5C%0A%20%20%20%20%22%22%22</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="lEQa" data-reactive="true">
            <marimo-cell-output>
            <div id="paidbmvwxp" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#paidbmvwxp table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#paidbmvwxp thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#paidbmvwxp p { margin: 0; padding: 0; }
 #paidbmvwxp .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #D5D5D5; border-right-style: solid; border-right-width: 3px; border-right-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: #D5D5D5; border-left-style: solid; border-left-width: 3px; border-left-color: #D5D5D5; }
 #paidbmvwxp .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #paidbmvwxp .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #paidbmvwxp .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #paidbmvwxp .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #paidbmvwxp .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #paidbmvwxp .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #paidbmvwxp .gt_col_heading { color: #FFFFFF; background-color: #01837B; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #paidbmvwxp .gt_column_spanner_outer { color: #FFFFFF; background-color: #01837B; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #paidbmvwxp .gt_column_spanner_outer:first-child { padding-left: 0; }
 #paidbmvwxp .gt_column_spanner_outer:last-child { padding-right: 0; }
 #paidbmvwxp .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #paidbmvwxp .gt_spanner_row { border-bottom-style: hidden; }
 #paidbmvwxp .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #paidbmvwxp .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; vertical-align: middle; }
 #paidbmvwxp .gt_from_md> :first-child { margin-top: 0; }
 #paidbmvwxp .gt_from_md> :last-child { margin-bottom: 0; }
 #paidbmvwxp .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: dashed; border-top-width: 1px; border-top-color: #D5D5D5; border-left-style: dashed; border-left-width: 1px; border-left-color: #D5D5D5; border-right-style: dashed; border-right-width: 1px; border-right-color: #D5D5D5; vertical-align: middle; overflow-x: hidden; }
 #paidbmvwxp .gt_stub { color: #333333; background-color: #929292; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: dashed; border-right-width: 2px; border-right-color: #D5D5D5; padding-left: 5px; padding-right: 5px; }
 #paidbmvwxp .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #paidbmvwxp .gt_row_group_first td { border-top-width: 2px; }
 #paidbmvwxp .gt_row_group_first th { border-top-width: 2px; }
 #paidbmvwxp .gt_striped { color: #333333; background-color: #F4F4F4; }
 #paidbmvwxp .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D5D5D5; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D5D5D5; }
 #paidbmvwxp .gt_grand_summary_row { color: #FFFFFF; background-color: #01837B; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #paidbmvwxp .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #paidbmvwxp .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #paidbmvwxp .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #paidbmvwxp .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #paidbmvwxp .gt_left { text-align: left; }
 #paidbmvwxp .gt_center { text-align: center; }
 #paidbmvwxp .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #paidbmvwxp .gt_font_normal { font-weight: normal; }
 #paidbmvwxp .gt_font_bold { font-weight: bold; }
 #paidbmvwxp .gt_font_italic { font-style: italic; }
 #paidbmvwxp .gt_super { font-size: 65%; }
 #paidbmvwxp .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #paidbmvwxp .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
  <tr class="gt_heading">
    <td colspan="3" class="gt_heading gt_title gt_font_normal">✅ Django Deployment Checklist</td>
  </tr>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Status">Status</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Task">Task</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="Notes">Notes</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-1" random-id="d18fbbbe-df4a-c5fa-6104-3b6446329b65"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set DEBUG = False</td>
    <td class="gt_row gt_left">Never deploy with DEBUG = True ⚠️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-2" random-id="6c1daf70-1f5a-2eba-6c8b-a36590c4bd5c"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Configure ALLOWED_HOSTS</td>
    <td class="gt_row gt_left gt_striped">Include your domain(s) or IP address 🌐</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-3" random-id="678ef857-3e34-116c-8549-41faa2c9720f"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set up a secret key</td>
    <td class="gt_row gt_left">Use a strong, secure key from an environment variable 🔐</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-4" random-id="d8616f77-b983-c0e2-7e81-e7d9ca068877"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Collect static files</td>
    <td class="gt_row gt_left gt_striped">Run `python manage.py collectstatic` 📦</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-5" random-id="f1ccbee2-e6c2-2f4c-cbbf-7de843d91cb5"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Apply database migrations</td>
    <td class="gt_row gt_left">Run `python manage.py migrate` 🗃️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-6" random-id="e1612292-3fc5-4eef-7c04-dc94546004a3"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Set up gunicorn or uWSGI</td>
    <td class="gt_row gt_left gt_striped">Use as a WSGI server in production 🔄</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-7" random-id="31ac4092-4c3a-405f-31b5-b03b989ee52b"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Configure reverse proxy (e.g., Nginx)</td>
    <td class="gt_row gt_left">Serve static/media files and forward to WSGI server 🧭</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-8" random-id="53a81e6f-6546-67c2-67d0-02640d820509"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Secure the database</td>
    <td class="gt_row gt_left gt_striped">Use strong credentials, disable remote root login 🛡️</td>
  </tr>
  <tr>
    <td class="gt_row gt_left"><marimo-ui-element object-id="vblA-9" random-id="f684967f-2a85-beda-d145-cb80872aee80"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left">Set up HTTPS (SSL)</td>
    <td class="gt_row gt_left">Use Let's Encrypt or your own certificate 🔒</td>
  </tr>
  <tr>
    <td class="gt_row gt_left gt_striped"><marimo-ui-element object-id="vblA-10" random-id="e96490da-82c9-b4e2-52d0-cc91d4b04f4f"><marimo-switch data-initial-value="false" data-label="null" data-disabled="false"></marimo-switch></marimo-ui-element></td>
    <td class="gt_row gt_left gt_striped">Configure logging &amp; monitoring</td>
    <td class="gt_row gt_left gt_striped">Track errors and app performance 📊</td>
  </tr>
</tbody>
  <tfoot class="gt_sourcenotes">
  <tr>
    <td class="gt_sourcenote" colspan="3">0 / 10</td>
  </tr>
  <tr>
    <td class="gt_sourcenote" colspan="3">    <div style="width: 750px; background-color: lightgray;">        <div style="height:20px;width:0.0px;background-color:#66CDAA;"></div>    </div>    </td>
  </tr>
</tfoot>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">done_count%20%3D%20sum(s.value%20for%20s%20in%20status_widgets)%0A%0Agt%20%3D%20(%0A%20%20%20%20GT(%0A%20%20%20%20%20%20%20%20df.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.Series(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Bstatus._repr_html_()%20for%20status%20in%20status_widgets%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20).alias(%22Status%22)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20.tab_source_note(f%22%7Bdone_count%7D%20%2F%20%7Bn_row%7D%22)%0A%20%20%20%20.tab_source_note(%0A%20%20%20%20%20%20%20%20html(%0A%20%20%20%20%20%20%20%20%20%20%20%20create_bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20done_count%20%2F%20n_row%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_width%3D750%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20height%3D20%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background_color1%3D%22lightgray%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20background_color2%3D%22%2366CDAA%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20.tab_header(%22%E2%9C%85%20Django%20Deployment%20Checklist%22)%0A%20%20%20%20.opt_stylize(color%3D%22cyan%22%2C%20style%3D4)%0A)%0Agt</marimo-cell-code>
        </marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_xEAc6KCZkX9h8hxoCkKNBc/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.7"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10"></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (mo,)</span>
<span id="cb1-12"></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-16">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-17">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html, md</span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, html, pl</span>
<span id="cb1-20"></span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-24">    tasks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set DEBUG = False"</span>,</span>
<span id="cb1-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure ALLOWED_HOSTS"</span>,</span>
<span id="cb1-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up a secret key"</span>,</span>
<span id="cb1-28">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Collect static files"</span>,</span>
<span id="cb1-29">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Apply database migrations"</span>,</span>
<span id="cb1-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up gunicorn or uWSGI"</span>,</span>
<span id="cb1-31">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure reverse proxy (e.g., Nginx)"</span>,</span>
<span id="cb1-32">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Secure the database"</span>,</span>
<span id="cb1-33">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set up HTTPS (SSL)"</span>,</span>
<span id="cb1-34">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Configure logging &amp; monitoring"</span>,</span>
<span id="cb1-35">    ]</span>
<span id="cb1-36"></span>
<span id="cb1-37">    notes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb1-38">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Never deploy with DEBUG = True ⚠️"</span>,</span>
<span id="cb1-39">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Include your domain(s) or IP address 🌐"</span>,</span>
<span id="cb1-40">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use a strong, secure key from an environment variable 🔐"</span>,</span>
<span id="cb1-41">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run `python manage.py collectstatic` 📦"</span>,</span>
<span id="cb1-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Run `python manage.py migrate` 🗃️"</span>,</span>
<span id="cb1-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use as a WSGI server in production 🔄"</span>,</span>
<span id="cb1-44">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Serve static/media files and forward to WSGI server 🧭"</span>,</span>
<span id="cb1-45">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use strong credentials, disable remote root login 🛡️"</span>,</span>
<span id="cb1-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Use Let's Encrypt or your own certificate 🔒"</span>,</span>
<span id="cb1-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Track errors and app performance 📊"</span>,</span>
<span id="cb1-48">    ]</span>
<span id="cb1-49"></span>
<span id="cb1-50">    n_row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(tasks)</span>
<span id="cb1-51">    status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☐"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_row</span>
<span id="cb1-52">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Status"</span>: status, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Task"</span>: tasks, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Notes"</span>: notes}</span>
<span id="cb1-53"></span>
<span id="cb1-54">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df, n_row</span>
<span id="cb1-56"></span>
<span id="cb1-57"></span>
<span id="cb1-58"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, n_row):</span>
<span id="cb1-60">    status_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch()</span>
<span id="cb1-61">    status_widgets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.array([status_widget] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n_row)</span>
<span id="cb1-62">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (status_widgets,)</span>
<span id="cb1-63"></span>
<span id="cb1-64"></span>
<span id="cb1-65"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.function</span></span>
<span id="cb1-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_bar(</span>
<span id="cb1-67">    x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>,</span>
<span id="cb1-68">    max_width: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb1-69">    height: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb1-70">    background_color1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-71">    background_color2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-72">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb1-73">    width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(max_width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-74">    px_width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px"</span></span>
<span id="cb1-75">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-76"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="width: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px; background-color: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-77"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;div style="height:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>height<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">px;width:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>px_width<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;background-color:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;"&gt;&lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-78"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-79"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-80"></span>
<span id="cb1-81"></span>
<span id="cb1-82"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-83"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, df, html, n_row, pl, status_widgets):</span>
<span id="cb1-84">    done_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(s.value <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> status_widgets)</span>
<span id="cb1-85"></span>
<span id="cb1-86">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-87">        GT(</span>
<span id="cb1-88">            df.with_columns(</span>
<span id="cb1-89">                pl.Series(</span>
<span id="cb1-90">                    [status._repr_html_() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> status <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> status_widgets]</span>
<span id="cb1-91">                ).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Status"</span>)</span>
<span id="cb1-92">            )</span>
<span id="cb1-93">        )</span>
<span id="cb1-94">        .tab_source_note(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>done_count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> / </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>n_row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-95">        .tab_source_note(</span>
<span id="cb1-96">            html(</span>
<span id="cb1-97">                create_bar(</span>
<span id="cb1-98">                    done_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n_row,</span>
<span id="cb1-99">                    max_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">750</span>,</span>
<span id="cb1-100">                    height<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,</span>
<span id="cb1-101">                    background_color1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgray"</span>,</span>
<span id="cb1-102">                    background_color2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#66CDAA"</span>,</span>
<span id="cb1-103">                )</span>
<span id="cb1-104">            )</span>
<span id="cb1-105">        )</span>
<span id="cb1-106">        .tab_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Django Deployment Checklist"</span>)</span>
<span id="cb1-107">        .opt_stylize(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-108">    )</span>
<span id="cb1-109">    gt</span>
<span id="cb1-110">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-111"></span>
<span id="cb1-112"></span>
<span id="cb1-113"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-114">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<ol type="1">
<li>This table is for demonstration purposes only. You should customize it based on your own needs.</li>
<li>This post was drafted by me, with AI assistance to refine the content.</li>
</ol>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>gt</category>
  <category>django</category>
  <guid>https://tech.ycwu.space/posts/marimo-django-deployment-checklist/20250625.html</guid>
  <pubDate>Wed, 25 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-django-deployment-checklist/marimo_django_deployment_checklist.png" medium="image" type="image/png" height="95" width="144"/>
</item>
<item>
  <title>Send Email via marimo</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/resend-via-marimo/20250625.html</link>
  <description><![CDATA[ 






<p>This post demonstrates how to send emails using <a href="https://resend.com/">Resend</a> from within a marimo app.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/resend-via-marimo/resend_via_marimo.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Resend via marimo"></p>
</figure>
</div>
<p>It seems the app runs fine locally but doesn’t work in a WASM environment. It’ll be interesting to explore where the boundaries of WASM lie.</p>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_9b9N9Lh1payY9HvowyzvSB/app">molab</a>.</p>
<div id="871db116" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.14.7"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> html <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> escape</span>
<span id="cb1-12">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-13">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> resend</span>
<span id="cb1-14"></span>
<span id="cb1-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> escape, mo, resend</span>
<span id="cb1-16"></span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(escape, resend):</span>
<span id="cb1-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_email(d: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb1-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        https://resend.com/docs/send-with-python</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        """</span></span>
<span id="cb1-24">        resend.api_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RESEND_API_KEY"</span>]</span>
<span id="cb1-25">        from_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from_"</span>]</span>
<span id="cb1-26">        to <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [mail.strip() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> mail <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>].split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)]</span>
<span id="cb1-27">        subject <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>]</span>
<span id="cb1-28">        html_ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> escape(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"content"</span>]).replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;br&gt;"</span>)</span>
<span id="cb1-29"></span>
<span id="cb1-30">        params: resend.Emails.SendParams <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-31">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from"</span>: from_,</span>
<span id="cb1-32">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>: to,</span>
<span id="cb1-33">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>: subject,</span>
<span id="cb1-34">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>: html_,</span>
<span id="cb1-35">        }</span>
<span id="cb1-36"></span>
<span id="cb1-37">        email <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> resend.Emails.send(params)</span>
<span id="cb1-38"></span>
<span id="cb1-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (send_email,)</span>
<span id="cb1-40"></span>
<span id="cb1-41"></span>
<span id="cb1-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo, send_email):</span>
<span id="cb1-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a form with multiple eleme</span></span>
<span id="cb1-45">    form <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-46">        mo.md(</span>
<span id="cb1-47">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-48"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        **marimo mail**</span></span>
<span id="cb1-49"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{RESEND_API_KEY}</span></span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{from_}</span></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{to}</span></span>
<span id="cb1-54"></span>
<span id="cb1-55"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{subject}</span></span>
<span id="cb1-56"></span>
<span id="cb1-57"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{content}</span></span>
<span id="cb1-58"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-59">        )</span>
<span id="cb1-60">        .batch(</span>
<span id="cb1-61">            RESEND_API_KEY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(</span>
<span id="cb1-62">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RESEND_API_KEY"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"password"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-63">            ),</span>
<span id="cb1-64">            from_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"From"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-65">            to<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"To"</span>, kind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"email"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-66">            subject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subject"</span>, full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-67">            content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>mo.ui.text_area(full_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>),</span>
<span id="cb1-68">        )</span>
<span id="cb1-69">        .form(show_clear_button<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, on_change<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>send_email)</span>
<span id="cb1-70">    )</span>
<span id="cb1-71">    form</span>
<span id="cb1-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-73"></span>
<span id="cb1-74"></span>
<span id="cb1-75"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-76">    app.run()</span></code></pre></div></div>
</details>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <category>resend</category>
  <guid>https://tech.ycwu.space/posts/resend-via-marimo/20250625.html</guid>
  <pubDate>Wed, 25 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/resend-via-marimo/resend_via_marimo.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>Great Tables with marimo in Quarto</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/gt-marimo-in-quarto/20250612.html</link>
  <description><![CDATA[ 






<p>This is a follow-up to my <a href="../../posts/marimo-in-quarto/20250607.html">previous post</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/gt-marimo-in-quarto/gt_marimo_in_quarto.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Great Tables with marimo in Quarto"></p>
</figure>
</div>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<p>Here, I demonstrate how marimo widgets can be embedded in Great Tables by wrapping them with the <code>html()</code> function provided by the library. This allows interactive widgets to control the table’s appearance in a Quarto environment via WASM — a surprisingly powerful capability, in my opinion.</p>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20html</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">data%20%3D%20%7B%0A%20%20%20%20%22col1%22%3A%20%5B2%2C%205%2C%207%2C%2010%2C%2015%5D%2C%0A%20%20%20%20%22col2%22%3A%20%5B%22x%22%2C%20%22y%22%2C%20%22y%22%2C%20%22z%22%2C%20%22z%22%5D%2C%0A%20%20%20%20%22color%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%22lightgrey%22%2C%0A%20%20%20%20%20%20%20%20%22lightblue%22%2C%0A%20%20%20%20%20%20%20%20%22lightblue%22%2C%0A%20%20%20%20%20%20%20%20%22papayawhip%22%2C%0A%20%20%20%20%20%20%20%20%22papayawhip%22%2C%0A%20%20%20%20%5D%2C%0A%7D%0Adf%20%3D%20pl.DataFrame(data)%0Aprint(df)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">style_widget%20%3D%20mo.ui.slider(1%2C%206%2C%20label%3D%22Style%20Number%22)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%20value%3D_colors%5B0%5D%2C%20label%3D%22Style%20Color%22%2C%20inline%3DTrue%0A)%0A%0Arow_striping_widget%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Row%20Striping%3F%22)%0A%0Agt%20%3D%20(%0A%20%20%20%20GT(df)%0A%20%20%20%20.tab_header(html(style_widget)%2C%20html(color_widget))%0A%20%20%20%20.tab_source_note(html(row_striping_widget))%0A%20%20%20%20.opt_align_table_header(%22left%22)%0A)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
            <marimo-cell-output>
            <div id="zozvutkarb" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#zozvutkarb table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#zozvutkarb thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#zozvutkarb p { margin: 0; padding: 0; }
 #zozvutkarb .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #004D80; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #004D80; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #zozvutkarb .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #zozvutkarb .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #zozvutkarb .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #zozvutkarb .gt_heading { background-color: #FFFFFF; text-align: left; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #zozvutkarb .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #zozvutkarb .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #zozvutkarb .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #zozvutkarb .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #zozvutkarb .gt_column_spanner_outer:first-child { padding-left: 0; }
 #zozvutkarb .gt_column_spanner_outer:last-child { padding-right: 0; }
 #zozvutkarb .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #zozvutkarb .gt_spanner_row { border-bottom-style: hidden; }
 #zozvutkarb .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #zozvutkarb .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: middle; }
 #zozvutkarb .gt_from_md> :first-child { margin-top: 0; }
 #zozvutkarb .gt_from_md> :last-child { margin-bottom: 0; }
 #zozvutkarb .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: none; border-top-width: 1px; border-top-color: #89D3FE; border-left-style: none; border-left-width: 1px; border-left-color: #89D3FE; border-right-style: none; border-right-width: 1px; border-right-color: #89D3FE; vertical-align: middle; overflow-x: hidden; }
 #zozvutkarb .gt_stub { color: #FFFFFF; background-color: #0076BA; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #0076BA; padding-left: 5px; padding-right: 5px; }
 #zozvutkarb .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #zozvutkarb .gt_row_group_first td { border-top-width: 2px; }
 #zozvutkarb .gt_row_group_first th { border-top-width: 2px; }
 #zozvutkarb .gt_striped { color: #333333; background-color: #F4F4F4; }
 #zozvutkarb .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #zozvutkarb .gt_grand_summary_row { color: #333333; background-color: #89D3FE; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #zozvutkarb .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #zozvutkarb .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #zozvutkarb .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #zozvutkarb .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #zozvutkarb .gt_left { text-align: left; }
 #zozvutkarb .gt_center { text-align: center; }
 #zozvutkarb .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #zozvutkarb .gt_font_normal { font-weight: normal; }
 #zozvutkarb .gt_font_bold { font-weight: bold; }
 #zozvutkarb .gt_font_italic { font-style: italic; }
 #zozvutkarb .gt_super { font-size: 65%; }
 #zozvutkarb .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #zozvutkarb .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
  <tr class="gt_heading">
    <td colspan="3" class="gt_heading gt_title gt_font_normal"><marimo-ui-element object-id="vblA-0" random-id="dcfc7de2-48fd-0647-98fb-fcb92c5066d9"><marimo-slider data-initial-value="1" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Style Number</span></span>&quot;" data-start="1" data-stop="6" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element></td>
  </tr>
  <tr class="gt_heading">
    <td colspan="3" class="gt_heading gt_subtitle gt_font_normal gt_bottom_border"><marimo-ui-element object-id="vblA-1" random-id="bcf6f376-7a56-fdb1-944e-fb9d4acab94c"><marimo-radio data-initial-value="&quot;blue&quot;" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Style Color</span></span>&quot;" data-options="[&quot;blue&quot;,&quot;cyan&quot;,&quot;pink&quot;,&quot;green&quot;,&quot;red&quot;,&quot;gray&quot;]" data-inline="true" data-disabled="false"></marimo-radio></marimo-ui-element></td>
  </tr>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="col1">col1</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="col2">col2</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="color">color</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_right">2</td>
    <td class="gt_row gt_left">x</td>
    <td class="gt_row gt_left">lightgrey</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">5</td>
    <td class="gt_row gt_left gt_striped">y</td>
    <td class="gt_row gt_left gt_striped">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">7</td>
    <td class="gt_row gt_left">y</td>
    <td class="gt_row gt_left">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">10</td>
    <td class="gt_row gt_left gt_striped">z</td>
    <td class="gt_row gt_left gt_striped">papayawhip</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">15</td>
    <td class="gt_row gt_left">z</td>
    <td class="gt_row gt_left">papayawhip</td>
  </tr>
</tbody>
  <tfoot class="gt_sourcenotes">
  <tr>
    <td class="gt_sourcenote" colspan="3"><marimo-ui-element object-id="vblA-2" random-id="61d92b17-f48c-4206-2d54-a9dd77479812"><marimo-switch data-initial-value="true" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Row Striping?</span></span>&quot;" data-disabled="false"></marimo-switch></marimo-ui-element></td>
  </tr>
</tfoot>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">gt.opt_stylize(%0A%20%20%20%20style%3Dstyle_widget.value%2C%0A%20%20%20%20color%3Dcolor_widget.value%2C%0A%20%20%20%20add_row_striping%3Drow_striping_widget.value%2C%0A)</marimo-cell-code>
        </marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_En2k3WtpkE1Uf6a8QvCS7n/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.13.15"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, html</span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, html, mo, pl</span>
<span id="cb1-14"></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-18">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>],</span>
<span id="cb1-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [</span>
<span id="cb1-22">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>,</span>
<span id="cb1-23">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>,</span>
<span id="cb1-24">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>,</span>
<span id="cb1-25">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>,</span>
<span id="cb1-26">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>,</span>
<span id="cb1-27">        ],</span>
<span id="cb1-28">    }</span>
<span id="cb1-29">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-31"></span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, df, html, mo):</span>
<span id="cb1-35">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Number"</span>)</span>
<span id="cb1-36"></span>
<span id="cb1-37">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-38">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-39">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Style Color"</span>, inline<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-40">    )</span>
<span id="cb1-41"></span>
<span id="cb1-42">    row_striping_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Row Striping?"</span>)</span>
<span id="cb1-43"></span>
<span id="cb1-44">    gt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-45">        GT(df)</span>
<span id="cb1-46">        .tab_header(html(style_widget), html(color_widget))</span>
<span id="cb1-47">        .tab_source_note(html(row_striping_widget))</span>
<span id="cb1-48">        .opt_align_table_header(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb1-49">    )</span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> color_widget, gt, row_striping_widget, style_widget</span>
<span id="cb1-51"></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-54"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(color_widget, gt, row_striping_widget, style_widget):</span>
<span id="cb1-55">    gt.opt_stylize(</span>
<span id="cb1-56">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value,</span>
<span id="cb1-57">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value,</span>
<span id="cb1-58">        add_row_striping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_striping_widget.value,</span>
<span id="cb1-59">    )</span>
<span id="cb1-60">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-61"></span>
<span id="cb1-62"></span>
<span id="cb1-63"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-64">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>gt</category>
  <category>marimo</category>
  <guid>https://tech.ycwu.space/posts/gt-marimo-in-quarto/20250612.html</guid>
  <pubDate>Thu, 12 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/gt-marimo-in-quarto/gt_marimo_in_quarto.png" medium="image" type="image/png" height="70" width="144"/>
</item>
<item>
  <title>marimo in Quarto</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/marimo-in-quarto/20250607.html</link>
  <description><![CDATA[ 






<p>This post demonstrates that Polars, Great Tables, and marimo can successfully run within a Quarto environment (as shown in this post). The example uses a table styler selector from Great Tables—and I’m honestly surprised it works!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/marimo-in-quarto/marimo_in_quarto.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="marimo in Quarto"></p>
</figure>
</div>
<section id="marimo" class="level2">
<h2 class="anchored" data-anchor-id="marimo">marimo</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Give It a Sec – WASM Magic Happening
</div>
</div>
<div class="callout-body-container callout-body">
<p>The widgets may take a few moments to load, as they rely on WebAssembly under the hood.</p>
</div>
</div>
<marimo-island data-app-id="main" data-cell-id="Hbol" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">import%20marimo%20as%20mo%0Aimport%20polars%20as%20pl%0Afrom%20great_tables%20import%20GT%2C%20loc%2C%20style</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="MJUe" data-reactive="true">
    <marimo-cell-output>
    <span></span>
    </marimo-cell-output>
    <marimo-cell-code hidden="">data%20%3D%20%7B%0A%20%20%20%20%22col1%22%3A%20%5B2%2C%205%2C%207%2C%2010%2C%2015%5D%2C%0A%20%20%20%20%22col2%22%3A%20%5B%22x%22%2C%20%22y%22%2C%20%22y%22%2C%20%22z%22%2C%20%22z%22%5D%2C%0A%20%20%20%20%22color%22%3A%20%5B%22lightgrey%22%2C%20%22lightblue%22%2C%20%22lightblue%22%2C%20%22papayawhip%22%2C%20%22papayawhip%22%5D%2C%0A%7D%0Adf%20%3D%20pl.DataFrame(data)%0Aprint(df)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="vblA" data-reactive="true">
    <marimo-cell-output>
    <div style="display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: normal;flex-wrap: nowrap;gap: 0.5rem"><marimo-ui-element object-id="vblA-0" random-id="b6372295-ca5a-78ca-75a4-05fd3ff28bac"><marimo-slider data-initial-value="1" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Select Style Number</span></span>&quot;" data-start="1" data-stop="6" data-steps="[]" data-debounce="false" data-disabled="false" data-orientation="&quot;horizontal&quot;" data-show-value="false" data-include-input="false" data-full-width="false"></marimo-slider></marimo-ui-element><marimo-ui-element object-id="vblA-1" random-id="d6175155-4449-b40d-d06d-d5e7b9ed8b01"><marimo-radio data-initial-value="&quot;blue&quot;" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Select Style Color</span></span>&quot;" data-options="[&quot;blue&quot;,&quot;cyan&quot;,&quot;pink&quot;,&quot;green&quot;,&quot;red&quot;,&quot;gray&quot;]" data-inline="false" data-disabled="false"></marimo-radio></marimo-ui-element><marimo-ui-element object-id="vblA-2" random-id="7b51622d-2f66-4988-ac25-7143c7c0c258"><marimo-switch data-initial-value="true" data-label="&quot;<span class=\&quot;markdown prose dark:prose-invert contents\&quot;><span class=\&quot;paragraph\&quot;>Add Row Striping?</span></span>&quot;" data-disabled="false"></marimo-switch></marimo-ui-element></div>
    </marimo-cell-output>
    <marimo-cell-code hidden="">style_widget%20%3D%20mo.ui.slider(1%2C%206%2C%20label%3D%22Select%20Style%20Number%22)%0Amo.output.append(style_widget)%0A%0A_colors%20%3D%20%5B%22blue%22%2C%20%22cyan%22%2C%20%22pink%22%2C%20%22green%22%2C%20%22red%22%2C%20%22gray%22%5D%0Acolor_widget%20%3D%20mo.ui.radio(%0A%20%20%20%20options%3D_colors%2C%20value%3D_colors%5B0%5D%2C%20label%3D%22Select%20Style%20Color%22%0A)%0Amo.output.append(color_widget)%0A%0Arow_striping_widget%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Add%20Row%20Striping%3F%22)%0Amo.output.append(row_striping_widget)</marimo-cell-code>
</marimo-island>
<marimo-island data-app-id="main" data-cell-id="bkHC" data-reactive="true">
            <marimo-cell-output>
            <div id="fyoahnhleu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#fyoahnhleu table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }
#fyoahnhleu thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#fyoahnhleu p { margin: 0; padding: 0; }
 #fyoahnhleu .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #004D80; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #004D80; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #fyoahnhleu .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #fyoahnhleu .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #fyoahnhleu .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #fyoahnhleu .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fyoahnhleu .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #fyoahnhleu .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fyoahnhleu .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #fyoahnhleu .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #fyoahnhleu .gt_column_spanner_outer:first-child { padding-left: 0; }
 #fyoahnhleu .gt_column_spanner_outer:last-child { padding-right: 0; }
 #fyoahnhleu .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #fyoahnhleu .gt_spanner_row { border-bottom-style: hidden; }
 #fyoahnhleu .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #fyoahnhleu .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; vertical-align: middle; }
 #fyoahnhleu .gt_from_md> :first-child { margin-top: 0; }
 #fyoahnhleu .gt_from_md> :last-child { margin-bottom: 0; }
 #fyoahnhleu .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: none; border-top-width: 1px; border-top-color: #89D3FE; border-left-style: none; border-left-width: 1px; border-left-color: #89D3FE; border-right-style: none; border-right-width: 1px; border-right-color: #89D3FE; vertical-align: middle; overflow-x: hidden; }
 #fyoahnhleu .gt_stub { color: #FFFFFF; background-color: #0076BA; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #0076BA; padding-left: 5px; padding-right: 5px; }
 #fyoahnhleu .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #fyoahnhleu .gt_row_group_first td { border-top-width: 2px; }
 #fyoahnhleu .gt_row_group_first th { border-top-width: 2px; }
 #fyoahnhleu .gt_striped { color: #333333; background-color: #F4F4F4; }
 #fyoahnhleu .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #0076BA; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #0076BA; }
 #fyoahnhleu .gt_grand_summary_row { color: #333333; background-color: #89D3FE; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #fyoahnhleu .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #fyoahnhleu .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #fyoahnhleu .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #fyoahnhleu .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #fyoahnhleu .gt_left { text-align: left; }
 #fyoahnhleu .gt_center { text-align: center; }
 #fyoahnhleu .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #fyoahnhleu .gt_font_normal { font-weight: normal; }
 #fyoahnhleu .gt_font_bold { font-weight: bold; }
 #fyoahnhleu .gt_font_italic { font-style: italic; }
 #fyoahnhleu .gt_super { font-size: 65%; }
 #fyoahnhleu .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #fyoahnhleu .gt_asterisk { font-size: 100%; vertical-align: 0; }
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>
<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="col1">col1</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="col2">col2</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="color">color</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td class="gt_row gt_right">2</td>
    <td class="gt_row gt_left">x</td>
    <td class="gt_row gt_left">lightgrey</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">5</td>
    <td class="gt_row gt_left gt_striped">y</td>
    <td class="gt_row gt_left gt_striped">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">7</td>
    <td class="gt_row gt_left">y</td>
    <td class="gt_row gt_left">lightblue</td>
  </tr>
  <tr>
    <td class="gt_row gt_right gt_striped">10</td>
    <td class="gt_row gt_left gt_striped">z</td>
    <td class="gt_row gt_left gt_striped">papayawhip</td>
  </tr>
  <tr>
    <td class="gt_row gt_right">15</td>
    <td class="gt_row gt_left">z</td>
    <td class="gt_row gt_left">papayawhip</td>
  </tr>
</tbody>
</table>
</div>
            </marimo-cell-output>
            <marimo-cell-code hidden="">GT(df).opt_stylize(%0A%20%20%20%20style%3Dstyle_widget.value%2C%0A%20%20%20%20color%3Dcolor_widget.value%2C%0A%20%20%20%20add_row_striping%3Drow_striping_widget.value%2C%0A)</marimo-cell-code>
        </marimo-island>
<p>Check out the full marimo code below or view it on <a href="https://molab.marimo.io/notebooks/nb_uHQvXDXF3UsvFQD9xZkaiu/app">molab</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo</span>
<span id="cb1-2"></span>
<span id="cb1-3">__generated_with <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.13.15"</span></span>
<span id="cb1-4">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> marimo.App(width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _():</span>
<span id="cb1-9">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marimo <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> mo</span>
<span id="cb1-10">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-11">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb1-12"></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> GT, mo, pl</span>
<span id="cb1-14"></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(pl):</span>
<span id="cb1-18">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>],</span>
<span id="cb1-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>],</span>
<span id="cb1-22">    }</span>
<span id="cb1-23">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb1-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (df,)</span>
<span id="cb1-25"></span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(mo):</span>
<span id="cb1-29">    style_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.slider(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select Style Number"</span>)</span>
<span id="cb1-30">    mo.output.append(style_widget)</span>
<span id="cb1-31"></span>
<span id="cb1-32">    _colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cyan"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>]</span>
<span id="cb1-33">    color_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.radio(</span>
<span id="cb1-34">        options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>_colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select Style Color"</span></span>
<span id="cb1-35">    )</span>
<span id="cb1-36">    mo.output.append(color_widget)</span>
<span id="cb1-37"></span>
<span id="cb1-38">    row_striping_widget <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mo.ui.switch(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Add Row Striping?"</span>)</span>
<span id="cb1-39">    mo.output.append(row_striping_widget)</span>
<span id="cb1-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> color_widget, row_striping_widget, style_widget</span>
<span id="cb1-41"></span>
<span id="cb1-42"></span>
<span id="cb1-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.cell</span></span>
<span id="cb1-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _(GT, color_widget, df, row_striping_widget, style_widget):</span>
<span id="cb1-45">    GT(df).opt_stylize(</span>
<span id="cb1-46">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style_widget.value,</span>
<span id="cb1-47">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_widget.value,</span>
<span id="cb1-48">        add_row_striping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row_striping_widget.value,</span>
<span id="cb1-49">    )</span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb1-51"></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb1-54">    app.run()</span></code></pre></div></div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>marimo</category>
  <guid>https://tech.ycwu.space/posts/marimo-in-quarto/20250607.html</guid>
  <pubDate>Sat, 07 Jun 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/marimo-in-quarto/marimo_in_quarto.png" medium="image" type="image/png" height="111" width="144"/>
</item>
<item>
  <title>Styling tricks in Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/styling-tricks-table-body/20250526.html</link>
  <description><![CDATA[ 






<p>This post highlights a powerful yet underused feature in Great Tables: using existing columns to style the table body via <a href="https://posit-dev.github.io/great-tables/reference/from_column.html">from_column()</a>.</p>
<p>Let’s say you have the following Polars DataFrame <code>df</code>:</p>
<div id="42fc14bb" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, from_column, loc, style</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> films</span>
<span id="cb1-4"></span>
<span id="cb1-5"></span>
<span id="cb1-6">df_style <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb1-7">    {</span>
<span id="cb1-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgreen"</span>],</span>
<span id="cb1-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"small"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x-large"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medium"</span>],</span>
<span id="cb1-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lighter"</span>],</span>
<span id="cb1-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>],</span>
<span id="cb1-12">    }</span>
<span id="cb1-13">)</span>
<span id="cb1-14"></span>
<span id="cb1-15">columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"run_time"</span>]</span>
<span id="cb1-16"></span>
<span id="cb1-17">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat(</span>
<span id="cb1-18">    [</span>
<span id="cb1-19">        pl.from_pandas(films.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>).loc[:, columns]),</span>
<span id="cb1-20">        df_style,</span>
<span id="cb1-21">    ],</span>
<span id="cb1-22">    how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"horizontal"</span>,</span>
<span id="cb1-23">)</span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>shape: (3, 7)
┌──────┬───────────────────────────┬──────────┬────────────┬─────────┬─────────┬────────┐
│ year ┆ title                     ┆ run_time ┆ color      ┆ size    ┆ weight  ┆ align  │
│ ---  ┆ ---                       ┆ ---      ┆ ---        ┆ ---     ┆ ---     ┆ ---    │
│ i64  ┆ str                       ┆ str      ┆ str        ┆ str     ┆ str     ┆ str    │
╞══════╪═══════════════════════════╪══════════╪════════════╪═════════╪═════════╪════════╡
│ 1946 ┆ The Lovers                ┆ 1h 30m   ┆ papayawhip ┆ small   ┆ normal  ┆ right  │
│ 1946 ┆ Anna and the King of Siam ┆ 2h 8m    ┆ lightblue  ┆ x-large ┆ bold    ┆ center │
│ 1946 ┆ Blood and Fire            ┆ 1h 40m   ┆ lightgreen ┆ medium  ┆ lighter ┆ left   │
└──────┴───────────────────────────┴──────────┴────────────┴─────────┴─────────┴────────┘</code></pre>
</div>
</div>
<p>The <code>color</code>, <code>size</code>, <code>weight</code>, and <code>align</code> columns contain style-related metadata. We can use <code>from_column()</code> to map this metadata to four styling options provided by Great Tables—<a href="https://posit-dev.github.io/great-tables/reference/style.fill.html">style.fill()</a>, <a href="https://posit-dev.github.io/great-tables/reference/style.text.html">style.text()</a>, <a href="https://posit-dev.github.io/great-tables/reference/style.borders.html">style.borders()</a>, and <a href="https://posit-dev.github.io/great-tables/reference/style.css.html">style.css()</a>—all of which are built on the internal <code>CellStyle</code> class.</p>
<div id="b4dde539" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">(</span>
<span id="cb3-2">    GT(df)</span>
<span id="cb3-3">    .tab_style(</span>
<span id="cb3-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb3-5">            style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)),</span>
<span id="cb3-6">            style.text(</span>
<span id="cb3-7">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>),</span>
<span id="cb3-8">                weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>),</span>
<span id="cb3-9">                align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>),</span>
<span id="cb3-10">            ),</span>
<span id="cb3-11">        ],</span>
<span id="cb3-12">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(columns),</span>
<span id="cb3-13">    )</span>
<span id="cb3-14">    .cols_hide(df_style.columns)</span>
<span id="cb3-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>)</span>
<span id="cb3-16">)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<div id="fktrpgywoo" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>
#fktrpgywoo table {
          font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
          -webkit-font-smoothing: antialiased;
          -moz-osx-font-smoothing: grayscale;
        }

#fktrpgywoo thead, tbody, tfoot, tr, td, th { border-style: none; }
 tr { background-color: transparent; }
#fktrpgywoo p { margin: 0; padding: 0; }
 #fktrpgywoo .gt_table { display: table; border-collapse: collapse; line-height: normal; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; }
 #fktrpgywoo .gt_caption { padding-top: 4px; padding-bottom: 4px; }
 #fktrpgywoo .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; }
 #fktrpgywoo .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 3px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; }
 #fktrpgywoo .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fktrpgywoo .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; }
 #fktrpgywoo .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; }
 #fktrpgywoo .gt_col_heading { color: #FFFFFF; background-color: #5F5F5F; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; }
 #fktrpgywoo .gt_column_spanner_outer { color: #FFFFFF; background-color: #5F5F5F; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; }
 #fktrpgywoo .gt_column_spanner_outer:first-child { padding-left: 0; }
 #fktrpgywoo .gt_column_spanner_outer:last-child { padding-right: 0; }
 #fktrpgywoo .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; }
 #fktrpgywoo .gt_spanner_row { border-bottom-style: hidden; }
 #fktrpgywoo .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; }
 #fktrpgywoo .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; vertical-align: middle; }
 #fktrpgywoo .gt_from_md> :first-child { margin-top: 0; }
 #fktrpgywoo .gt_from_md> :last-child { margin-bottom: 0; }
 #fktrpgywoo .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: none; border-top-width: 1px; border-top-color: #D5D5D5; border-left-style: none; border-left-width: 1px; border-left-color: #D5D5D5; border-right-style: none; border-right-width: 1px; border-right-color: #D5D5D5; vertical-align: middle; overflow-x: hidden; }
 #fktrpgywoo .gt_stub { color: #333333; background-color: #D5D5D5; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D5D5D5; padding-left: 5px; padding-right: 5px; }
 #fktrpgywoo .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; }
 #fktrpgywoo .gt_row_group_first td { border-top-width: 2px; }
 #fktrpgywoo .gt_row_group_first th { border-top-width: 2px; }
 #fktrpgywoo .gt_striped { color: #333333; background-color: #F4F4F4; }
 #fktrpgywoo .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #5F5F5F; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #5F5F5F; }
 #fktrpgywoo .gt_grand_summary_row { color: #333333; background-color: #D5D5D5; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; }
 #fktrpgywoo .gt_first_grand_summary_row_bottom { border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; }
 #fktrpgywoo .gt_last_grand_summary_row_top { border-bottom-style: double; border-bottom-width: 6px; border-bottom-color: #D3D3D3; }
 #fktrpgywoo .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; }
 #fktrpgywoo .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; text-align: left; }
 #fktrpgywoo .gt_left { text-align: left; }
 #fktrpgywoo .gt_center { text-align: center; }
 #fktrpgywoo .gt_right { text-align: right; font-variant-numeric: tabular-nums; }
 #fktrpgywoo .gt_font_normal { font-weight: normal; }
 #fktrpgywoo .gt_font_bold { font-weight: bold; }
 #fktrpgywoo .gt_font_italic { font-style: italic; }
 #fktrpgywoo .gt_super { font-size: 65%; }
 #fktrpgywoo .gt_footnote_marks { font-size: 75%; vertical-align: 0.4em; position: initial; }
 #fktrpgywoo .gt_asterisk { font-size: 100%; vertical-align: 0; }
 
</style>
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<thead>

<tr class="gt_col_headings">
  <th class="gt_col_heading gt_columns_bottom_border gt_right" rowspan="1" colspan="1" scope="col" id="year">year</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="title">title</th>
  <th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="run_time">run_time</th>
</tr>
</thead>
<tbody class="gt_table_body">
  <tr>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_right">1946</td>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_left">The Lovers</td>
    <td style="background-color: papayawhip; font-size: small;text-align: right;font-weight: normal;" class="gt_row gt_left">1h 30m</td>
  </tr>
  <tr>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_right gt_striped">1946</td>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_left gt_striped">Anna and the King of Siam</td>
    <td style="background-color: lightblue; font-size: x-large;text-align: center;font-weight: bold;" class="gt_row gt_left gt_striped">2h 8m</td>
  </tr>
  <tr>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_right">1946</td>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_left">Blood and Fire</td>
    <td style="background-color: lightgreen; font-size: medium;text-align: left;font-weight: lighter;" class="gt_row gt_left">1h 40m</td>
  </tr>
</tbody>


</table>

</div>
        
</div>
</div>
<p><strong>One last note:</strong> <code>from_column()</code> works with both Pandas and Polars DataFrames. For Polars users, you can also pass expressions directly without wrapping them in <code>from_column()</code>. The following code produces the same styled table as shown above:</p>
<div id="b26b0e3b" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">(</span>
<span id="cb4-2">    GT(df)</span>
<span id="cb4-3">    .tab_style(</span>
<span id="cb4-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb4-5">            style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)),</span>
<span id="cb4-6">            style.text(</span>
<span id="cb4-7">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>), weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"weight"</span>), align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"align"</span>)</span>
<span id="cb4-8">            ),</span>
<span id="cb4-9">        ],</span>
<span id="cb4-10">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(columns),</span>
<span id="cb4-11">    )</span>
<span id="cb4-12">    .cols_hide(df_style.columns)</span>
<span id="cb4-13">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray"</span>)</span>
<span id="cb4-14">)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>pandas</category>
  <category>polars</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/styling-tricks-table-body/20250526.html</guid>
  <pubDate>Mon, 26 May 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/styling-tricks-table-body/table_body.png" medium="image" type="image/png" height="49" width="144"/>
</item>
<item>
  <title>case_when() in Polars</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/casewhen-in-polars/20250525.html</link>
  <description><![CDATA[ 






<p>This is a follow-up to my <a href="../../posts/polars-custom-expr-namespace/20250417.html">previous post</a>.</p>
<p>While the conditional branching mechanism of <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.when.html">pl.when().then().otherwise()</a> is quite powerful, I often find it a bit verbose—especially when the conditions are complex. In those cases, it becomes harder to validate the correctness of each branch at a glance.</p>
<p>On the other hand, I find the <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.case_when.html">pd.Series.case_when()</a> pattern in Pandas slightly more concise and readable. However, I’ve always wished it supported a fallback mechanism like Polars’ <code>.otherwise()</code>.</p>
<p>In the end, I thought it would be interesting to borrow the concept behind <code>pd.Series.case_when()</code> and implement it as a standalone utility function in Polars.</p>
<section id="case_when" class="level2">
<h2 class="anchored" data-anchor-id="case_when"><code>case_when()</code></h2>
<p>The <code>case_when()</code> function accepts two arguments:</p>
<ul>
<li><code>caselist</code>: A list of two-element tuples, where the first item is the condition (used in <code>pl.when()</code>), and the second is the corresponding result expression (used in <code>.then()</code>).</li>
<li><code>otherwise</code>: A fallback expression used in <code>.otherwise()</code> if no conditions match.</li>
</ul>
<p>The given example demonstrates how <code>case_when()</code> can simplify conditional logic compared to the more verbose <code>pl.when().then().otherwise()</code> chain.</p>
<div id="93adb77d" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> functools <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> cache</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Any</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> case_when(</span>
<span id="cb1-8">    caselist: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>[pl.Expr, pl.Expr]], otherwise: pl.Expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb1-9">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Simplifies conditional logic in Polars by chaining multiple `when-then` expressions.</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Parameters</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ----------</span></span>
<span id="cb1-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    caselist</span></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        A list of (condition, value) pairs. Each condition is evaluated in order,</span></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        and the corresponding value is returned when a condition is met.</span></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    otherwise</span></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        The fallback value to use if none of the conditions match.</span></span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Returns</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    -------</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    pl.Expr</span></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Examples:</span></span>
<span id="cb1-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    -------</span></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ```python</span></span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    import polars as pl</span></span>
<span id="cb1-29"></span>
<span id="cb1-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    df = pl.DataFrame({"x": [1, 2, 3, 4]})</span></span>
<span id="cb1-31"></span>
<span id="cb1-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    expr = case_when(</span></span>
<span id="cb1-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        caselist=[</span></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            (pl.col("x") &lt; 2, pl.lit("small")),</span></span>
<span id="cb1-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            (pl.col("x") &lt; 4, pl.lit("medium"))</span></span>
<span id="cb1-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        ],</span></span>
<span id="cb1-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        otherwise=pl.lit("large"),</span></span>
<span id="cb1-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ).alias("size")</span></span>
<span id="cb1-39"></span>
<span id="cb1-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # This is equivalent to writing:</span></span>
<span id="cb1-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # expr = (</span></span>
<span id="cb1-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #     pl.when(pl.col("x") &lt; 2)</span></span>
<span id="cb1-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .then(pl.lit("small"))</span></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .when(pl.col("x") &lt; 4)</span></span>
<span id="cb1-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .then(pl.lit("medium"))</span></span>
<span id="cb1-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .otherwise(pl.lit("large"))</span></span>
<span id="cb1-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    #       .alias("size")</span></span>
<span id="cb1-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # )</span></span>
<span id="cb1-49"></span>
<span id="cb1-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    df.with_columns(expr)</span></span>
<span id="cb1-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ```</span></span>
<span id="cb1-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    shape: (4, 2)</span></span>
<span id="cb1-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ┌─────┬────────┐</span></span>
<span id="cb1-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ x   ┆ size   │</span></span>
<span id="cb1-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ --- ┆ ---    │</span></span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ i64 ┆ str    │</span></span>
<span id="cb1-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ├─────┼────────┤</span></span>
<span id="cb1-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 1   ┆ small  │</span></span>
<span id="cb1-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 2   ┆ medium │</span></span>
<span id="cb1-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 3   ┆ medium │</span></span>
<span id="cb1-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    │ 4   ┆ large  │</span></span>
<span id="cb1-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    └─────┴────────┘</span></span>
<span id="cb1-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-64">    (first_when, first_then), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>cases <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> caselist</span>
<span id="cb1-65"></span>
<span id="cb1-66">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first</span></span>
<span id="cb1-67">    expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.when(first_when).then(first_then)</span>
<span id="cb1-68"></span>
<span id="cb1-69">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># middles</span></span>
<span id="cb1-70">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> when, then <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cases:</span>
<span id="cb1-71">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.when(when).then(then)</span>
<span id="cb1-72"></span>
<span id="cb1-73">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last</span></span>
<span id="cb1-74">    expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.otherwise(otherwise)</span>
<span id="cb1-75"></span>
<span id="cb1-76">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> expr</span></code></pre></div></div>
</div>
</section>
<section id="custom-expression-namespace" class="level2">
<h2 class="anchored" data-anchor-id="custom-expression-namespace">Custom Expression Namespace</h2>
<p>With <code>case_when()</code> in place, we can refactor the <code>DiscreteSplitter</code> expression namespace like this:</p>
<div id="020fff9f" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@cache</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _mod_expr(n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pl.int_range(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.UInt32).mod(n)</span>
<span id="cb2-4"></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _litify(lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[pl.lit]:</span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [pl.lit(lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lits]</span>
<span id="cb2-8"></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb2-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb2-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, expr: pl.Expr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb2-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr</span>
<span id="cb2-14"></span>
<span id="cb2-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb2-16">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits)</span>
<span id="cb2-17">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _mod_expr(n)</span>
<span id="cb2-18">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>litified, litified_otherwise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _litify(lits)</span>
<span id="cb2-19">        caselist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(mod_expr.eq(i), lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(litified)]</span>
<span id="cb2-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> case_when(caselist, litified_otherwise).alias(name)</span>
<span id="cb2-21"></span>
<span id="cb2-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2], name)</span>
<span id="cb2-24"></span>
<span id="cb2-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb2-26">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, lit3: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb2-27">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2, lit3], name)</span>
<span id="cb2-29"></span>
<span id="cb2-30">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb2-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_expr(lits, name)</span></code></pre></div></div>
</div>
<p>Now, <code>bucketize()</code> is the primary method that encapsulates the core logic for categorical mapping. <code>binarize()</code> and <code>trinarize()</code> are just convenient wrappers for common cases.</p>
<p>Here’s a simple example of using the custom expression namespace:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb3-2">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb3-3">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb3-4">    .with_columns(</span>
<span id="cb3-5">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),</span>
<span id="cb3-6">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>),</span>
<span id="cb3-7">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]),</span>
<span id="cb3-8">    )</span>
<span id="cb3-9">)</span></code></pre></div></div>
<pre><code>shape: (9, 5)
┌───────┬─────┬────────────┬────────────┬────────────┐
│ index ┆ n   ┆ binarized  ┆ trinarized ┆ bucketized │
│ ---   ┆ --- ┆ ---        ┆ ---        ┆ ---        │
│ u32   ┆ i64 ┆ str        ┆ str        ┆ i32        │
╞═══════╪═════╪════════════╪════════════╪════════════╡
│ 1     ┆ 100 ┆ lightblue  ┆ one        ┆ 1          │
│ 2     ┆ 50  ┆ papayawhip ┆ two        ┆ 2          │
│ 3     ┆ 72  ┆ lightblue  ┆ three      ┆ 3          │
│ 4     ┆ 83  ┆ papayawhip ┆ one        ┆ 4          │
│ 5     ┆ 97  ┆ lightblue  ┆ two        ┆ 1          │
│ 6     ┆ 42  ┆ papayawhip ┆ three      ┆ 2          │
│ 7     ┆ 20  ┆ lightblue  ┆ one        ┆ 3          │
│ 8     ┆ 51  ┆ papayawhip ┆ two        ┆ 4          │
│ 9     ┆ 77  ┆ lightblue  ┆ three      ┆ 1          │
└───────┴─────┴────────────┴────────────┴────────────┘</code></pre>
</section>
<section id="custom-dataframe-namespace" class="level2">
<h2 class="anchored" data-anchor-id="custom-dataframe-namespace">Custom DataFrame Namespace</h2>
<p>Instead of relying on <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.with_row_index.html">pl.DataFrame.with_row_index()</a>, we can also use <code>_mod_expr()</code> directly to enable similar categorization.</p>
<p>Here’s how the <code>DiscreteSplitter</code> can be implemented as a custom DataFrame namespace:</p>
<div id="d7e9fc51" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_dataframe_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb5-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, df: pl.DataFrame) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb5-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df</span>
<span id="cb5-5"></span>
<span id="cb5-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>):</span>
<span id="cb5-7">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits)</span>
<span id="cb5-8">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _mod_expr(n)</span>
<span id="cb5-9">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>litified, litified_otherwise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _litify(lits)</span>
<span id="cb5-10">        caselist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(mod_expr.eq(i), lit) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(litified)]</span>
<span id="cb5-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> case_when(caselist, litified_otherwise).alias(name)</span>
<span id="cb5-12"></span>
<span id="cb5-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_final_df(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-14">        cls <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>)</span>
<span id="cb5-15">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_expr(lits, name)</span>
<span id="cb5-16">        new_spt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cls(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df.with_columns(expr))</span>
<span id="cb5-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> new_spt._df</span>
<span id="cb5-18"></span>
<span id="cb5-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>name)</span>
<span id="cb5-21"></span>
<span id="cb5-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb5-23">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: Any, lit2: Any, lit3: Any, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb5-24">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.bucketize([lit1, lit2, lit3], name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>name)</span>
<span id="cb5-26"></span>
<span id="cb5-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb5-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_final_df(lits, name)</span></code></pre></div></div>
</div>
<p>Example usage:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb6-2">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb6-3">    .spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>)</span>
<span id="cb6-4">    .spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>)</span>
<span id="cb6-5">    .spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb6-6">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-7">)</span></code></pre></div></div>
<pre><code>shape: (9, 5)
┌───────┬─────┬────────────┬────────────┬────────────┐
│ index ┆ n   ┆ binarized  ┆ trinarized ┆ bucketized │
│ ---   ┆ --- ┆ ---        ┆ ---        ┆ ---        │
│ u32   ┆ i64 ┆ str        ┆ str        ┆ i32        │
╞═══════╪═════╪════════════╪════════════╪════════════╡
│ 1     ┆ 100 ┆ lightblue  ┆ one        ┆ 1          │
│ 2     ┆ 50  ┆ papayawhip ┆ two        ┆ 2          │
│ 3     ┆ 72  ┆ lightblue  ┆ three      ┆ 3          │
│ 4     ┆ 83  ┆ papayawhip ┆ one        ┆ 4          │
│ 5     ┆ 97  ┆ lightblue  ┆ two        ┆ 1          │
│ 6     ┆ 42  ┆ papayawhip ┆ three      ┆ 2          │
│ 7     ┆ 20  ┆ lightblue  ┆ one        ┆ 3          │
│ 8     ┆ 51  ┆ papayawhip ┆ two        ┆ 4          │
│ 9     ┆ 77  ┆ lightblue  ┆ three      ┆ 1          │
└───────┴─────┴────────────┴────────────┴────────────┘</code></pre>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Extracting the conditional logic into a standalone <code>case_when()</code> function turned out to be both a practical and satisfying exercise—perfect for a rainy afternoon of coding. It not only improves readability but also makes the branching logic easier to reuse and reason about.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <guid>https://tech.ycwu.space/posts/casewhen-in-polars/20250525.html</guid>
  <pubDate>Sun, 25 May 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/casewhen-in-polars/code.png" medium="image" type="image/png" height="90" width="144"/>
</item>
<item>
  <title>Custom Row Selector in Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/gt-row-selector/20250511.html</link>
  <description><![CDATA[ 






<p>This short post shows how we can create custom row selectors in Great Tables by leveraging the row index. While <a href="https://github.com/posit-dev/great-tables/issues/678">it</a> may or may not be adopted by the team, I thought it would be fun to document it here on the blog.</p>
<p>I recently created a utility called <a href="https://github.com/posit-dev/great-tables/compare/main...jrycw:great-tables:feat-every_n_row">every_n_row()</a>, designed to work with both Pandas and Polars DataFrames (support for <code>pyarrow</code> is still under investigation). With <code>every_n_row()</code>, we can easily target alternating rows—for example, select odd rows using <code>every_n_row(2)</code> and even rows using either <code>every_n_row(2, 1)</code> or <code>~every_n_row(2)</code>.</p>
<div id="1c1ebd40" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, every_n_row, loc, style</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> countrypops</span>
<span id="cb1-5"></span>
<span id="cb1-6">df_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> countrypops.sample(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>).loc[:, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country_name"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>]]</span>
<span id="cb1-7">df_pl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.from_pandas(df_pd)</span>
<span id="cb1-8"></span>
<span id="cb1-9">(</span>
<span id="cb1-10">    GT(df_pd)</span>
<span id="cb1-11">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>every_n_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb1-12">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>every_n_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb1-13">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb1-14">)</span>
<span id="cb1-15"></span>
<span id="cb1-16">(</span>
<span id="cb1-17">    GT(df_pl)</span>
<span id="cb1-18">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>every_n_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb1-19">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=~</span>every_n_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb1-20">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb1-21">)</span></code></pre></div></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/gt-row-selector/every_n_row.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Row Selector: every_n_row"></p>
</figure>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>pandas</category>
  <category>polars</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/gt-row-selector/20250511.html</guid>
  <pubDate>Sun, 11 May 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/gt-row-selector/every_n_row.png" medium="image" type="image/png" height="112" width="144"/>
</item>
<item>
  <title>Polars Custom Expression Namespace</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/polars-custom-expr-namespace/20250417.html</link>
  <description><![CDATA[ 






<p>Today I explored how to register a <strong>custom expression namespace</strong> in Polars. This feature turned out to be super helpful for solving a common problem I run into when building tables or plots—<strong>colorizing rows based on their row index</strong>.</p>
<p>Here is the code snippet: <img src="https://tech.ycwu.space/posts/polars-custom-expr-namespace/code.png" class="img-fluid quarto-figure quarto-figure-center" alt="Code Snippet"></p>
<div id="024ad37d" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Any</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb1-5"></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb1-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, expr: pl.Expr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb1-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr</span>
<span id="cb1-11"></span>
<span id="cb1-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _mod_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pl.int_range(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.UInt32).mod(n)</span>
<span id="cb1-14"></span>
<span id="cb1-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(</span>
<span id="cb1-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span></span>
<span id="cb1-17">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-18">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-20">            pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-21">            .then(pl.lit(lit1))</span>
<span id="cb1-22">            .otherwise(pl.lit(lit2))</span>
<span id="cb1-23">            .alias(name)</span>
<span id="cb1-24">        )</span>
<span id="cb1-25"></span>
<span id="cb1-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb1-27">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit3: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb1-28">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-29">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-31">            pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-32">            .then(pl.lit(lit1))</span>
<span id="cb1-33">            .when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-34">            .then(pl.lit(lit2))</span>
<span id="cb1-35">            .otherwise(pl.lit(lit3))</span>
<span id="cb1-36">            .alias(name)</span>
<span id="cb1-37">        )</span>
<span id="cb1-38"></span>
<span id="cb1-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(</span>
<span id="cb1-40">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span></span>
<span id="cb1-41">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-42">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits))</span>
<span id="cb1-43"></span>
<span id="cb1-44">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first</span></span>
<span id="cb1-45">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)).then(pl.lit(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]))</span>
<span id="cb1-46"></span>
<span id="cb1-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># middles</span></span>
<span id="cb1-48">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, one_lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb1-49">            expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.when(mod_expr.eq(i)).then(pl.lit(one_lit))</span>
<span id="cb1-50"></span>
<span id="cb1-51">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last</span></span>
<span id="cb1-52">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.otherwise(pl.lit(lits[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb1-53">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> expr.alias(name)</span>
<span id="cb1-54"></span>
<span id="cb1-55"></span>
<span id="cb1-56">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-57">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb1-58">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-59">    .with_columns(</span>
<span id="cb1-60">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),</span>
<span id="cb1-61">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>),</span>
<span id="cb1-62">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]),</span>
<span id="cb1-63">    )</span>
<span id="cb1-64">)</span>
<span id="cb1-65"></span>
<span id="cb1-66">(</span>
<span id="cb1-67">    GT(df)</span>
<span id="cb1-68">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb1-69">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb1-70">)</span></code></pre></div></div>
</details>
</div>
<section id="step-by-step-breakdown" class="level2">
<h2 class="anchored" data-anchor-id="step-by-step-breakdown">Step-by-Step Breakdown</h2>
<section id="registering-the-namespace" class="level3">
<h3 class="anchored" data-anchor-id="registering-the-namespace">Registering the Namespace</h3>
<p>We use <a href="https://docs.pola.rs/api/python/stable/reference/api/polars.api.register_expr_namespace.html">pl.api.register_expr_namespace()</a> to attach our class to the <code>spt</code> namespace. Once registered, we can call our methods like this: <code>pl.col("any").spt.binarize(...)</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb2-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, expr: pl.Expr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb2-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr</span></code></pre></div></div>
</section>
<section id="helper-row-index-based-modulo-expression" class="level3">
<h3 class="anchored" data-anchor-id="helper-row-index-based-modulo-expression">Helper: Row-Index Based Modulo Expression</h3>
<p>To assign values based on row position, we need a way to refer to the row index inside an expression. Polars provides a trick using <code>pl.int_range(pl.len())</code>, as shown in <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.with_row_index.html">pl.DataFrame.with_row_index()</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb3-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _mod_expr(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb3-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pl.int_range(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(), dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.UInt32).mod(n)</span></code></pre></div></div>
</section>
<section id="binarize-two-groups" class="level3">
<h3 class="anchored" data-anchor-id="binarize-two-groups"><code>binarize</code>: Two Groups</h3>
<p>This method maps alternating rows into two categories using <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.when.html#polars.when">pl.when().then().otherwise()</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb4-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(</span>
<span id="cb4-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span></span>
<span id="cb4-5">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb4-6">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb4-8">            pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb4-9">            .then(pl.lit(lit1))</span>
<span id="cb4-10">            .otherwise(pl.lit(lit2))</span>
<span id="cb4-11">            .alias(name)</span>
<span id="cb4-12">        )</span></code></pre></div></div>
</section>
<section id="trinarize-three-groups" class="level3">
<h3 class="anchored" data-anchor-id="trinarize-three-groups"><code>trinarize</code>: Three Groups</h3>
<p>Similar to <code>binarize</code>, but splits the rows into three groups using two <code>when().then()</code> branches before the final <code>otherwise()</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb5-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb5-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit3: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb5-5">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb5-6">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb5-8">            pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb5-9">            .then(pl.lit(lit1))</span>
<span id="cb5-10">            .when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb5-11">            .then(pl.lit(lit2))</span>
<span id="cb5-12">            .otherwise(pl.lit(lit3))</span>
<span id="cb5-13">            .alias(name)</span>
<span id="cb5-14">        )</span></code></pre></div></div>
</section>
<section id="bucketize-n-groups" class="level3">
<h3 class="anchored" data-anchor-id="bucketize-n-groups"><code>bucketize</code>: N Groups</h3>
<p>A generalized version of the above, which dynamically assigns values from a list across <code>n</code> groups:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_expr_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb6-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(</span>
<span id="cb6-4">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span></span>
<span id="cb6-5">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb6-6">        mod_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_expr(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits))</span>
<span id="cb6-7"></span>
<span id="cb6-8">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first</span></span>
<span id="cb6-9">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.when(mod_expr.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)).then(pl.lit(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]))</span>
<span id="cb6-10"></span>
<span id="cb6-11">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># middles</span></span>
<span id="cb6-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, one_lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb6-13">            expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.when(mod_expr.eq(i)).then(pl.lit(one_lit))</span>
<span id="cb6-14"></span>
<span id="cb6-15">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last</span></span>
<span id="cb6-16">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.otherwise(pl.lit(lits[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb6-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> expr.alias(name)</span></code></pre></div></div>
</section>
<section id="example-usage" class="level3">
<h3 class="anchored" data-anchor-id="example-usage">Example Usage</h3>
<p>Here’s a simple example that demonstrates how the custom namespace works in practice:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb7-2">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb7-3">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-4">    .with_columns(</span>
<span id="cb7-5">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),</span>
<span id="cb7-6">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>),</span>
<span id="cb7-7">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]),</span>
<span id="cb7-8">    )</span>
<span id="cb7-9">)</span></code></pre></div></div>
<p>This produces the following DataFrame:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb8-2">┌───────┬─────┬────────────┬────────────┬────────────┐</span>
<span id="cb8-3">│ index ┆ n   ┆ binarized  ┆ trinarized ┆ bucketized │</span>
<span id="cb8-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>   ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span> ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>        ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>        ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>        │</span>
<span id="cb8-5">│ u32   ┆ i64 ┆ <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>        ┆ <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>        ┆ i32        │</span>
<span id="cb8-6">╞═══════╪═════╪════════════╪════════════╪════════════╡</span>
<span id="cb8-7">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> ┆ lightblue  ┆ one        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>          │</span>
<span id="cb8-8">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>  ┆ papayawhip ┆ two        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>          │</span>
<span id="cb8-9">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>  ┆ lightblue  ┆ three      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>          │</span>
<span id="cb8-10">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>  ┆ papayawhip ┆ one        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>          │</span>
<span id="cb8-11">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>  ┆ lightblue  ┆ two        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>          │</span>
<span id="cb8-12">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>  ┆ papayawhip ┆ three      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>          │</span>
<span id="cb8-13">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>  ┆ lightblue  ┆ one        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>          │</span>
<span id="cb8-14">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>  ┆ papayawhip ┆ two        ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>          │</span>
<span id="cb8-15">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>     ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>  ┆ lightblue  ┆ three      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>          │</span>
<span id="cb8-16">└───────┴─────┴────────────┴────────────┴────────────┘</span></code></pre></div></div>
<p>Note: Since the custom logic is based on the row index rather than actual column values, you can safely use <code>pl.col("")</code> as a placeholder when calling the namespace methods.</p>
<p>Each new column shows how rows are grouped using the row index modulo 2, 3, or 4—useful for highlighting patterns or applying styling.</p>
<p>For instance, you can use the <code>binarized</code> column with Great Tables like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">(</span>
<span id="cb9-2">    GT(df)</span>
<span id="cb9-3">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb9-4">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb9-5">)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/polars-custom-expr-namespace/gt.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Table with alternating row colors"></p>
</figure>
</div>
</section>
<section id="conclusion" class="level3">
<h3 class="anchored" data-anchor-id="conclusion">Conclusion</h3>
<p>Registering a custom expression namespace in Polars is a powerful way to encapsulate and reuse logic across your codebase. In this post, we created a <code>DiscreteSplitter</code> class to simplify index-based grouping, enabling operations like <code>binarize</code>, <code>trinarize</code>, and <code>bucketize</code>. This approach keeps your expressions clean and composable, especially when generating tables or plots that require styling based on row position.</p>
<p>It’s also worth noting that Polars supports similar registration for Series, LazyFrame, and DataFrame objects—check out the <a href="https://docs.pola.rs/api/python/stable/reference/api.html">official documentation</a> for more details.</p>
</section>
<section id="remark" class="level3">
<h3 class="anchored" data-anchor-id="remark">Remark</h3>
<p>Here’s a rough draft showing how to achieve a similar effect using the DataFrame namespace. I might revisit and refine this approach in the future.</p>
<div id="1d617c12" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Any</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb10-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, loc, style</span>
<span id="cb10-5"></span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@pl.api.register_dataframe_namespace</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"spt"</span>)</span>
<span id="cb10-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> DiscreteSplitter:</span>
<span id="cb10-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, df: pl.DataFrame) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb10-10">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df</span>
<span id="cb10-11">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mod"</span></span>
<span id="cb10-12">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.col(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_colname)</span>
<span id="cb10-13">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index"</span></span>
<span id="cb10-14">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_col <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.col(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_colname)</span>
<span id="cb10-15"></span>
<span id="cb10-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_df(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb10-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df.with_row_index(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_colname).with_columns(</span>
<span id="cb10-18">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_col.mod(n).alias(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_colname)</span>
<span id="cb10-19">        )</span>
<span id="cb10-20"></span>
<span id="cb10-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> _get_final_df(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, n: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, expr: pl.Expr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb10-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb10-23">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_df(n)</span>
<span id="cb10-24">            .with_columns(expr)</span>
<span id="cb10-25">            .drop([<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._idx_colname, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_colname])</span>
<span id="cb10-26">        )</span>
<span id="cb10-27"></span>
<span id="cb10-28">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> binarize(</span>
<span id="cb10-29">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span></span>
<span id="cb10-30">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb10-31">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb10-32"></span>
<span id="cb10-33">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb10-34">            pl.when(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb10-35">            .then(pl.lit(lit1))</span>
<span id="cb10-36">            .otherwise(pl.lit(lit2))</span>
<span id="cb10-37">            .alias(name)</span>
<span id="cb10-38">        )</span>
<span id="cb10-39"></span>
<span id="cb10-40">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_final_df(n, expr)</span>
<span id="cb10-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df</span>
<span id="cb10-42"></span>
<span id="cb10-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> trinarize(</span>
<span id="cb10-44">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lit1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, lit3: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trinarized"</span></span>
<span id="cb10-45">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb10-46">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb10-47"></span>
<span id="cb10-48">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb10-49">            pl.when(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb10-50">            .then(pl.lit(lit1))</span>
<span id="cb10-51">            .when(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb10-52">            .then(pl.lit(lit2))</span>
<span id="cb10-53">            .otherwise(pl.lit(lit3))</span>
<span id="cb10-54">            .alias(name)</span>
<span id="cb10-55">        )</span>
<span id="cb10-56"></span>
<span id="cb10-57">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_final_df(n, expr)</span>
<span id="cb10-58">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df</span>
<span id="cb10-59"></span>
<span id="cb10-60">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bucketize(</span>
<span id="cb10-61">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, lits: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[Any], name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bucketized"</span></span>
<span id="cb10-62">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb10-63">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(lits)</span>
<span id="cb10-64"></span>
<span id="cb10-65">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># first</span></span>
<span id="cb10-66">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.when(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col.eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)).then(pl.lit(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]))</span>
<span id="cb10-67"></span>
<span id="cb10-68">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># middles</span></span>
<span id="cb10-69">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, one_lit <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(lits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb10-70">            expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.when(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._mod_col.eq(i)).then(pl.lit(one_lit))</span>
<span id="cb10-71"></span>
<span id="cb10-72">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># last</span></span>
<span id="cb10-73">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.otherwise(pl.lit(lits[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb10-74"></span>
<span id="cb10-75">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alias</span></span>
<span id="cb10-76">        expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> expr.alias(name)</span>
<span id="cb10-77"></span>
<span id="cb10-78">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._get_final_df(n, expr)</span>
<span id="cb10-79">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._df</span>
<span id="cb10-80"></span>
<span id="cb10-81"></span>
<span id="cb10-82">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb10-83">    pl.DataFrame({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">83</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">51</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span>]})</span>
<span id="cb10-84">    .spt.binarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>)</span>
<span id="cb10-85">    .spt.trinarize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"three"</span>)</span>
<span id="cb10-86">    .spt.bucketize([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb10-87">    .with_row_index(offset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-88">)</span>
<span id="cb10-89"></span>
<span id="cb10-90">(</span>
<span id="cb10-91">    GT(df)</span>
<span id="cb10-92">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"binarized"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb10-93">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb10-94">)</span></code></pre></div></div>
</details>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <guid>https://tech.ycwu.space/posts/polars-custom-expr-namespace/20250417.html</guid>
  <pubDate>Thu, 17 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/polars-custom-expr-namespace/code.png" medium="image" type="image/png" height="205" width="144"/>
</item>
<item>
  <title>Clone the Reciprocal Tariffs Table Using Plotnine</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table-p9/20250416.html</link>
  <description><![CDATA[ 






<p>This post presents a recreated visualization using Polars and plotnine, based on my <a href="../../posts/clone-reciprocal-tariffs-table/20250403.html">earlier work</a>.</p>
<p>Below is the final figure: <img src="https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table-p9/reciprocal_tariffs_p9.png" class="img-fluid quarto-figure quarto-figure-center" alt="Cloned Reciprocal Tariffs Table"></p>
<div id="1abae546" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib.figure <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Figure</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (</span>
<span id="cb1-4">    aes,</span>
<span id="cb1-5">    element_blank,</span>
<span id="cb1-6">    element_rect,</span>
<span id="cb1-7">    element_text,</span>
<span id="cb1-8">    geom_segment,</span>
<span id="cb1-9">    geom_text,</span>
<span id="cb1-10">    ggplot,</span>
<span id="cb1-11">    position_nudge,</span>
<span id="cb1-12">    scale_color_identity,</span>
<span id="cb1-13">    scale_size_identity,</span>
<span id="cb1-14">    scale_y_discrete,</span>
<span id="cb1-15">    theme,</span>
<span id="cb1-16">    theme_void,</span>
<span id="cb1-17">    watermark,</span>
<span id="cb1-18">)</span>
<span id="cb1-19"></span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># source1: https://truthsocial.com/@realDonaldTrump/114270398531479278</span></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># source2:</span></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "https://upload.wikimedia.org/wikipedia/commons/</span></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># thumb/3/36/Seal_of_the_President_of_the_United_States.svg/</span></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 800px-Seal_of_the_President_of_the_United_States.svg.png"</span></span>
<span id="cb1-26">logo_filename <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logo_resized.png"</span></span>
<span id="cb1-27"></span>
<span id="cb1-28">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>: [</span>
<span id="cb1-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"China"</span>,</span>
<span id="cb1-31">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"European Union"</span>,</span>
<span id="cb1-32">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vietnam"</span>,</span>
<span id="cb1-33">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taiwan"</span>,</span>
<span id="cb1-34">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Japan"</span>,</span>
<span id="cb1-35">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"India"</span>,</span>
<span id="cb1-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"South Korea"</span>,</span>
<span id="cb1-37">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thailand"</span>,</span>
<span id="cb1-38">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Switzerland"</span>,</span>
<span id="cb1-39">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Indonesia"</span>,</span>
<span id="cb1-40">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Malaysia"</span>,</span>
<span id="cb1-41">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cambodia"</span>,</span>
<span id="cb1-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United Kingdom"</span>,</span>
<span id="cb1-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"South Africa"</span>,</span>
<span id="cb1-44">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brazil"</span>,</span>
<span id="cb1-45">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bangladesh"</span>,</span>
<span id="cb1-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Singapore"</span>,</span>
<span id="cb1-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Israel"</span>,</span>
<span id="cb1-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Philippines"</span>,</span>
<span id="cb1-49">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chile"</span>,</span>
<span id="cb1-50">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Australia"</span>,</span>
<span id="cb1-51">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pakistan"</span>,</span>
<span id="cb1-52">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Turkey"</span>,</span>
<span id="cb1-53">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sri Lanka"</span>,</span>
<span id="cb1-54">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Colombia"</span>,</span>
<span id="cb1-55">    ],</span>
<span id="cb1-56">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>: [</span>
<span id="cb1-57">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"67%"</span>,</span>
<span id="cb1-58">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"39%"</span>,</span>
<span id="cb1-59">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"90%"</span>,</span>
<span id="cb1-60">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"64%"</span>,</span>
<span id="cb1-61">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"46%"</span>,</span>
<span id="cb1-62">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"52%"</span>,</span>
<span id="cb1-63">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"50%"</span>,</span>
<span id="cb1-64">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"72%"</span>,</span>
<span id="cb1-65">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"61%"</span>,</span>
<span id="cb1-66">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"64%"</span>,</span>
<span id="cb1-67">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"47%"</span>,</span>
<span id="cb1-68">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"97%"</span>,</span>
<span id="cb1-69">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-70">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60%"</span>,</span>
<span id="cb1-71">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-72">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"74%"</span>,</span>
<span id="cb1-73">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-74">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"33%"</span>,</span>
<span id="cb1-75">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34%"</span>,</span>
<span id="cb1-76">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-77">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-78">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"58%"</span>,</span>
<span id="cb1-79">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-80">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"88%"</span>,</span>
<span id="cb1-81">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-82">    ],</span>
<span id="cb1-83">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>: [</span>
<span id="cb1-84">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34%"</span>,</span>
<span id="cb1-85">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20%"</span>,</span>
<span id="cb1-86">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"46%"</span>,</span>
<span id="cb1-87">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"32%"</span>,</span>
<span id="cb1-88">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"24%"</span>,</span>
<span id="cb1-89">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"26%"</span>,</span>
<span id="cb1-90">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"25%"</span>,</span>
<span id="cb1-91">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"36%"</span>,</span>
<span id="cb1-92">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31%"</span>,</span>
<span id="cb1-93">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"32%"</span>,</span>
<span id="cb1-94">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"24%"</span>,</span>
<span id="cb1-95">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"49%"</span>,</span>
<span id="cb1-96">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-97">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30%"</span>,</span>
<span id="cb1-98">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-99">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"37%"</span>,</span>
<span id="cb1-100">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-101">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"17%"</span>,</span>
<span id="cb1-102">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"17%"</span>,</span>
<span id="cb1-103">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-104">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-105">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"29%"</span>,</span>
<span id="cb1-106">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-107">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"44%"</span>,</span>
<span id="cb1-108">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-109">    ],</span>
<span id="cb1-110">}</span>
<span id="cb1-111"></span>
<span id="cb1-112">country, tariffs_charged, reciprocal_tariffs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.keys()</span>
<span id="cb1-113"></span>
<span id="cb1-114">dark_navy_blue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0B162A"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># background</span></span>
<span id="cb1-115">light_blue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#B5D3E7"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row</span></span>
<span id="cb1-116">white <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFFFF"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row</span></span>
<span id="cb1-117">yellow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F6D588"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "reciprocal_tariffs" column</span></span>
<span id="cb1-118">gold <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFF8DE"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logo</span></span>
<span id="cb1-119"></span>
<span id="cb1-120">fontname_georgia <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Georgia"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># title</span></span>
<span id="cb1-121">fontname_roboto <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Roboto"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># body</span></span>
<span id="cb1-122"></span>
<span id="cb1-123"></span>
<span id="cb1-124"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tweak_df() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb1-125">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column width</span></span>
<span id="cb1-126">    x_col1_start, x_col1_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">52.5</span></span>
<span id="cb1-127">    x_col2_start, x_col2_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span></span>
<span id="cb1-128">    x_col3_start, x_col3_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">82.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">97.5</span></span>
<span id="cb1-129"></span>
<span id="cb1-130">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x-position for body text</span></span>
<span id="cb1-131">    x_col1_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb1-132">    x_col2_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x_col2_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (x_col2_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_col2_start) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-133">    x_col3_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x_col3_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (x_col3_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_col3_start) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-134"></span>
<span id="cb1-135">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-136">        pl.DataFrame(data)</span>
<span id="cb1-137">        .with_row_index()</span>
<span id="cb1-138">        .with_columns(</span>
<span id="cb1-139">            pl.col(country).cast(pl.Categorical),</span>
<span id="cb1-140">            pl.when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index"</span>).mod(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>).eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-141">            .then(pl.lit(light_blue))</span>
<span id="cb1-142">            .otherwise(pl.lit(white))</span>
<span id="cb1-143">            .alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="cb1-144">            pl.lit(x_col1_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_start"</span>),</span>
<span id="cb1-145">            pl.lit(x_col1_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_end"</span>),</span>
<span id="cb1-146">            pl.lit(x_col2_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_start"</span>),</span>
<span id="cb1-147">            pl.lit(x_col2_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_end"</span>),</span>
<span id="cb1-148">            pl.lit(x_col3_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_start"</span>),</span>
<span id="cb1-149">            pl.lit(x_col3_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_end"</span>),</span>
<span id="cb1-150">            pl.lit(x_col1_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_text"</span>),</span>
<span id="cb1-151">            pl.lit(x_col2_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_text"</span>),</span>
<span id="cb1-152">            pl.lit(x_col3_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_text"</span>),</span>
<span id="cb1-153">        )</span>
<span id="cb1-154">    )</span>
<span id="cb1-155"></span>
<span id="cb1-156"></span>
<span id="cb1-157"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_textdata_df(x_ref: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, y_ref: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb1-158">    title_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb1-159">    title_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span id="cb1-160">    heading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span>
<span id="cb1-161">    heading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span id="cb1-162">    subheading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-163">    subheading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span></span>
<span id="cb1-164"></span>
<span id="cb1-165">    textdata_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb1-166">        {</span>
<span id="cb1-167">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>: [</span>
<span id="cb1-168">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reciprocal Tariffs"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># title</span></span>
<span id="cb1-169">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Country"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1</span></span>
<span id="cb1-170">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tariffs Charged"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2</span></span>
<span id="cb1-171">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to the U.S.A."</span>,</span>
<span id="cb1-172">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Including"</span>,</span>
<span id="cb1-173">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Currency Manipulation"</span>,</span>
<span id="cb1-174">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"and Trade Barriers"</span>,</span>
<span id="cb1-175">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U.S.A. Discounted"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3</span></span>
<span id="cb1-176">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reciprocal Tariffs"</span>,</span>
<span id="cb1-177">            ],</span>
<span id="cb1-178">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>: [</span>
<span id="cb1-179">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.0</span>,</span>
<span id="cb1-180">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.5</span>,</span>
<span id="cb1-181">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb1-182">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb1-183">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb1-184">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb1-185">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb1-186">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">89.5</span>,</span>
<span id="cb1-187">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">89.5</span>,</span>
<span id="cb1-188">            ],</span>
<span id="cb1-189">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>: [</span>
<span id="cb1-190">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>,</span>
<span id="cb1-191">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>,</span>
<span id="cb1-192">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.8</span>,</span>
<span id="cb1-193">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.4</span>,</span>
<span id="cb1-194">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.1</span>,</span>
<span id="cb1-195">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.8</span>,</span>
<span id="cb1-196">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>,</span>
<span id="cb1-197">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.0</span>,</span>
<span id="cb1-198">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.6</span>,</span>
<span id="cb1-199">            ],</span>
<span id="cb1-200">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [</span>
<span id="cb1-201">                gold,</span>
<span id="cb1-202">                white,</span>
<span id="cb1-203">                white,</span>
<span id="cb1-204">                white,</span>
<span id="cb1-205">                white,</span>
<span id="cb1-206">                white,</span>
<span id="cb1-207">                white,</span>
<span id="cb1-208">                white,</span>
<span id="cb1-209">                white,</span>
<span id="cb1-210">            ],</span>
<span id="cb1-211">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>: [</span>
<span id="cb1-212">                title_fontsize,</span>
<span id="cb1-213">                heading_fontsize,</span>
<span id="cb1-214">                heading_fontsize,</span>
<span id="cb1-215">                heading_fontsize,</span>
<span id="cb1-216">                subheading_fontsize,</span>
<span id="cb1-217">                subheading_fontsize,</span>
<span id="cb1-218">                subheading_fontsize,</span>
<span id="cb1-219">                heading_fontsize,</span>
<span id="cb1-220">                heading_fontsize,</span>
<span id="cb1-221">            ],</span>
<span id="cb1-222">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: [</span>
<span id="cb1-223">                title_fontweight,</span>
<span id="cb1-224">                heading_fontweight,</span>
<span id="cb1-225">                heading_fontweight,</span>
<span id="cb1-226">                heading_fontweight,</span>
<span id="cb1-227">                subheading_fontweight,</span>
<span id="cb1-228">                subheading_fontweight,</span>
<span id="cb1-229">                subheading_fontweight,</span>
<span id="cb1-230">                heading_fontweight,</span>
<span id="cb1-231">                heading_fontweight,</span>
<span id="cb1-232">            ],</span>
<span id="cb1-233">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontname"</span>: [</span>
<span id="cb1-234">                fontname_georgia,</span>
<span id="cb1-235">                fontname_georgia,</span>
<span id="cb1-236">                fontname_georgia,</span>
<span id="cb1-237">                fontname_georgia,</span>
<span id="cb1-238">                fontname_georgia,</span>
<span id="cb1-239">                fontname_georgia,</span>
<span id="cb1-240">                fontname_georgia,</span>
<span id="cb1-241">                fontname_georgia,</span>
<span id="cb1-242">                fontname_georgia,</span>
<span id="cb1-243">            ],</span>
<span id="cb1-244">        }</span>
<span id="cb1-245">    )</span>
<span id="cb1-246">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> textdata_df</span>
<span id="cb1-247"></span>
<span id="cb1-248"></span>
<span id="cb1-249"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_g() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb1-250">    geom_segment_props <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lineend"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>}</span>
<span id="cb1-251"></span>
<span id="cb1-252">    geom_text_props <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-253">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ha"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="cb1-254">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"va"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb1-255">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"position"</span>: position_nudge(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>),</span>
<span id="cb1-256">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb1-257">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb1-258">    }</span>
<span id="cb1-259"></span>
<span id="cb1-260">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-261">        ggplot(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df, mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country, yend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country))</span>
<span id="cb1-262">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1 segment</span></span>
<span id="cb1-263">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="cb1-264">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_end"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="cb1-265">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="cb1-266">        )</span>
<span id="cb1-267">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2 segment</span></span>
<span id="cb1-268">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="cb1-269">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_end"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="cb1-270">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="cb1-271">        )</span>
<span id="cb1-272">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3 segment</span></span>
<span id="cb1-273">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="cb1-274">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_end"</span>),</span>
<span id="cb1-275">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>yellow,</span>
<span id="cb1-276">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="cb1-277">        )</span>
<span id="cb1-278">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1 text</span></span>
<span id="cb1-279">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="cb1-280">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2 text</span></span>
<span id="cb1-281">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tariffs_charged), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="cb1-282">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3 text</span></span>
<span id="cb1-283">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>reciprocal_tariffs), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="cb1-284">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using "color_mod" column directly</span></span>
<span id="cb1-285">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_color_identity()</span>
<span id="cb1-286">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand extra space</span></span>
<span id="cb1-287">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_y_discrete(</span>
<span id="cb1-288">            limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df.select(country).reverse().to_series().to_list(),</span>
<span id="cb1-289">            expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>),</span>
<span id="cb1-290">        )</span>
<span id="cb1-291">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># title and headers</span></span>
<span id="cb1-292">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(</span>
<span id="cb1-293">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>textdata_df,</span>
<span id="cb1-294">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(</span>
<span id="cb1-295">                x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>,</span>
<span id="cb1-296">                y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>,</span>
<span id="cb1-297">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>,</span>
<span id="cb1-298">                color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>,</span>
<span id="cb1-299">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>,</span>
<span id="cb1-300">                fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>,</span>
<span id="cb1-301">                fontname<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontname"</span>,</span>
<span id="cb1-302">            ),</span>
<span id="cb1-303">            va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb1-304">            ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb1-305">        )</span>
<span id="cb1-306">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using "size" column directly</span></span>
<span id="cb1-307">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_size_identity()</span>
<span id="cb1-308">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logo</span></span>
<span id="cb1-309">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> watermark(logo_filename, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2235</span>)</span>
<span id="cb1-310">    )</span>
<span id="cb1-311"></span>
<span id="cb1-312"></span>
<span id="cb1-313"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> themify(p: ggplot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Figure:</span>
<span id="cb1-314">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-315">        p</span>
<span id="cb1-316">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme_void()</span>
<span id="cb1-317">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme(</span>
<span id="cb1-318">            legend_position<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turns off the legend</span></span>
<span id="cb1-319">            axis_text_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb1-320">            axis_text_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb1-321">            axis_title_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb1-322">            axis_title_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb1-323">            panel_background<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_rect(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb1-324">            plot_background<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_rect(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb1-325">            text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(family<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>fontname_roboto),</span>
<span id="cb1-326">            dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>,</span>
<span id="cb1-327">            figure_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb1-328">        )</span>
<span id="cb1-329">    ).draw(show<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-330"></span>
<span id="cb1-331"></span>
<span id="cb1-332">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tweak_df()</span>
<span id="cb1-333">textdata_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_textdata_df()</span>
<span id="cb1-334">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plot_g()</span>
<span id="cb1-335">fig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> themify(p)</span>
<span id="cb1-336">fig</span></code></pre></div></div>
</details>
</div>
<section id="preparing-the-dataframes" class="level2">
<h2 class="anchored" data-anchor-id="preparing-the-dataframes">Preparing the DataFrames</h2>
<section id="main-dataframe" class="level3">
<h3 class="anchored" data-anchor-id="main-dataframe">Main DataFrame</h3>
<p>We start by extracting the data from a previous project. The <code>country</code> column is converted to a categorical type, which simplifies handling in plotnine. To enable alternating row colors in the final visualization, we also create a new column called <code>color_mod</code>. Additional columns are created to define the positions for the segments and text labels.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tweak_df() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb2-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column width</span></span>
<span id="cb2-3">    x_col1_start, x_col1_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">52.5</span></span>
<span id="cb2-4">    x_col2_start, x_col2_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span></span>
<span id="cb2-5">    x_col3_start, x_col3_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">82.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">97.5</span></span>
<span id="cb2-6"></span>
<span id="cb2-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x-position for body text</span></span>
<span id="cb2-8">    x_col1_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb2-9">    x_col2_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x_col2_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (x_col2_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_col2_start) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-10">    x_col3_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x_col3_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (x_col3_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_col3_start) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-11"></span>
<span id="cb2-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb2-13">        pl.DataFrame(data)</span>
<span id="cb2-14">        .with_row_index()</span>
<span id="cb2-15">        .with_columns(</span>
<span id="cb2-16">            pl.col(country).cast(pl.Categorical),</span>
<span id="cb2-17">            pl.when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index"</span>).mod(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>).eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb2-18">            .then(pl.lit(light_blue))</span>
<span id="cb2-19">            .otherwise(pl.lit(white))</span>
<span id="cb2-20">            .alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="cb2-21">            pl.lit(x_col1_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_start"</span>),</span>
<span id="cb2-22">            pl.lit(x_col1_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_end"</span>),</span>
<span id="cb2-23">            pl.lit(x_col2_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_start"</span>),</span>
<span id="cb2-24">            pl.lit(x_col2_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_end"</span>),</span>
<span id="cb2-25">            pl.lit(x_col3_start).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_start"</span>),</span>
<span id="cb2-26">            pl.lit(x_col3_end).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_end"</span>),</span>
<span id="cb2-27">            pl.lit(x_col1_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_text"</span>),</span>
<span id="cb2-28">            pl.lit(x_col2_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_text"</span>),</span>
<span id="cb2-29">            pl.lit(x_col3_text).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_text"</span>),</span>
<span id="cb2-30">        )</span>
<span id="cb2-31">    )</span>
<span id="cb2-32"></span>
<span id="cb2-33">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tweak_df()</span></code></pre></div></div>
</section>
<section id="textdata-dataframe" class="level3">
<h3 class="anchored" data-anchor-id="textdata-dataframe">Textdata DataFrame</h3>
<p>Next, we create another DataFrame that contains information for all the text elements, such as text color, font size, and font weight, for the title and headers.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_textdata_df(</span>
<span id="cb3-2">    x_ref: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, y_ref: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb3-3">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb3-4">    title_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb3-5">    title_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span id="cb3-6">    heading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span>
<span id="cb3-7">    heading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span id="cb3-8">    subheading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb3-9">    subheading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span></span>
<span id="cb3-10"></span>
<span id="cb3-11">    textdata_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb3-12">        {</span>
<span id="cb3-13">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>: [</span>
<span id="cb3-14">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reciprocal Tariffs"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># title</span></span>
<span id="cb3-15">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Country"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1</span></span>
<span id="cb3-16">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tariffs Charged"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2</span></span>
<span id="cb3-17">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to the U.S.A."</span>,</span>
<span id="cb3-18">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Including"</span>,</span>
<span id="cb3-19">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Currency Manipulation"</span>,</span>
<span id="cb3-20">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"and Trade Barriers"</span>,</span>
<span id="cb3-21">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U.S.A. Discounted"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3</span></span>
<span id="cb3-22">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reciprocal Tariffs"</span>,</span>
<span id="cb3-23">            ],</span>
<span id="cb3-24">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>: [</span>
<span id="cb3-25">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.0</span>,</span>
<span id="cb3-26">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.5</span>,</span>
<span id="cb3-27">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb3-28">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb3-29">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb3-30">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb3-31">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">67.5</span>,</span>
<span id="cb3-32">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">89.5</span>,</span>
<span id="cb3-33">                x_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">89.5</span>,</span>
<span id="cb3-34">            ],</span>
<span id="cb3-35">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>: [</span>
<span id="cb3-36">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>,</span>
<span id="cb3-37">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>,</span>
<span id="cb3-38">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.8</span>,</span>
<span id="cb3-39">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.4</span>,</span>
<span id="cb3-40">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.1</span>,</span>
<span id="cb3-41">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.8</span>,</span>
<span id="cb3-42">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.5</span>,</span>
<span id="cb3-43">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">26.0</span>,</span>
<span id="cb3-44">                y_ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.6</span>,</span>
<span id="cb3-45">            ],</span>
<span id="cb3-46">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [</span>
<span id="cb3-47">                gold,</span>
<span id="cb3-48">                white,</span>
<span id="cb3-49">                white,</span>
<span id="cb3-50">                white,</span>
<span id="cb3-51">                white,</span>
<span id="cb3-52">                white,</span>
<span id="cb3-53">                white,</span>
<span id="cb3-54">                white,</span>
<span id="cb3-55">                white,</span>
<span id="cb3-56">            ],</span>
<span id="cb3-57">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>: [</span>
<span id="cb3-58">                title_fontsize,</span>
<span id="cb3-59">                heading_fontsize,</span>
<span id="cb3-60">                heading_fontsize,</span>
<span id="cb3-61">                heading_fontsize,</span>
<span id="cb3-62">                subheading_fontsize,</span>
<span id="cb3-63">                subheading_fontsize,</span>
<span id="cb3-64">                subheading_fontsize,</span>
<span id="cb3-65">                heading_fontsize,</span>
<span id="cb3-66">                heading_fontsize,</span>
<span id="cb3-67">            ],</span>
<span id="cb3-68">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: [</span>
<span id="cb3-69">                title_fontweight,</span>
<span id="cb3-70">                heading_fontweight,</span>
<span id="cb3-71">                heading_fontweight,</span>
<span id="cb3-72">                heading_fontweight,</span>
<span id="cb3-73">                subheading_fontweight,</span>
<span id="cb3-74">                subheading_fontweight,</span>
<span id="cb3-75">                subheading_fontweight,</span>
<span id="cb3-76">                heading_fontweight,</span>
<span id="cb3-77">                heading_fontweight,</span>
<span id="cb3-78">            ],</span>
<span id="cb3-79">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontname"</span>: [</span>
<span id="cb3-80">                fontname_georgia,</span>
<span id="cb3-81">                fontname_georgia,</span>
<span id="cb3-82">                fontname_georgia,</span>
<span id="cb3-83">                fontname_georgia,</span>
<span id="cb3-84">                fontname_georgia,</span>
<span id="cb3-85">                fontname_georgia,</span>
<span id="cb3-86">                fontname_georgia,</span>
<span id="cb3-87">                fontname_georgia,</span>
<span id="cb3-88">                fontname_georgia,</span>
<span id="cb3-89">            ],</span>
<span id="cb3-90">        }</span>
<span id="cb3-91">    )</span>
<span id="cb3-92">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> textdata_df</span>
<span id="cb3-93"></span>
<span id="cb3-94">textdata_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_textdata_df()</span></code></pre></div></div>
</section>
</section>
<section id="constructing-the-ggplot-object" class="level2">
<h2 class="anchored" data-anchor-id="constructing-the-ggplot-object">Constructing the ggplot Object</h2>
<p>With the processed DataFrame ready, we can now build the <a href="https://plotnine.org/reference/ggplot.html#plotnine.ggplot">ggplot()</a> object:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="annotated-cell-4" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><span id="annotated-cell-4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_g() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="annotated-cell-4-2">    geom_segment_props <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lineend"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>}</span>
<span id="annotated-cell-4-3"></span>
<span id="annotated-cell-4-4">    geom_text_props <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="annotated-cell-4-5">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ha"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="annotated-cell-4-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"va"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="annotated-cell-4-7">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"position"</span>: position_nudge(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.08</span>),</span>
<span id="annotated-cell-4-8">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="annotated-cell-4-9">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="annotated-cell-4-10">    }</span>
<span id="annotated-cell-4-11"></span>
<span id="annotated-cell-4-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="annotated-cell-4-13">        ggplot(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df, mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country, yend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country))</span>
<span id="annotated-cell-4-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1 segment</span></span>
<span id="annotated-cell-4-15">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="annotated-cell-4-16">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_end"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="annotated-cell-4-17">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="annotated-cell-4-18">        )</span>
<span id="annotated-cell-4-19">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2 segment</span></span>
<span id="annotated-cell-4-20">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="annotated-cell-4-21">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_end"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color_mod"</span>),</span>
<span id="annotated-cell-4-22">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="annotated-cell-4-23">        )</span>
<span id="annotated-cell-4-24">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3 segment</span></span>
<span id="annotated-cell-4-25">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="annotated-cell-4-26">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_start"</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_end"</span>),</span>
<span id="annotated-cell-4-27">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>yellow,</span>
<span id="annotated-cell-4-28">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_segment_props,</span>
<span id="annotated-cell-4-29">        )</span>
<span id="annotated-cell-4-30">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col1 text</span></span>
<span id="annotated-cell-4-31">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col1_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>country), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="annotated-cell-4-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col2 text</span></span>
<span id="annotated-cell-4-33">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col2_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tariffs_charged), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="annotated-cell-4-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># col3 text</span></span>
<span id="annotated-cell-4-35">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x_col3_text"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>reciprocal_tariffs), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>geom_text_props)</span>
<span id="annotated-cell-4-36">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using "color_mod" column directly</span></span>
<span id="annotated-cell-4-37">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_color_identity()</span>
<span id="annotated-cell-4-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># expand extra space</span></span>
<span id="annotated-cell-4-39">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_y_discrete(</span>
<span id="annotated-cell-4-40">            limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df.select(country).reverse().to_series().to_list(),</span>
<span id="annotated-cell-4-41">            expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>),</span>
<span id="annotated-cell-4-42">        )</span>
<span id="annotated-cell-4-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># title and headers</span></span>
<span id="annotated-cell-4-44">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-4-45" class="code-annotation-target">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>textdata_df,</span>
<span id="annotated-cell-4-46">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(</span>
<span id="annotated-cell-4-47">                x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>,</span>
<span id="annotated-cell-4-48">                y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>,</span>
<span id="annotated-cell-4-49">                label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>,</span>
<span id="annotated-cell-4-50">                color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>,</span>
<span id="annotated-cell-4-51">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>,</span>
<span id="annotated-cell-4-52">                fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>,</span>
<span id="annotated-cell-4-53">                fontname<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontname"</span>,</span>
<span id="annotated-cell-4-54">            ),</span>
<span id="annotated-cell-4-55">            va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="annotated-cell-4-56">            ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="annotated-cell-4-57">        )</span>
<span id="annotated-cell-4-58">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># using "size" column directly</span></span>
<span id="annotated-cell-4-59">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_size_identity()</span>
<span id="annotated-cell-4-60">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logo</span></span>
<span id="annotated-cell-4-61">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> watermark(logo_filename, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2235</span>)</span>
<span id="annotated-cell-4-62">    )</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="45" data-code-annotation="1">The <code>data=</code> argument is set to <code>textdata_df</code>, which contains all the custom label and styling details for the title and column headers.</span>
</dd>
</dl>
<section id="whats-happening-here" class="level3">
<h3 class="anchored" data-anchor-id="whats-happening-here">What’s happening here?</h3>
<ol type="1">
<li><a href="https://plotnine.org/reference/geom_segment.html#plotnine.geom_segment">geom_segment()</a>: Since I couldn’t find a way to apply border radius, I used <code>geom_segment()</code> with <code>lineend="round"</code> as the best available workaround. Thick lines serve as cell backgrounds.</li>
<li><a href="https://plotnine.org/reference/geom_text.html#plotnine.geom_text">geom_text()</a>: Adds text for each column.</li>
<li><a href="https://plotnine.org/reference/scale_color_identity.html#plotnine.scale_color_identity">scale_color_identity()</a>: Uses the color values directly from the <code>color_mod</code> column, without applying a scale.</li>
<li><a href="https://plotnine.org/reference/scale_y_discrete.html#plotnine.scale_y_discrete">scale_y_discrete()</a>: Reorders the <code>country</code> axis and tweaks padding to add space above and below the table.</li>
<li><a href="https://plotnine.org/reference/scale_size_identity.html#plotnine.scale_size_identity">scale_size_identity()</a>: Similar to color scaling, this instructs plotnine to use the font sizes specified in the <code>fontsize</code> column without transformation.</li>
<li><a href="https://plotnine.org/reference/watermark.html#plotnine.watermark">watermark()</a>: Embeds a logo. Since there’s no native figure size parameter in plotnine, I manually scaled the output.</li>
</ol>
</section>
</section>
<section id="custom-theme" class="level2">
<h2 class="anchored" data-anchor-id="custom-theme">Custom Theme</h2>
<p>We apply a tailored theme with <code>themify()</code> to refine the figure’s appearance:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> themify(p: ggplot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Figure:</span>
<span id="cb4-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb4-3">        p</span>
<span id="cb4-4">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme_void()</span>
<span id="cb4-5">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme(</span>
<span id="cb4-6">            legend_position<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turns off the legend</span></span>
<span id="cb4-7">            axis_text_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb4-8">            axis_text_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb4-9">            axis_title_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb4-10">            axis_title_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb4-11">            panel_background<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_rect(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb4-12">            plot_background<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_rect(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb4-13">            text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(family<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>fontname_roboto),</span>
<span id="cb4-14">            dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>,</span>
<span id="cb4-15">            figure_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),</span>
<span id="cb4-16">        )</span>
<span id="cb4-17">    ).draw(show<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
</section>
<section id="final-rendering" class="level2">
<h2 class="anchored" data-anchor-id="final-rendering">Final Rendering</h2>
<p>Now, let’s tie it all together:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plot_g()</span>
<span id="cb5-2">fig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> themify(p)</span>
<span id="cb5-3">fig</span></code></pre></div></div>
</section>
<section id="closing-thoughts" class="level2">
<h2 class="anchored" data-anchor-id="closing-thoughts">Closing Thoughts</h2>
<p>This post showcases how plotnine can be used to create table-like visualizations. I’m genuinely impressed by its capabilities — it’s surprisingly fun to approach a table as a figure.</p>
<p>Throughout this exploration, I learned a lot from <a href="https://github.com/123-fake-st/2024_plotnine_contest">this repository</a>, which won the Plotnine Contest 2024. It’s a fantastic example of what’s possible with the library.</p>
<p>It would be exciting to explore how plotnine and Great Tables might work together to enable even richer visual storytelling — I’m looking forward to diving into that next.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<ol type="1">
<li>This table is intended as a self-practice project, and the data in the table may not be 100% accurate. Please refer to the <a href="https://truthsocial.com/@realDonaldTrump/114270398531479278">original source</a> if you require verified data.</li>
<li>This post was drafted by me, with AI assistance to refine the content.</li>
</ol>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>plotnine</category>
  <guid>https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table-p9/20250416.html</guid>
  <pubDate>Wed, 16 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table-p9/reciprocal_tariffs_p9.png" medium="image" type="image/png" height="191" width="144"/>
</item>
<item>
  <title>Weekend Challenge – Recreating a Data Visualization with Polars and Plotnine</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/p9-polars-alta-ski-resort/20250412.html</link>
  <description><![CDATA[ 






<p>This post is part of a visualization recreation challenge using Polars and plotnine, inspired by <a href="../../posts/effective-dataviz-polars-alta-ski-resort/20250210.html">my earlier work</a>.</p>
<p>It marks my first serious dive into plotnine—an impressive library with a bit of a learning curve.<br>
I’ll walk through the journey I took to recreate the visualization. Some parts may overlap with the earlier post, but I believe that’s acceptable to keep this one self-contained.</p>
<p>The final figure, shown below, visualizes temperature trends for the ski season in Alta over the past few decades.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/p9-polars-alta-ski-resort/alta_ski_resort.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Alta ski resort"></p>
</figure>
</div>
<div id="f48557c8" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars.selectors <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cs</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> highlight_text <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ax_text</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib.axes <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Axes</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib.figure <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Figure</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (</span>
<span id="cb1-8">    aes,</span>
<span id="cb1-9">    element_blank,</span>
<span id="cb1-10">    element_text,</span>
<span id="cb1-11">    geom_line,</span>
<span id="cb1-12">    geom_point,</span>
<span id="cb1-13">    geom_segment,</span>
<span id="cb1-14">    geom_text,</span>
<span id="cb1-15">    ggplot,</span>
<span id="cb1-16">    labs,</span>
<span id="cb1-17">    scale_color_cmap,</span>
<span id="cb1-18">    scale_x_continuous,</span>
<span id="cb1-19">    scale_y_continuous,</span>
<span id="cb1-20">    theme,</span>
<span id="cb1-21">    theme_classic,</span>
<span id="cb1-22">)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://github.com/mattharrison/datasets/raw/refs/heads/master/data/alta-noaa-1980-2019.csv</span></span>
<span id="cb1-25">data_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alta-noaa-1980-2019.csv"</span></span>
<span id="cb1-26">columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>]</span>
<span id="cb1-27">idx_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="cb1-28">temp_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp"</span></span>
<span id="cb1-29"></span>
<span id="cb1-30">heading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.5</span></span>
<span id="cb1-31">heading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span></span>
<span id="cb1-32">subheading_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span>
<span id="cb1-33">subheading_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span></span>
<span id="cb1-34">source_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.5</span></span>
<span id="cb1-35">source_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"light"</span></span>
<span id="cb1-36">axis_fontsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span></span>
<span id="cb1-37">axis_fontweight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"normal"</span></span>
<span id="cb1-38">sub_props <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>: subheading_fontsize, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: subheading_fontweight}</span>
<span id="cb1-39"></span>
<span id="cb1-40">grey <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#aaaaaa"</span></span>
<span id="cb1-41">red <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e3120b"</span></span>
<span id="cb1-42">blue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0000ff"</span></span>
<span id="cb1-43"></span>
<span id="cb1-44"></span>
<span id="cb1-45"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_season_expr(col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, alias: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.expr:</span>
<span id="cb1-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-47">        (</span>
<span id="cb1-48">            pl.when((pl.col(col).dt.month().is_between(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, closed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"both"</span>)))</span>
<span id="cb1-49">            .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Summer "</span>))</span>
<span id="cb1-50">            .otherwise(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski "</span>))</span>
<span id="cb1-51">        )</span>
<span id="cb1-52">        .add(</span>
<span id="cb1-53">            pl.when(pl.col(col).dt.month() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb1-54">            .then(pl.col(col).dt.year().cast(pl.String))</span>
<span id="cb1-55">            .otherwise(pl.col(col).dt.year().add(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).cast(pl.String))</span>
<span id="cb1-56">        )</span>
<span id="cb1-57">        .alias(alias)</span>
<span id="cb1-58">    )</span>
<span id="cb1-59"></span>
<span id="cb1-60"></span>
<span id="cb1-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_day_of_season_expr(</span>
<span id="cb1-62">    col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, group_col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, alias: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="cb1-63">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.expr:</span>
<span id="cb1-64">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-65">        (pl.col(col) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pl.col(col).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>()).dt.total_days().over(group_col).alias(alias)</span>
<span id="cb1-66">    )</span>
<span id="cb1-67"></span>
<span id="cb1-68"></span>
<span id="cb1-69"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tweak_df(</span>
<span id="cb1-70">    data_path: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, columns: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>], idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="cb1-71">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.DataFrame:</span>
<span id="cb1-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-73">        pl.scan_csv(data_path)</span>
<span id="cb1-74">        .select(columns)</span>
<span id="cb1-75">        .with_columns(</span>
<span id="cb1-76">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.to_datetime(),</span>
<span id="cb1-77">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>).interpolate(),</span>
<span id="cb1-78">        )</span>
<span id="cb1-79">        .sort(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>)</span>
<span id="cb1-80">        .with_columns(</span>
<span id="cb1-81">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Caveat: Cannot be placed in the previous `with_columns()`</span></span>
<span id="cb1-82">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># due to different statuses of `TOBS`.</span></span>
<span id="cb1-83">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>).rolling_mean(window_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, center<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TMEAN"</span>),</span>
<span id="cb1-84">            get_season_expr(col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, alias<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>),</span>
<span id="cb1-85">        )</span>
<span id="cb1-86">        .with_columns(</span>
<span id="cb1-87">            add_day_of_season_expr(col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, group_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, alias<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname)</span>
<span id="cb1-88">        )</span>
<span id="cb1-89">        .collect()</span>
<span id="cb1-90">    )</span>
<span id="cb1-91"></span>
<span id="cb1-92"></span>
<span id="cb1-93"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(_df: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb1-94">    season_temps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.contains(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski"</span>)).pivot(</span>
<span id="cb1-95">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TMEAN"</span>, aggregate_function<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span></span>
<span id="cb1-96">    )</span>
<span id="cb1-97"></span>
<span id="cb1-98">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main</span></span>
<span id="cb1-99">    df_main <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> season_temps.unpivot(</span>
<span id="cb1-100">        (cs.starts_with(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cs.by_name(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski 2019"</span>)),</span>
<span id="cb1-101">        index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname,</span>
<span id="cb1-102">        variable_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>,</span>
<span id="cb1-103">        value_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname,</span>
<span id="cb1-104">    ).select(</span>
<span id="cb1-105">        idx_colname,</span>
<span id="cb1-106">        temp_colname,</span>
<span id="cb1-107">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">slice</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>).cast(pl.Int32),</span>
<span id="cb1-108">    )</span>
<span id="cb1-109"></span>
<span id="cb1-110">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decades</span></span>
<span id="cb1-111">    decades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1990</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</span>]</span>
<span id="cb1-112">    blues <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0055EE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0033CC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0011AA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#3377FF"</span>]</span>
<span id="cb1-113"></span>
<span id="cb1-114">    df_decade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat(</span>
<span id="cb1-115">        [</span>
<span id="cb1-116">            season_temps.select(</span>
<span id="cb1-117">                idx_colname,</span>
<span id="cb1-118">                pl.mean_horizontal(cs.contains(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(decade)[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])).alias(temp_colname),</span>
<span id="cb1-119">                pl.lit(b).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>),</span>
<span id="cb1-120">            )</span>
<span id="cb1-121">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> b, decade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(blues, decades)</span>
<span id="cb1-122">        ],</span>
<span id="cb1-123">        how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span>,</span>
<span id="cb1-124">    )</span>
<span id="cb1-125"></span>
<span id="cb1-126">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decade points</span></span>
<span id="cb1-127">    df_decade_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-128">        df_decade.group_by(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>), maintain_order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb1-129">        .agg(</span>
<span id="cb1-130">            pl.col(idx_colname).first().append(pl.col(idx_colname).last()),</span>
<span id="cb1-131">            pl.col(temp_colname).first().append(pl.col(temp_colname).last()),</span>
<span id="cb1-132">        )</span>
<span id="cb1-133">        .explode(idx_colname, temp_colname)</span>
<span id="cb1-134">    )</span>
<span id="cb1-135"></span>
<span id="cb1-136">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decade annotations</span></span>
<span id="cb1-137">    decade_annts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-138">        df_decade_pts.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(idx_colname).eq(pl.col(idx_colname).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()))</span>
<span id="cb1-139">        .select(temp_colname)</span>
<span id="cb1-140">        .to_series()</span>
<span id="cb1-141">        .to_list()</span>
<span id="cb1-142">    )</span>
<span id="cb1-143"></span>
<span id="cb1-144">    df_decade_annt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb1-145">        {</span>
<span id="cb1-146">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(decade_annts),</span>
<span id="cb1-147">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adjust y position for better appearance</span></span>
<span id="cb1-148">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>: [</span>
<span id="cb1-149">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb1-150">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-151">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-152">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb1-153">            ],</span>
<span id="cb1-154">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: blues,</span>
<span id="cb1-155">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>: decades,</span>
<span id="cb1-156">        }</span>
<span id="cb1-157">    )</span>
<span id="cb1-158"></span>
<span id="cb1-159">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ski_2019</span></span>
<span id="cb1-160">    ski_2019 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-161">        season_temps.select(</span>
<span id="cb1-162">            idx_colname, pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski 2019"</span>).alias(temp_colname)</span>
<span id="cb1-163">        ).drop_nulls()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "DAY_OF_SEASON"=181, "temp"=null</span></span>
<span id="cb1-164">    )</span>
<span id="cb1-165"></span>
<span id="cb1-166">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ski_2019 points</span></span>
<span id="cb1-167">    ski_2019_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat([ski_2019.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), ski_2019.tail(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)])</span>
<span id="cb1-168"></span>
<span id="cb1-169">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ggplot</span></span>
<span id="cb1-170">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-171">        ggplot(mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname))</span>
<span id="cb1-172">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># multiple grey lines</span></span>
<span id="cb1-173">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<span id="cb1-174">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor(year)"</span>),</span>
<span id="cb1-175">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_main,</span>
<span id="cb1-176">            alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb1-177">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-178">        )</span>
<span id="cb1-179">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 blue lines</span></span>
<span id="cb1-180">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<span id="cb1-181">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor(color)"</span>),</span>
<span id="cb1-182">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade,</span>
<span id="cb1-183">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="cb1-184">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-185">            lineend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>,</span>
<span id="cb1-186">        )</span>
<span id="cb1-187">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2019 red line</span></span>
<span id="cb1-188">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<span id="cb1-189">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ski_2019,</span>
<span id="cb1-190">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>red,</span>
<span id="cb1-191">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,</span>
<span id="cb1-192">            lineend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>,</span>
<span id="cb1-193">        )</span>
<span id="cb1-194">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 black dashed line for temp=32F</span></span>
<span id="cb1-195">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="cb1-196">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, yend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>),</span>
<span id="cb1-197">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-198">            linetype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,</span>
<span id="cb1-199">        )</span>
<span id="cb1-200">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start and end dots for 4 blue lines</span></span>
<span id="cb1-201">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_point(</span>
<span id="cb1-202">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname),</span>
<span id="cb1-203">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_pts,</span>
<span id="cb1-204">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_pts[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="cb1-205">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb1-206">        )</span>
<span id="cb1-207">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start and end dots for 2019 red line</span></span>
<span id="cb1-208">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_point(</span>
<span id="cb1-209">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname),</span>
<span id="cb1-210">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ski_2019_pts,</span>
<span id="cb1-211">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>red,</span>
<span id="cb1-212">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-213">        )</span>
<span id="cb1-214">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># annotations for 4 blue lines</span></span>
<span id="cb1-215">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(</span>
<span id="cb1-216">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>),</span>
<span id="cb1-217">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_annt,</span>
<span id="cb1-218">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_annt[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="cb1-219">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize,</span>
<span id="cb1-220">            fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight,</span>
<span id="cb1-221">            ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="cb1-222">            va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb1-223">        )</span>
<span id="cb1-224">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> labs(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day of season"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-225">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_x_continuous(</span>
<span id="cb1-226">            breaks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>],</span>
<span id="cb1-227">            limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>),</span>
<span id="cb1-228">            expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>),</span>
<span id="cb1-229">        )</span>
<span id="cb1-230">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_y_continuous(breaks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>], limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>), expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-231">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_color_cmap(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greys"</span>, guide<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)</span>
<span id="cb1-232">    )</span>
<span id="cb1-233"></span>
<span id="cb1-234"></span>
<span id="cb1-235"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> points_to_inches(points):</span>
<span id="cb1-236">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> points <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span></span>
<span id="cb1-237"></span>
<span id="cb1-238"></span>
<span id="cb1-239"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> themify(p: ggplot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Figure:</span>
<span id="cb1-240">    figsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">165</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pts</span></span>
<span id="cb1-241">    figsize_inches <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [points_to_inches(dim) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> dim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> figsize]</span>
<span id="cb1-242"></span>
<span id="cb1-243">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-244">        p</span>
<span id="cb1-245">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme_classic()</span>
<span id="cb1-246">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme(</span>
<span id="cb1-247">            axis_line_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb1-248">            axis_title_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize),</span>
<span id="cb1-249">            axis_title_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize),</span>
<span id="cb1-250">            axis_text_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb1-251">            axis_text_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb1-252">            dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>,</span>
<span id="cb1-253">            figure_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>figsize_inches,</span>
<span id="cb1-254">            aspect_ratio<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-255">            text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Roboto"</span>),</span>
<span id="cb1-256">        )</span>
<span id="cb1-257">    ).draw(show<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-258"></span>
<span id="cb1-259"></span>
<span id="cb1-260"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_ax_text(ax: Axes) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Axes:</span>
<span id="cb1-261">    ax_text(</span>
<span id="cb1-262">        s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;Alta Ski Resort&gt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;Temperature trends by &gt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;decade&gt;&lt; and &gt;&lt;2019&gt;"</span>,</span>
<span id="cb1-263">        x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb1-264">        y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>,</span>
<span id="cb1-265">        fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>heading_fontsize,</span>
<span id="cb1-266">        ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax,</span>
<span id="cb1-267">        va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb1-268">        ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="cb1-269">        zorder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb1-270">        highlight_textprops<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb1-271">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>: heading_fontsize, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: heading_fontweight},</span>
<span id="cb1-272">            sub_props,</span>
<span id="cb1-273">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: blue, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>sub_props},</span>
<span id="cb1-274">            sub_props,</span>
<span id="cb1-275">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: red, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>sub_props},</span>
<span id="cb1-276">        ],</span>
<span id="cb1-277">    )</span>
<span id="cb1-278"></span>
<span id="cb1-279">    ax.text(</span>
<span id="cb1-280">        <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb1-281">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb1-282">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: NOAA"</span>,</span>
<span id="cb1-283">        fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>source_fontsize,</span>
<span id="cb1-284">        fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>source_fontweight,</span>
<span id="cb1-285">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>grey,</span>
<span id="cb1-286">    )</span>
<span id="cb1-287">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ax</span>
<span id="cb1-288"></span>
<span id="cb1-289"></span>
<span id="cb1-290">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tweak_df(data_path, columns, idx_colname)</span>
<span id="cb1-291">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plot_temps(df, idx_colname)</span>
<span id="cb1-292">fig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> themify(p)</span>
<span id="cb1-293">ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fig.axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-294">ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> add_ax_text(ax)</span>
<span id="cb1-295">fig</span></code></pre></div></div>
</details>
</div>
<section id="data-processing-pipeline" class="level2">
<h2 class="anchored" data-anchor-id="data-processing-pipeline">Data Processing Pipeline</h2>
<p>Below is the data pipeline used to generate the DataFrame for the upcoming visualization stage:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="annotated-cell-2" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><span id="annotated-cell-2-1">data_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alta-noaa-1980-2019.csv"</span></span>
<span id="annotated-cell-2-2">columns <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>]</span>
<span id="annotated-cell-2-3">idx_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="annotated-cell-2-4">temp_colname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"temp"</span></span>
<span id="annotated-cell-2-5"></span>
<span id="annotated-cell-2-6"></span>
<span id="annotated-cell-2-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tweak_df(data_path: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, columns: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>], idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>):</span>
<span id="annotated-cell-2-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="annotated-cell-2-9">        pl.scan_csv(data_path)</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-2-10" class="code-annotation-target">        .select(columns)</span>
<span id="annotated-cell-2-11">        .with_columns(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-2-12" class="code-annotation-target">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.to_datetime(),</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-2-13" class="code-annotation-target">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>).interpolate(),</span>
<span id="annotated-cell-2-14">        )</span>
<span id="annotated-cell-2-15">        .sort(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>)</span>
<span id="annotated-cell-2-16">        .with_columns(</span>
<span id="annotated-cell-2-17">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Caveat: Cannot be placed in the previous `with_columns()`</span></span>
<span id="annotated-cell-2-18">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># due to different statuses of `TOBS`.</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-2-19" class="code-annotation-target">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TOBS"</span>).rolling_mean(window_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>, center<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TMEAN"</span>),</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="5" onclick="event.preventDefault();">5</a><span id="annotated-cell-2-20" class="code-annotation-target">            get_season_expr(col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, alias<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>),</span>
<span id="annotated-cell-2-21">        )</span>
<span id="annotated-cell-2-22">        .with_columns(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="6" onclick="event.preventDefault();">6</a><span id="annotated-cell-2-23" class="code-annotation-target">            add_day_of_season_expr(col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, group_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, alias<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname)</span>
<span id="annotated-cell-2-24">        )</span>
<span id="annotated-cell-2-25">        .collect()</span>
<span id="annotated-cell-2-26">    )</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="10" data-code-annotation="1">Select the <code>DATE</code> column (dates) and <code>TOBS</code> column (recorded temperatures in Fahrenheit).</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="12" data-code-annotation="2">Convert the <code>DATE</code> column to a datetime format.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="13" data-code-annotation="3">Perform interpolation on the <code>TOBS</code> column.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="4">4</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="19" data-code-annotation="4">Compute a 28-day rolling average for <code>TOBS</code>.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="5">5</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="20" data-code-annotation="5">Use <code>get_season_expr()</code> to categorize each date into a <code>SEASON</code>.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="6">6</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="23" data-code-annotation="6">Apply <code>add_day_of_season_expr()</code> to calculate <code>DAY_OF_SEASON</code>, representing days elapsed since the start of the season.</span>
</dd>
</dl>
<p>The first three steps involve straightforward Polars expressions. In the following two sub-sections, we’ll dive deeper into steps 5 and 6.</p>
<section id="categorizing-dates-into-summer-and-ski-seasons" class="level3">
<h3 class="anchored" data-anchor-id="categorizing-dates-into-summer-and-ski-seasons">Categorizing Dates into <code>Summer</code> and <code>Ski</code> Seasons</h3>
<p>To analyze seasonal trends, we classify dates into two categories:</p>
<ul>
<li><strong><code>Summer</code></strong>: Covers May through October.<br>
</li>
<li><strong><code>Ski</code></strong>: Covers November through April.</li>
</ul>
<p>If a date falls in November or December, it is assigned to the following year’s season. For example, <code>2015-10-31</code> is categorized as <code>Summer 2015</code>, while <code>2015-11-01</code> belongs to <code>Ski 2016</code>.</p>
<p>To implement this logic, we define <code>get_season_expr()</code>, which leverages Polars’ <a href="https://docs.pola.rs/api/python/dev/reference/expressions/api/polars.when.html">when-then-otherwise</a> expressions to determine the season and year.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_season_expr(col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, alias: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.expr:</span>
<span id="cb2-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb2-3">        (</span>
<span id="cb2-4">            pl.when((pl.col(col).dt.month().is_between(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, closed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"both"</span>)))</span>
<span id="cb2-5">            .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Summer "</span>))</span>
<span id="cb2-6">            .otherwise(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski "</span>))</span>
<span id="cb2-7">        )</span>
<span id="cb2-8">        .add(</span>
<span id="cb2-9">            pl.when(pl.col(col).dt.month() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb2-10">            .then(pl.col(col).dt.year().cast(pl.String))</span>
<span id="cb2-11">            .otherwise(pl.col(col).dt.year().add(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).cast(pl.String))</span>
<span id="cb2-12">        )</span>
<span id="cb2-13">        .alias(alias)</span>
<span id="cb2-14">    )</span></code></pre></div></div>
<p>In this function:</p>
<ul>
<li>If the month is between May and October, the function assigns <code>"Summer "</code>. Otherwise, it assigns <code>"Ski "</code> (with a trailing space for concatenation).<br>
</li>
<li>The year is determined based on the month: dates from January to October retain their current year, while those in November and December are shifted to the next year.</li>
</ul>
<p>By applying this function, we can add a <code>SEASON</code> column to a DataFrame, ensuring each date is categorized correctly.</p>
</section>
<section id="calculating-the-total-days-for-each-season" class="level3">
<h3 class="anchored" data-anchor-id="calculating-the-total-days-for-each-season">Calculating the Total Days for Each Season</h3>
<p>Once we have the seasonal categories, we calculate <code>DAY_OF_SEASON</code>, which tracks the number of days elapsed within each season. This is achieved using the <a href="https://docs.pola.rs/api/python/dev/reference/expressions/api/polars.Expr.over.html">pl.expr.over()</a> expression, which operates similarly to Pandas’ <code>groupby().transform()</code>, applying transformations within groups.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_day_of_season_expr(</span>
<span id="cb3-2">    col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>, group_col: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, alias: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="cb3-3">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.expr:</span>
<span id="cb3-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb3-5">        (pl.col(col) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pl.col(col).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>()).dt.total_days().over(group_col).alias(alias)</span>
<span id="cb3-6">    )</span></code></pre></div></div>
</section>
</section>
<section id="visualizing-temperature-trends-with-plotnine" class="level2">
<h2 class="anchored" data-anchor-id="visualizing-temperature-trends-with-plotnine">Visualizing Temperature Trends with Plotnine</h2>
<p>With the data prepared, we now turn our focus to visualization. The <code>plot_temps()</code> function is relatively long, so we’ll walk through it step by step. Ultimately, we’ll construct several intermediate DataFrames and use them to build a <code>ggplot</code> object for rendering.</p>
<section id="reshaping-the-main-dataframe" class="level3">
<h3 class="anchored" data-anchor-id="reshaping-the-main-dataframe">Reshaping the Main DataFrame</h3>
<p>We begin by filtering the dataset to include only rows corresponding to <code>Ski</code> seasons. Then, using <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.pivot.html">pl.DataFrame.pivot()</a> and <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.unpivot.html">pl.DataFrame.unpivot()</a>, we reshape the main DataFrame.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(</span>
<span id="cb4-2">    _df: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span></span>
<span id="cb4-3">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb4-4">    season_temps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> _df.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.contains(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski"</span>)).pivot(</span>
<span id="cb4-5">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SEASON"</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TMEAN"</span>, aggregate_function<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span></span>
<span id="cb4-6">    )</span>
<span id="cb4-7"></span>
<span id="cb4-8">    df_main <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> season_temps.unpivot(</span>
<span id="cb4-9">        (cs.starts_with(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> cs.by_name(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski 2019"</span>)),</span>
<span id="cb4-10">        index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname,</span>
<span id="cb4-11">        variable_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>,</span>
<span id="cb4-12">        value_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname,</span>
<span id="cb4-13">    ).select(</span>
<span id="cb4-14">        idx_colname,</span>
<span id="cb4-15">        temp_colname,</span>
<span id="cb4-16">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">slice</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>).cast(pl.Int32),</span>
<span id="cb4-17">    )</span>
<span id="cb4-18">    ...</span></code></pre></div></div>
<p>Note: We intentionally exclude <code>Ski 2019</code> here, as it will be handled separately later.</p>
<p>Preview of <code>df_main</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7_098</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-2">┌───────────────┬───────────┬──────┐</span>
<span id="cb5-3">│ DAY_OF_SEASON ┆ temp      ┆ year │</span>
<span id="cb5-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>           ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>       ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>  │</span>
<span id="cb5-5">│ i64           ┆ f64       ┆ i32  │</span>
<span id="cb5-6">╞═══════════════╪═══════════╪══════╡</span>
<span id="cb5-7">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>             ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span> │</span>
<span id="cb5-8">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>             ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span> │</span>
<span id="cb5-9">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>             ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span> │</span>
<span id="cb5-10">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>             ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span> │</span>
<span id="cb5-11">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>             ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span> │</span>
<span id="cb5-12">│ …             ┆ …         ┆ …    │</span>
<span id="cb5-13">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">177</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">44.0</span>      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span> │</span>
<span id="cb5-14">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">178</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">44.464286</span> ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span> │</span>
<span id="cb5-15">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">179</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">44.607143</span> ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span> │</span>
<span id="cb5-16">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">44.142857</span> ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span> │</span>
<span id="cb5-17">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">181</span>           ┆ null      ┆ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2018</span> │</span>
<span id="cb5-18">└───────────────┴───────────┴──────┘</span></code></pre></div></div>
</section>
<section id="building-a-dataframe-for-decade-averages" class="level3">
<h3 class="anchored" data-anchor-id="building-a-dataframe-for-decade-averages">Building a DataFrame for Decade Averages</h3>
<p>To reveal long-term patterns, we compute average temperature trends by decade. Each decade will be represented by a separate line with a distinct color.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(df_: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb6-2">    ...</span>
<span id="cb6-3">    decades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1990</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2010</span>]</span>
<span id="cb6-4">    blues <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0055EE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0033CC"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0011AA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#3377FF"</span>]</span>
<span id="cb6-5"></span>
<span id="cb6-6">    df_decade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat(</span>
<span id="cb6-7">        [</span>
<span id="cb6-8">            season_temps.select(</span>
<span id="cb6-9">                idx_colname,</span>
<span id="cb6-10">                pl.mean_horizontal(cs.contains(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(decade)[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])).alias(temp_colname),</span>
<span id="cb6-11">                pl.lit(b).alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>),</span>
<span id="cb6-12">            )</span>
<span id="cb6-13">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> b, decade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(blues, decades)</span>
<span id="cb6-14">        ],</span>
<span id="cb6-15">        how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span>,</span>
<span id="cb6-16">    )</span></code></pre></div></div>
<p>Preview of <code>df_decade</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">728</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-2">┌───────────────┬───────────┬─────────┐</span>
<span id="cb7-3">│ DAY_OF_SEASON ┆ temp      ┆ color   │</span>
<span id="cb7-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>           ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>       ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>     │</span>
<span id="cb7-5">│ i64           ┆ f64       ┆ <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>     │</span>
<span id="cb7-6">╞═══════════════╪═══════════╪═════════╡</span>
<span id="cb7-7">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.704365</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE │</span></span>
<span id="cb7-8">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.156746</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE │</span></span>
<span id="cb7-9">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.875</span>    ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE │</span></span>
<span id="cb7-10">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.561508</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE │</span></span>
<span id="cb7-11">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.041667</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE │</span></span>
<span id="cb7-12">│ …             ┆ …         ┆ …       │</span>
<span id="cb7-13">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">177</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.275</span>    ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF │</span></span>
<span id="cb7-14">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">178</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.639286</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF │</span></span>
<span id="cb7-15">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">179</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.092857</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF │</span></span>
<span id="cb7-16">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.653571</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF │</span></span>
<span id="cb7-17">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">181</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">41.428571</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF │</span></span>
<span id="cb7-18">└───────────────┴───────────┴─────────┘</span></code></pre></div></div>
</section>
<section id="annotating-decade-lines" class="level3">
<h3 class="anchored" data-anchor-id="annotating-decade-lines">Annotating Decade Lines</h3>
<p>To enhance readability, we annotate each decade line in two ways: by marking the start and end points and by adding labels to the line endings. For this purpose, we create two separate DataFrames.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(df_: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb8-2">    ...</span>
<span id="cb8-3">    df_decade_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb8-4">        df_decade.group_by(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>), maintain_order<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb8-5">        .agg(</span>
<span id="cb8-6">            pl.col(idx_colname).first().append(pl.col(idx_colname).last()),</span>
<span id="cb8-7">            pl.col(temp_colname).first().append(pl.col(temp_colname).last()),</span>
<span id="cb8-8">        )</span>
<span id="cb8-9">        .explode(idx_colname, temp_colname)</span>
<span id="cb8-10">    )</span>
<span id="cb8-11"></span>
<span id="cb8-12">    decade_annts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb8-13">        df_decade_pts.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(idx_colname).eq(pl.col(idx_colname).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()))</span>
<span id="cb8-14">        .select(temp_colname)</span>
<span id="cb8-15">        .to_series()</span>
<span id="cb8-16">        .to_list()</span>
<span id="cb8-17">    )</span>
<span id="cb8-18"></span>
<span id="cb8-19">    df_decade_annt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(</span>
<span id="cb8-20">        {</span>
<span id="cb8-21">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(decade_annts),</span>
<span id="cb8-22">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adjust y position for better appearance</span></span>
<span id="cb8-23">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>: [</span>
<span id="cb8-24">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb8-25">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb8-26">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb8-27">                decade_annts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb8-28">            ],</span>
<span id="cb8-29">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: blues,</span>
<span id="cb8-30">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>: decades,</span>
<span id="cb8-31">        }</span>
<span id="cb8-32">    )</span></code></pre></div></div>
<p>Preview of <code>df_decade_pts</code> (start and end points):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb9-2">┌─────────┬───────────────┬───────────┐</span>
<span id="cb9-3">│ color   ┆ DAY_OF_SEASON ┆ temp      │</span>
<span id="cb9-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>     ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>           ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>       │</span>
<span id="cb9-5">│ <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>     ┆ i64           ┆ f64       │</span>
<span id="cb9-6">╞═════════╪═══════════════╪═══════════╡</span>
<span id="cb9-7">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE ┆ 0             ┆ 32.704365 │</span></span>
<span id="cb9-8">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE ┆ 181           ┆ 34.357143 │</span></span>
<span id="cb9-9">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0033CC ┆ 0             ┆ 34.851786 │</span></span>
<span id="cb9-10">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0033CC ┆ 181           ┆ 44.535714 │</span></span>
<span id="cb9-11">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0011AA ┆ 0             ┆ 35.719643 │</span></span>
<span id="cb9-12">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0011AA ┆ 181           ┆ 40.77381  │</span></span>
<span id="cb9-13">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF ┆ 0             ┆ 35.380357 │</span></span>
<span id="cb9-14">│ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF ┆ 181           ┆ 41.428571 │</span></span>
<span id="cb9-15">└─────────┴───────────────┴───────────┘</span></code></pre></div></div>
<p>Preview of <code>df_decade_annt</code> (annotation labels):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb10-2">┌─────┬───────────┬─────────┬───────┐</span>
<span id="cb10-3">│ x   ┆ y         ┆ color   ┆ label │</span>
<span id="cb10-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span> ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>       ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>     ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>   │</span>
<span id="cb10-5">│ i64 ┆ f64       ┆ <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>     ┆ i64   │</span>
<span id="cb10-6">╞═════╪═══════════╪═════════╪═══════╡</span>
<span id="cb10-7">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span> ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">34.357143</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0055EE ┆ 1980  │</span></span>
<span id="cb10-8">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span> ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45.035714</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0033CC ┆ 1990  │</span></span>
<span id="cb10-9">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span> ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37.77381</span>  ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#0011AA ┆ 2000  │</span></span>
<span id="cb10-10">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span> ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">41.428571</span> ┆ <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#3377FF ┆ 2010  │</span></span>
<span id="cb10-11">└─────┴───────────┴─────────┴───────┘</span></code></pre></div></div>
</section>
<section id="isolating-the-2019-ski-season" class="level3">
<h3 class="anchored" data-anchor-id="isolating-the-2019-ski-season">Isolating the 2019 Ski Season</h3>
<p>We separate out the 2019 <code>Ski</code> season so that we can highlight it independently in the final visualization.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(df_: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb11-2">    ...</span>
<span id="cb11-3">    ski_2019 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb11-4">        season_temps.select(</span>
<span id="cb11-5">            idx_colname, pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ski 2019"</span>).alias(temp_colname)</span>
<span id="cb11-6">        ).drop_nulls()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "DAY_OF_SEASON"=181, "temp"=null</span></span>
<span id="cb11-7">    )</span></code></pre></div></div>
<p>Preview of <code>ski_2019</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">shape: (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">181</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-2">┌───────────────┬───────────┐</span>
<span id="cb12-3">│ DAY_OF_SEASON ┆ temp      │</span>
<span id="cb12-4">│ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>           ┆ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span>       │</span>
<span id="cb12-5">│ i64           ┆ f64       │</span>
<span id="cb12-6">╞═══════════════╪═══════════╡</span>
<span id="cb12-7">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">33.214286</span> │</span>
<span id="cb12-8">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.892857</span> │</span>
<span id="cb12-9">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.25</span>     │</span>
<span id="cb12-10">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.142857</span> │</span>
<span id="cb12-11">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>             ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.357143</span> │</span>
<span id="cb12-12">│ …             ┆ …         │</span>
<span id="cb12-13">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">176</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.607143</span> │</span>
<span id="cb12-14">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">177</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.285714</span> │</span>
<span id="cb12-15">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">178</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">39.964286</span> │</span>
<span id="cb12-16">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">179</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.464286</span> │</span>
<span id="cb12-17">│ <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>           ┆ <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">41.25</span>     │</span>
<span id="cb12-18">└───────────────┴───────────┘</span></code></pre></div></div>
<p>We also prepare a small DataFrame to annotate the start and end points of the 2019 line:</p>
<p>Preview of <code>ski_2019_pts</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(df_: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="cb13-2">    ...</span>
<span id="cb13-3">    ski_2019_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.concat([ski_2019.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), ski_2019.tail(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)])</span></code></pre></div></div>
</section>
<section id="creating-the-ggplot-object" class="level3">
<h3 class="anchored" data-anchor-id="creating-the-ggplot-object">Creating the ggplot Object</h3>
<p>We now bring everything together into a single <code>ggplot</code> object:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="annotated-cell-15" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><span id="annotated-cell-15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> plot_temps(df_: pl.DataFrame, idx_colname: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DAY_OF_SEASON"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> ggplot:</span>
<span id="annotated-cell-15-2">    ...</span>
<span id="annotated-cell-15-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-15-4" class="code-annotation-target">        ggplot(mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname))</span>
<span id="annotated-cell-15-5">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># multiple grey lines</span></span>
<span id="annotated-cell-15-6">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="2" onclick="event.preventDefault();">2</a><span id="annotated-cell-15-7" class="code-annotation-target">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor(year)"</span>),</span>
<span id="annotated-cell-15-8">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_main,</span>
<span id="annotated-cell-15-9">            alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="annotated-cell-15-10">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="annotated-cell-15-11">        )</span>
<span id="annotated-cell-15-12">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4 blue lines</span></span>
<span id="annotated-cell-15-13">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="3" onclick="event.preventDefault();">3</a><span id="annotated-cell-15-14" class="code-annotation-target">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor(color)"</span>),</span>
<span id="annotated-cell-15-15">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade,</span>
<span id="annotated-cell-15-16">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="annotated-cell-15-17">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="4" onclick="event.preventDefault();">4</a><span id="annotated-cell-15-18" class="code-annotation-target">            lineend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>,</span>
<span id="annotated-cell-15-19">        )</span>
<span id="annotated-cell-15-20">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2019 red line</span></span>
<span id="annotated-cell-15-21">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_line(</span>
<span id="annotated-cell-15-22">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ski_2019,</span>
<span id="annotated-cell-15-23">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>red,</span>
<span id="annotated-cell-15-24">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>,</span>
<span id="annotated-cell-15-25">            lineend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"round"</span>,</span>
<span id="annotated-cell-15-26">        )</span>
<span id="annotated-cell-15-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 black dashed line for temp=32F</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="5" onclick="event.preventDefault();">5</a><span id="annotated-cell-15-28" class="code-annotation-target">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_segment(</span>
<span id="annotated-cell-15-29">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, xend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, yend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>),</span>
<span id="annotated-cell-15-30">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="annotated-cell-15-31">            linetype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,</span>
<span id="annotated-cell-15-32">        )</span>
<span id="annotated-cell-15-33">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start and end dots for 4 blue lines</span></span>
<span id="annotated-cell-15-34">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_point(</span>
<span id="annotated-cell-15-35">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname),</span>
<span id="annotated-cell-15-36">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_pts,</span>
<span id="annotated-cell-15-37">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_pts[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="annotated-cell-15-38">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="annotated-cell-15-39">        )</span>
<span id="annotated-cell-15-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start and end dots for 2019 red line</span></span>
<span id="annotated-cell-15-41">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_point(</span>
<span id="annotated-cell-15-42">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>idx_colname, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp_colname),</span>
<span id="annotated-cell-15-43">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ski_2019_pts,</span>
<span id="annotated-cell-15-44">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>red,</span>
<span id="annotated-cell-15-45">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="annotated-cell-15-46">        )</span>
<span id="annotated-cell-15-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># annotations for 4 blue lines</span></span>
<span id="annotated-cell-15-48">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_text(</span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="6" onclick="event.preventDefault();">6</a><span id="annotated-cell-15-49" class="code-annotation-target">            mapping<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>),</span>
<span id="annotated-cell-15-50">            data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_annt,</span>
<span id="annotated-cell-15-51">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df_decade_annt[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>],</span>
<span id="annotated-cell-15-52">            size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize,</span>
<span id="annotated-cell-15-53">            fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight,</span>
<span id="annotated-cell-15-54">            ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="annotated-cell-15-55">            va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="annotated-cell-15-56">        )</span>
<span id="annotated-cell-15-57">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> labs(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Day of season"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="annotated-cell-15-58">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_x_continuous(</span>
<span id="annotated-cell-15-59">            breaks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>],</span>
<span id="annotated-cell-15-60">            limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>),</span>
<span id="annotated-cell-15-61">            expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>),</span>
<span id="annotated-cell-15-62">        )</span>
<span id="annotated-cell-15-63">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_y_continuous(breaks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>], limits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>), expand<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="annotated-cell-15-64">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> scale_color_cmap(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greys"</span>, guide<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)</span>
<span id="annotated-cell-15-65">    )</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-15" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="4" data-code-annotation="1">We define the default aesthetic mapping in <a href="https://plotnine.org/reference/ggplot.html#plotnine.ggplot">ggplot()</a> to avoid repetition in later layers.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="7" data-code-annotation="2"><code>factor(year)</code> is mapped to <code>color=</code>, and the <code>Greys</code> colormap is applied using <a href="https://plotnine.org/reference/scale_color_cmap.html#plotnine.scale_color_cmap">scale_color_cmap()</a>—a very convenient way to show progression.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="14" data-code-annotation="3">For the decade lines, <code>factor(color)</code> is mapped to the <code>fill=</code> aesthetic. Colors are controlled via a <code>color</code> column in the dataframe—an effective trick.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="4">4</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="18" data-code-annotation="4">The <code>lineend=</code> parameter is useful if you don’t plan to mark endpoints with dots.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="5">5</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="28" data-code-annotation="5">Using <a href="https://plotnine.org/reference/geom_hline.html#plotnine.geom_hline">geom_hline()</a> would extend the line infinitely in both directions. In this case, we used <a href="https://plotnine.org/reference/geom_segment.html#plotnine.geom_segment">geom_segment()</a> to constrain the line.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="6">6</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="49" data-code-annotation="6">The <code>label=</code> aesthetic in <a href="https://plotnine.org/reference/geom_text.html#plotnine.geom_text">geom_text()</a> is mapped from the <code>label</code> column in <code>df_decade_annt</code>.</span>
</dd>
</dl>
</section>
</section>
<section id="touchups" class="level2">
<h2 class="anchored" data-anchor-id="touchups">Touchups</h2>
<section id="adding-a-theme" class="level3">
<h3 class="anchored" data-anchor-id="adding-a-theme">Adding a Theme</h3>
<p>We apply a custom theme using the <code>themify()</code> function, adjusting various <strong>themeable</strong> elements to refine the plot’s appearance:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> points_to_inches(points):</span>
<span id="cb14-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> points <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span></span>
<span id="cb14-3"></span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> themify(p: ggplot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Figure:</span>
<span id="cb14-6">    figsize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">160</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">165</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pts</span></span>
<span id="cb14-7">    figsize_inches <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [points_to_inches(dim) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> dim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> figsize]</span>
<span id="cb14-8"></span>
<span id="cb14-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb14-10">        p</span>
<span id="cb14-11">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme_classic()</span>
<span id="cb14-12">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> theme(</span>
<span id="cb14-13">            axis_line_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_blank(),</span>
<span id="cb14-14">            axis_title_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize),</span>
<span id="cb14-15">            axis_title_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontweight, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>axis_fontsize),</span>
<span id="cb14-16">            axis_text_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb14-17">            axis_text_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>),</span>
<span id="cb14-18">            dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>,</span>
<span id="cb14-19">            figure_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>figsize_inches,</span>
<span id="cb14-20">            aspect_ratio<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb14-21">            text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>element_text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Roboto"</span>),</span>
<span id="cb14-22">        )</span>
<span id="cb14-23">    ).draw(show<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
</section>
<section id="adding-a-title-and-source-note" class="level3">
<h3 class="anchored" data-anchor-id="adding-a-title-and-source-note">Adding a Title and Source Note</h3>
<p>For the title, we use <code>ax_text()</code> from the <a href="https://github.com/znstrider/highlight_text">HighlightText</a> library. It allows inline text highlighting using <code>&lt; &gt;</code>, letting us emphasize specific parts of the title like <code>&lt;Alta Ski Resort&gt;</code>, <code>&lt;Temperature trends by &gt;</code>, <code>&lt;decade&gt;</code>, <code>&lt; and &gt;</code>, and <code>&lt;2019&gt;</code> with customized styles.</p>
<p>To add a source note, we simply use Matplotlib’s <code>ax.text()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> add_ax_text(ax: Axes) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Axes:</span>
<span id="cb15-2">    ax_text(</span>
<span id="cb15-3">        s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;Alta Ski Resort&gt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;Temperature trends by &gt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;decade&gt;&lt; and &gt;&lt;2019&gt;"</span>,</span>
<span id="cb15-4">        x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb15-5">        y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>,</span>
<span id="cb15-6">        fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>heading_fontsize,</span>
<span id="cb15-7">        ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax,</span>
<span id="cb15-8">        va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb15-9">        ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,</span>
<span id="cb15-10">        zorder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb15-11">        highlight_textprops<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb15-12">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontsize"</span>: heading_fontsize, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fontweight"</span>: heading_fontweight},</span>
<span id="cb15-13">            sub_props,</span>
<span id="cb15-14">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: blue, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>sub_props},</span>
<span id="cb15-15">            sub_props,</span>
<span id="cb15-16">            {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: red, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>sub_props},</span>
<span id="cb15-17">        ],</span>
<span id="cb15-18">    )</span>
<span id="cb15-19"></span>
<span id="cb15-20">    ax.text(</span>
<span id="cb15-21">        <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb15-22">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb15-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: NOAA"</span>,</span>
<span id="cb15-24">        fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>source_fontsize,</span>
<span id="cb15-25">        fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>source_fontweight,</span>
<span id="cb15-26">        color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>grey,</span>
<span id="cb15-27">    )</span>
<span id="cb15-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ax</span></code></pre></div></div>
</section>
</section>
<section id="rendering-the-plot" class="level2">
<h2 class="anchored" data-anchor-id="rendering-the-plot">Rendering the Plot</h2>
<p>Now we put everything together and render the final plot. A key trick here is retrieving the <code>ax</code> object using <code>fig.axes[0]</code>, which allows us to apply both HighlightText and regular Matplotlib functions.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tweak_df(data_path, columns, idx_colname)</span>
<span id="cb16-2">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plot_temps(df, idx_colname)</span>
<span id="cb16-3">fig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> themify(p)</span>
<span id="cb16-4">ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fig.axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb16-5">ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> add_ax_text(ax)</span>
<span id="cb16-6">fig</span></code></pre></div></div>
</section>
<section id="takeaways" class="level2">
<h2 class="anchored" data-anchor-id="takeaways">Takeaways</h2>
<p>Wrapping up this post, I’ve come to appreciate how powerful the plotnine library truly is. While its aesthetic system requires a bit of mental shift, it offers a clean, expressive way to build layered visualizations.</p>
<p>One key takeaway for me is that each layer can operate on its own dataset, which adds a lot of flexibility. What I enjoyed most, though, is the theme system—it makes it easy to define a consistent visual style that can be reused across different plots.</p>
<p>One limitation I ran into was the lack of a plotnine-native alternative to <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplot_mosaic.html">pli.subplot_mosaic()</a>. This feature allows for more granular layout control—for example, dividing the figure into separate axes with custom height ratios for the title, main plot, and source note using <code>gridspec_kw={"height_ratios": [6, 12, 1]}</code>.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>matplotlib</category>
  <category>plotnine</category>
  <guid>https://tech.ycwu.space/posts/p9-polars-alta-ski-resort/20250412.html</guid>
  <pubDate>Sat, 12 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/p9-polars-alta-ski-resort/alta_ski_resort.png" medium="image" type="image/png" height="131" width="144"/>
</item>
<item>
  <title>How to Access the Axes in Plotnine</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/access-axes-in-plotnine/20250411.html</link>
  <description><![CDATA[ 






<p>This is a short post documenting my experience trying to retrieve the <code>Axes</code> from <a href="https://plotnine.org/">plotnine</a>.</p>
<p>The idea was inspired by <a href="https://github.com/nrennie/2024-plotnine-contest">this code</a> and <a href="https://www.youtube.com/watch?v=NBGJuaBF2rc">this video</a> I came across, but the original solution didn’t quite work for me. Fortunately, I found that we can access the list of <code>Axes</code> directly using either <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.axes.html">fig.axes</a> or <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.get_axes.html#matplotlib.figure.Figure.get_axes">fig.get_axes()</a>.</p>
<p>In the example below, I adapt a snippet from the <a href="https://plotnine.org/reference/geom_smooth.html#examples">plotnine documentation</a> to demonstrate how to highlight text in color using <a href="https://github.com/znstrider/highlight_text">HighlightText</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/access-axes-in-plotnine/p9_ht_ax_text.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="plotnine used in combination with HighlightText"></p>
</figure>
</div>
<div id="3f56dc2f" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> highlight_text <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ht</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> aes, geom_point, ggplot, labs, theme_matplotlib, theme_set</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine.data <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> mpg</span>
<span id="cb1-4"></span>
<span id="cb1-5">theme_set(theme_matplotlib())</span>
<span id="cb1-6"></span>
<span id="cb1-7">p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-8">    ggplot(mpg, aes(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"displ"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hwy"</span>))</span>
<span id="cb1-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> geom_point()</span>
<span id="cb1-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> labs(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"displacement"</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"horsepower"</span>)</span>
<span id="cb1-11">)</span>
<span id="cb1-12"></span>
<span id="cb1-13">fig <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p.draw()</span>
<span id="cb1-14">ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fig.axes[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or via fig.get_axes()[0]</span></span>
<span id="cb1-15">ht_ax_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Color highlighted by &lt;HighlightText::{"color": "#E58606"}&gt;'</span></span>
<span id="cb1-16">ht.ax_text(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, ht_ax_text, vsep<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, va<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax)</span>
<span id="cb1-17">fig</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>



 ]]></description>
  <category>python</category>
  <category>matplotlib</category>
  <category>plotnine</category>
  <guid>https://tech.ycwu.space/posts/access-axes-in-plotnine/20250411.html</guid>
  <pubDate>Fri, 11 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/access-axes-in-plotnine/p9_ht_ax_text.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Clone the Reciprocal Tariffs Table Using Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table/20250403.html</link>
  <description><![CDATA[ 






<p>This post demonstrates how to clone the Reciprocal Tariffs table that President Donald Trump announced on April 2 on <a href="https://truthsocial.com/@realDonaldTrump/114270398531479278">Truth Social</a>.</p>
<p>Since the code is largely self-explanatory, I will highlight some tricks and caveats I encountered while building the table:</p>
<ol type="1">
<li>The <code>data</code> dictionary was extracted with the help of AI.</li>
<li>The border radius was one of the trickiest aspects of the table—it took some time to figure out how to fill the gap between the rounded corners and the rectangle. Fortunately, I drew inspiration from the Great Tables example, <a href="https://posit-dev.github.io/great-tables/examples/">Highest Paid Athletes in 2023</a>. The solution was to use two <code>&lt;div&gt;</code> tags, which allowed me to assign distinct colors to the inside and outside of the border.</li>
<li>I created four empty columns—<code>["0", "1", "2", "3"]</code>—to serve as borders. This allowed for easier adjustment of border properties such as color and width. An interesting feature of <a href="https://posit-dev.github.io/great-tables/reference/GT.cols_width.html#great_tables.GT.cols_width">GT.cols_width()</a> is that you can set column widths using percentages, and the total doesn’t need to add up to exactly 100%—it just works. This is especially handy when experimenting with different table designs, as it lets you estimate widths without worrying about precise calculations.</li>
<li>I added a “mod” column to help distinguish whether a row’s index is odd or even. This made it possible to combine <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.when.html">pl.when().then().otherwise()</a> with <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.map_elements.html">pl.Expr.map_elements()</a>. Although <code>pl.Expr.map_elements()</code> is generally considered an anti-pattern in Polars, I believe it fits my use case well.</li>
<li>The <code>column_labels_border_bottom_style=</code> parameter in <a href="https://posit-dev.github.io/great-tables/reference/GT.tab_options.html#great_tables.GT.tab_options">GT.tab_options()</a> is key to hiding the line between the column labels and the table body.</li>
<li>Logo embedding might seem a bit tricky if you’re new to Great Tables; I suggest reading the <a href="https://posit-dev.github.io/great-tables/blog/rendering-images/">blog post</a> that details how to render images anywhere in Great Tables.</li>
<li>To enhance the overall visual effect, I added a row at the end of the table and filled it with the same background color.</li>
<li>The original table featured two rings, but I was only able to implement the inner one using <a href="https://posit-dev.github.io/great-tables/reference/GT.opt_table_outline.html#great_tables.GT.opt_table_outline">GT.opt_table_outline()</a>. I decided to leave it out for a cleaner appearance.</li>
<li>While the color codes and font properties could be further enhanced, Great Tables successfully replicates the table’s structure and style with only minor differences.</li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table/reciprocal_tariffs_gt.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Cloned Reciprocal Tariffs Table"></p>
</figure>
</div>
<div id="95abfd16" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show full code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, google_font, html, loc, style, vals</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># source1: https://truthsocial.com/@realDonaldTrump/114270398531479278</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># source2:</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "https://upload.wikimedia.org/wikipedia/commons/</span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># thumb/3/36/Seal_of_the_President_of_the_United_States.svg/</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 800px-Seal_of_the_President_of_the_United_States.svg.png"</span></span>
<span id="cb1-9">logo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> vals.fmt_image(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logo.png"</span>, height<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-10"></span>
<span id="cb1-11">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>: [</span>
<span id="cb1-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"China"</span>,</span>
<span id="cb1-14">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"European Union"</span>,</span>
<span id="cb1-15">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vietnam"</span>,</span>
<span id="cb1-16">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taiwan"</span>,</span>
<span id="cb1-17">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Japan"</span>,</span>
<span id="cb1-18">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"India"</span>,</span>
<span id="cb1-19">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"South Korea"</span>,</span>
<span id="cb1-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thailand"</span>,</span>
<span id="cb1-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Switzerland"</span>,</span>
<span id="cb1-22">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Indonesia"</span>,</span>
<span id="cb1-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Malaysia"</span>,</span>
<span id="cb1-24">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cambodia"</span>,</span>
<span id="cb1-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"United Kingdom"</span>,</span>
<span id="cb1-26">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"South Africa"</span>,</span>
<span id="cb1-27">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brazil"</span>,</span>
<span id="cb1-28">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bangladesh"</span>,</span>
<span id="cb1-29">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Singapore"</span>,</span>
<span id="cb1-30">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Israel"</span>,</span>
<span id="cb1-31">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Philippines"</span>,</span>
<span id="cb1-32">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chile"</span>,</span>
<span id="cb1-33">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Australia"</span>,</span>
<span id="cb1-34">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pakistan"</span>,</span>
<span id="cb1-35">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Turkey"</span>,</span>
<span id="cb1-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sri Lanka"</span>,</span>
<span id="cb1-37">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Colombia"</span>,</span>
<span id="cb1-38">    ],</span>
<span id="cb1-39">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>: [</span>
<span id="cb1-40">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"67%"</span>,</span>
<span id="cb1-41">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"39%"</span>,</span>
<span id="cb1-42">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"90%"</span>,</span>
<span id="cb1-43">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"64%"</span>,</span>
<span id="cb1-44">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"46%"</span>,</span>
<span id="cb1-45">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"52%"</span>,</span>
<span id="cb1-46">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"50%"</span>,</span>
<span id="cb1-47">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"72%"</span>,</span>
<span id="cb1-48">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"61%"</span>,</span>
<span id="cb1-49">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"64%"</span>,</span>
<span id="cb1-50">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"47%"</span>,</span>
<span id="cb1-51">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"97%"</span>,</span>
<span id="cb1-52">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-53">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"60%"</span>,</span>
<span id="cb1-54">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-55">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"74%"</span>,</span>
<span id="cb1-56">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-57">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"33%"</span>,</span>
<span id="cb1-58">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34%"</span>,</span>
<span id="cb1-59">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-60">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-61">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"58%"</span>,</span>
<span id="cb1-62">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-63">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"88%"</span>,</span>
<span id="cb1-64">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-65">    ],</span>
<span id="cb1-66">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>: [</span>
<span id="cb1-67">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34%"</span>,</span>
<span id="cb1-68">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20%"</span>,</span>
<span id="cb1-69">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"46%"</span>,</span>
<span id="cb1-70">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"32%"</span>,</span>
<span id="cb1-71">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"24%"</span>,</span>
<span id="cb1-72">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"26%"</span>,</span>
<span id="cb1-73">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"25%"</span>,</span>
<span id="cb1-74">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"36%"</span>,</span>
<span id="cb1-75">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31%"</span>,</span>
<span id="cb1-76">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"32%"</span>,</span>
<span id="cb1-77">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"24%"</span>,</span>
<span id="cb1-78">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"49%"</span>,</span>
<span id="cb1-79">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-80">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"30%"</span>,</span>
<span id="cb1-81">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-82">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"37%"</span>,</span>
<span id="cb1-83">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-84">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"17%"</span>,</span>
<span id="cb1-85">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"17%"</span>,</span>
<span id="cb1-86">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-87">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-88">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"29%"</span>,</span>
<span id="cb1-89">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-90">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"44%"</span>,</span>
<span id="cb1-91">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10%"</span>,</span>
<span id="cb1-92">    ],</span>
<span id="cb1-93">}</span>
<span id="cb1-94"></span>
<span id="cb1-95">dark_navy_blue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0B162A"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># background</span></span>
<span id="cb1-96">light_blue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#B5D3E7"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row</span></span>
<span id="cb1-97">white <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFFFFF"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row</span></span>
<span id="cb1-98">yellow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F6D588"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "reciprocal_tariffs" column</span></span>
<span id="cb1-99">gold <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FFF8DE"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># logo</span></span>
<span id="cb1-100"></span>
<span id="cb1-101"></span>
<span id="cb1-102"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> change_border_radius(</span>
<span id="cb1-103">    x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, border_radius: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, background_color1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, background_color2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span></span>
<span id="cb1-104">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb1-105">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-106"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;div style="background-color: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;border: None"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-107"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;div style="border-radius: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>border_radius<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-108"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                    background-color:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>background_color2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">;"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-109"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">            &amp;nbsp;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-110"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-111"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-112"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb1-113"></span>
<span id="cb1-114"></span>
<span id="cb1-115"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> change_border_radius_expr(</span>
<span id="cb1-116">    cols: pl.Expr,</span>
<span id="cb1-117">    return_dtype: pl.DataType,</span>
<span id="cb1-118">    border_radius: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-119">    background_color1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-120">    background_color2: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>,</span>
<span id="cb1-121">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pl.Expr:</span>
<span id="cb1-122">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cols.map_elements(</span>
<span id="cb1-123">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: change_border_radius(</span>
<span id="cb1-124">            x, border_radius, background_color1, background_color2</span>
<span id="cb1-125">        ),</span>
<span id="cb1-126">        return_dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>return_dtype,</span>
<span id="cb1-127">    )</span>
<span id="cb1-128"></span>
<span id="cb1-129"></span>
<span id="cb1-130">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-131">    pl.DataFrame(data)</span>
<span id="cb1-132">    .with_row_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mod"</span>)</span>
<span id="cb1-133">    .with_columns(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mod"</span>).mod(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>[pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).alias(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(i)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)])</span>
<span id="cb1-134">    .with_columns(</span>
<span id="cb1-135">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "country" and "tariffs_charged" columns</span></span>
<span id="cb1-136">        pl.when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mod"</span>).eq(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-137">        .then(</span>
<span id="cb1-138">            change_border_radius_expr(</span>
<span id="cb1-139">                pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>),</span>
<span id="cb1-140">                pl.String,</span>
<span id="cb1-141">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>,</span>
<span id="cb1-142">                dark_navy_blue,</span>
<span id="cb1-143">                light_blue,</span>
<span id="cb1-144">            )</span>
<span id="cb1-145">        )</span>
<span id="cb1-146">        .otherwise(</span>
<span id="cb1-147">            change_border_radius_expr(</span>
<span id="cb1-148">                pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>),</span>
<span id="cb1-149">                pl.String,</span>
<span id="cb1-150">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>,</span>
<span id="cb1-151">                dark_navy_blue,</span>
<span id="cb1-152">                white,</span>
<span id="cb1-153">            )</span>
<span id="cb1-154">        ),</span>
<span id="cb1-155">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "reciprocal_tariffs" column</span></span>
<span id="cb1-156">        change_border_radius_expr(</span>
<span id="cb1-157">            pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>), pl.String, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>, dark_navy_blue, yellow</span>
<span id="cb1-158">        ),</span>
<span id="cb1-159">    )</span>
<span id="cb1-160">    .select([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>])</span>
<span id="cb1-161">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a row at the end of the table</span></span>
<span id="cb1-162">    .pipe(</span>
<span id="cb1-163">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df_: pl.concat(</span>
<span id="cb1-164">            [df_, pl.DataFrame({col: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df_.columns})], how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span></span>
<span id="cb1-165">        )</span>
<span id="cb1-166">    )</span>
<span id="cb1-167">)</span>
<span id="cb1-168"></span>
<span id="cb1-169"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inner_ring_style, inner_ring_width, inner_ring_color = "dashed", "3px", "orange"</span></span>
<span id="cb1-170"></span>
<span id="cb1-171">(</span>
<span id="cb1-172">    GT(df)</span>
<span id="cb1-173">    .cols_align(</span>
<span id="cb1-174">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>]</span>
<span id="cb1-175">    )</span>
<span id="cb1-176">    .cols_label(</span>
<span id="cb1-177">        {</span>
<span id="cb1-178">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>: html(</span>
<span id="cb1-179">                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-180"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;br&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-181"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-182"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                    </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>logo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">&amp;nbsp;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-183"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                    &lt;span style="color: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>gold<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">; font-size: 40px;"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-184"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                        &amp;nbsp&amp;nbsp;Reciprocal Tariffs</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-185"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                    &lt;/span&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-186"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;/div&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-187"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;br&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-188"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;b&gt;Country&lt;/b&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-189"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">                """</span></span>
<span id="cb1-190">            ),</span>
<span id="cb1-191">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>: html(</span>
<span id="cb1-192">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-193"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;b&gt;Tariffs Charged&lt;br&gt;to the U.S.A.&lt;/b&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-194"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;br&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-195"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;span style="font-size: 12px;"&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-196"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                    Including&lt;br&gt;Currency Manipulation&lt;br&gt;and Trade Barriers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-197"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                &lt;/span&gt;</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb1-198"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                """</span></span>
<span id="cb1-199">            ),</span>
<span id="cb1-200">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>: html(</span>
<span id="cb1-201">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;b&gt;U.S.A. Discounted&lt;br&gt;Reciprocal Tariffs&lt;/b&gt;"</span></span>
<span id="cb1-202">            ),</span>
<span id="cb1-203">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-204">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-205">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-206">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-207">        }</span>
<span id="cb1-208">    )</span>
<span id="cb1-209">    .cols_width(</span>
<span id="cb1-210">        {</span>
<span id="cb1-211">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"50%"</span>,</span>
<span id="cb1-212">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3%"</span>,</span>
<span id="cb1-213">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7%"</span>,</span>
<span id="cb1-214">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7%"</span>,</span>
<span id="cb1-215">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3%"</span>,</span>
<span id="cb1-216">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18%"</span>,</span>
<span id="cb1-217">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"18%"</span>,</span>
<span id="cb1-218">        }</span>
<span id="cb1-219">    )</span>
<span id="cb1-220">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For labels and body:</span></span>
<span id="cb1-221">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the background color of the labels and body to `dark_navy_blue`</span></span>
<span id="cb1-222">    .tab_style(</span>
<span id="cb1-223">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb1-224">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[loc.column_labels(), loc.body()],</span>
<span id="cb1-225">    )</span>
<span id="cb1-226">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For body:</span></span>
<span id="cb1-227">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the border color of the body to `dark_navy_blue`</span></span>
<span id="cb1-228">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the weight and size</span></span>
<span id="cb1-229">    .tab_style(</span>
<span id="cb1-230">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb1-231">            style.borders(sides<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all"</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb1-232">            style.text(weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xx-large"</span>),</span>
<span id="cb1-233">        ],</span>
<span id="cb1-234">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(),</span>
<span id="cb1-235">    )</span>
<span id="cb1-236">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For labels:</span></span>
<span id="cb1-237">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the font, weight, size and color</span></span>
<span id="cb1-238">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># center-align the labels</span></span>
<span id="cb1-239">    .tab_style(</span>
<span id="cb1-240">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb1-241">            style.text(</span>
<span id="cb1-242">                font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>google_font(name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Georgia"</span>),</span>
<span id="cb1-243">                weight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb1-244">                size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"large"</span>,</span>
<span id="cb1-245">                color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>white,</span>
<span id="cb1-246">            ),</span>
<span id="cb1-247">            style.css(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text-align: center;"</span>),</span>
<span id="cb1-248">        ],</span>
<span id="cb1-249">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.column_labels(),</span>
<span id="cb1-250">    )</span>
<span id="cb1-251">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hide the bottom line of the label section</span></span>
<span id="cb1-252">    .tab_options(column_labels_border_bottom_style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hidden"</span>)</span>
<span id="cb1-253">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set the body background color to `dark_navy_blue` for the last row</span></span>
<span id="cb1-254">    .tab_style(</span>
<span id="cb1-255">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dark_navy_blue),</span>
<span id="cb1-256">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(rows<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb1-257">    )</span>
<span id="cb1-258">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># .opt_table_outline(</span></span>
<span id="cb1-259">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     style=inner_ring_style,</span></span>
<span id="cb1-260">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     width=inner_ring_width,</span></span>
<span id="cb1-261">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     color=inner_ring_color,</span></span>
<span id="cb1-262">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># )</span></span>
<span id="cb1-263">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># need to adjust `window_size` to obtain a higher-quality figure</span></span>
<span id="cb1-264">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># .save("reciprocal_tariffs_gt.png", web_driver="firefox", window_size=(1200, 1000))</span></span>
<span id="cb1-265">)</span></code></pre></div></div>
</details>
</div>
<section id="remark" class="level2">
<h2 class="anchored" data-anchor-id="remark">Remark</h2>
<p><em>Added on July 11, 2025</em></p>
<p>I’ve been actively developing the <a href="https://github.com/jrycw/turtle-island"><strong>Turtle Island</strong></a> library and found this example to be a great opportunity to test its usability.</p>
<p>I was genuinely surprised by how well <code>ti.case_when()</code> and <code>ti.is_every_nth_row()</code> worked together to refactor the code into a more concise and expressive form. Since <code>ti.is_every_nth_row()</code> returns an expression, we aren’t actually creating a new column in the DataFrame—it’s more like adding a virtual column.</p>
<p>This experience has further strengthened my confidence in the design and practical value of <strong>Turtle Island</strong>.</p>
<div id="c9b712e5" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>If using <strong>Turtle Island</strong></summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> turtle_island <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ti</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "country" and "tariffs_charged" columns</span></span>
<span id="cb2-4">country_tariffs_charged_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ti.case_when(</span>
<span id="cb2-5">    caselist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb2-6">        (</span>
<span id="cb2-7">            ti.is_every_nth_row(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb2-8">            change_border_radius_expr(</span>
<span id="cb2-9">                pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>),</span>
<span id="cb2-10">                pl.String,</span>
<span id="cb2-11">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>,</span>
<span id="cb2-12">                dark_navy_blue,</span>
<span id="cb2-13">                light_blue,</span>
<span id="cb2-14">            ),</span>
<span id="cb2-15">        )</span>
<span id="cb2-16">    ],</span>
<span id="cb2-17">    otherwise<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>change_border_radius_expr(</span>
<span id="cb2-18">        pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>),</span>
<span id="cb2-19">        pl.String,</span>
<span id="cb2-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>,</span>
<span id="cb2-21">        dark_navy_blue,</span>
<span id="cb2-22">        white,</span>
<span id="cb2-23">    ),</span>
<span id="cb2-24">)</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "reciprocal_tariffs" column</span></span>
<span id="cb2-27">reciprocal_tariffs_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> change_border_radius_expr(</span>
<span id="cb2-28">    pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>),</span>
<span id="cb2-29">    pl.String,</span>
<span id="cb2-30">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5px"</span>,</span>
<span id="cb2-31">    dark_navy_blue,</span>
<span id="cb2-32">    yellow,</span>
<span id="cb2-33">)</span>
<span id="cb2-34"></span>
<span id="cb2-35">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb2-36">    pl.DataFrame(data)</span>
<span id="cb2-37">    .with_columns(</span>
<span id="cb2-38">        country_tariffs_charged_expr,</span>
<span id="cb2-39">        reciprocal_tariffs_expr,</span>
<span id="cb2-40">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>[pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>).alias(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(i)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)],</span>
<span id="cb2-41">    )</span>
<span id="cb2-42">    .select([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tariffs_charged"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reciprocal_tariffs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>])</span>
<span id="cb2-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a row at the end of the table</span></span>
<span id="cb2-44">    .pipe(</span>
<span id="cb2-45">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df_: pl.concat(</span>
<span id="cb2-46">            [df_, pl.DataFrame({col: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> col <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df_.columns})], how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertical"</span></span>
<span id="cb2-47">        )</span>
<span id="cb2-48">    )</span>
<span id="cb2-49">)</span></code></pre></div></div>
</details>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<ol type="1">
<li>This table is intended as a self-practice project, and the data in the table may not be 100% accurate. Please refer to the <a href="https://truthsocial.com/@realDonaldTrump/114270398531479278">original source</a> if you require verified data.</li>
<li>This post was drafted by me, with AI assistance to refine the content.</li>
</ol>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>polars</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table/20250403.html</guid>
  <pubDate>Thu, 03 Apr 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/clone-reciprocal-tariffs-table/reciprocal_tariffs_gt.png" medium="image" type="image/png" height="193" width="144"/>
</item>
<item>
  <title>Applying Custom Color Palettes to the Table Body in Great Tables</title>
  <dc:creator>Jerry Wu</dc:creator>
  <link>https://tech.ycwu.space/posts/table-body-custom-palette/20250225.html</link>
  <description><![CDATA[ 






<p>This post provides a concise guide on styling the table body with custom colors using Pandas and Polars.</p>
<p>There are two primary methods for applying a color palette: one leverages a pre-existing column containing color values, while the other determines colors dynamically based on conditions. I’ll walk you through both approaches.</p>
<p>Since all the generated tables will have the same appearance, I’ll display the final result just once:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://tech.ycwu.space/posts/table-body-custom-palette/table_body_custom_palette.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="Table Body with a Custom Palette"></p>
</figure>
</div>
<section id="preparations" class="level2">
<h2 class="anchored" data-anchor-id="preparations">Preparations</h2>
<p>We’ll start by creating a <code>data</code> dictionary, which will be used with Pandas or Polars later. Additionally, we’ll define a <code>color_mapping</code> dictionary to store the palette information for styling.</p>
<div id="b22e09ff" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> great_tables <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GT, from_column, loc, style</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> selectors <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cs</span>
<span id="cb1-5"></span>
<span id="cb1-6">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>: [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>],</span>
<span id="cb1-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>],</span>
<span id="cb1-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>],</span>
<span id="cb1-10">}</span>
<span id="cb1-11"></span>
<span id="cb1-12">color_mapping <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>}</span></code></pre></div></div>
</div>
</section>
<section id="pandas" class="level2">
<h2 class="anchored" data-anchor-id="pandas">Pandas</h2>
<p>First, we create a Pandas DataFrame called <code>df_pd</code> using <code>data</code> as the input:</p>
<div id="6f9cade2" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">df_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(data)</span>
<span id="cb2-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_pd)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>   col1 col2       color
0     2    x   lightgrey
1     5    y   lightblue
2     7    y   lightblue
3    10    z  papayawhip
4    15    z  papayawhip</code></pre>
</div>
</div>
<section id="using-an-existing-column" class="level3">
<h3 class="anchored" data-anchor-id="using-an-existing-column">Using an Existing Column</h3>
<p>In this straightforward scenario, the DataFrame already contains a predefined column with color names for each row. You can use the <a href="https://posit-dev.github.io/great-tables/reference/from_column.html#great_tables.from_column">from_column()</a> function provided by Great Tables to apply colors to the table body:</p>
<div id="c5db508a" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">(</span>
<span id="cb4-2">    GT(df_pd)</span>
<span id="cb4-3">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb4-4">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb4-5">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb4-6">)</span></code></pre></div></div>
</div>
</section>
<section id="using-functions" class="level3">
<h3 class="anchored" data-anchor-id="using-functions">Using Functions</h3>
<p>In cases where colors need to be determined dynamically based on conditions, the <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.case_when.html#pandas.Series.case_when">pd.Series.case_when()</a> function can be very useful. The following example categorizes the values of the <code>col1</code> column into three different colors:</p>
<div id="a7afde4d" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> color_selector(df_):</span>
<span id="cb5-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>].case_when(</span>
<span id="cb5-3">        [</span>
<span id="cb5-4">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>].lt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;3</span></span>
<span id="cb5-5">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>].lt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;10</span></span>
<span id="cb5-6">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>].ge(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &gt;=10</span></span>
<span id="cb5-7">        ]</span>
<span id="cb5-8">    )</span>
<span id="cb5-9"></span>
<span id="cb5-10"></span>
<span id="cb5-11">(</span>
<span id="cb5-12">    GT(df_pd)</span>
<span id="cb5-13">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_selector), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb5-14">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb5-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb5-16">)</span></code></pre></div></div>
</div>
<p>For categorical-like columns (e.g., <code>col2</code>), a simple value-to-color mapping may be sufficient. In such cases, a predefined dictionary can be used with <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html#pandas.Series.map">pd.Series.map()</a> or <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.replace.html#pandas.Series.replace">pd.Series.replace()</a>:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">pd.Series.map()</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">pd.Series.replace()</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div id="bade3b22" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">(</span>
<span id="cb6-2">    GT(df_pd)</span>
<span id="cb6-3">    .tab_style(</span>
<span id="cb6-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df_: df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(color_mapping)),</span>
<span id="cb6-5">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(),</span>
<span id="cb6-6">    )</span>
<span id="cb6-7">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb6-8">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb6-9">)</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div id="49e1f1c5" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">(</span>
<span id="cb7-2">    GT(df_pd)</span>
<span id="cb7-3">    .tab_style(</span>
<span id="cb7-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> df_: df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].replace(color_mapping)),</span>
<span id="cb7-5">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(),</span>
<span id="cb7-6">    )</span>
<span id="cb7-7">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb7-8">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb7-9">)</span></code></pre></div></div>
</div>
</div>
</div>
</div>
<p>Alternatively, if you prefer sticking with the same approach, <code>pd.Series.case_when()</code> still works:</p>
<div id="9a25034c" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> color_selector(df_):</span>
<span id="cb8-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].case_when(</span>
<span id="cb8-3">        [</span>
<span id="cb8-4">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].eq(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>),</span>
<span id="cb8-5">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].eq(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>),</span>
<span id="cb8-6">            (df_[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>].eq(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>),</span>
<span id="cb8-7">        ]</span>
<span id="cb8-8">    )</span>
<span id="cb8-9"></span>
<span id="cb8-10"></span>
<span id="cb8-11">(</span>
<span id="cb8-12">    GT(df_pd)</span>
<span id="cb8-13">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_selector), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb8-14">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb8-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb8-16">)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="polars" class="level2">
<h2 class="anchored" data-anchor-id="polars">Polars</h2>
<p>Just like before, let’s start by creating a Polars DataFrame named <code>df_pl</code> using <code>data</code> as input:</p>
<div id="97b220ae" class="cell" data-execution_count="8">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">df_pl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.DataFrame(data)</span>
<span id="cb9-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df_pl)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>shape: (5, 3)
┌──────┬──────┬────────────┐
│ col1 ┆ col2 ┆ color      │
│ ---  ┆ ---  ┆ ---        │
│ i64  ┆ str  ┆ str        │
╞══════╪══════╪════════════╡
│ 2    ┆ x    ┆ lightgrey  │
│ 5    ┆ y    ┆ lightblue  │
│ 7    ┆ y    ┆ lightblue  │
│ 10   ┆ z    ┆ papayawhip │
│ 15   ┆ z    ┆ papayawhip │
└──────┴──────┴────────────┘</code></pre>
</div>
</div>
<section id="using-an-existing-column-1" class="level3">
<h3 class="anchored" data-anchor-id="using-an-existing-column-1">Using an Existing Column</h3>
<p>If the DataFrame already contains a column specifying colors, we can apply them directly with <code>from_column()</code>:</p>
<div id="58f04ea0" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">(</span>
<span id="cb11-2">    GT(df_pl)</span>
<span id="cb11-3">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_column(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb11-4">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb11-5">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb11-6">)</span></code></pre></div></div>
</div>
<p>As another option, we can reference the <code>color</code> column using a Polars expression:</p>
<div id="dde96e51" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">(</span>
<span id="cb12-2">    GT(df_pl)</span>
<span id="cb12-3">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb12-4">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb12-5">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb12-6">)</span></code></pre></div></div>
</div>
</section>
<section id="using-conditional-logic-with-polars-expressions" class="level3">
<h3 class="anchored" data-anchor-id="using-conditional-logic-with-polars-expressions">Using Conditional Logic with Polars Expressions</h3>
<p>For cases where colors need to be assigned dynamically, <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.when.html#polars.when">pl.when()</a> provides a structured way to define conditions. The example below assigns colors based on the values in <code>col1</code>:</p>
<div id="8db4bcd4" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">color_selector_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb13-2">    pl.when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>).lt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;3</span></span>
<span id="cb13-3">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>))</span>
<span id="cb13-4">    .when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>).lt(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &lt;10</span></span>
<span id="cb13-5">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>))</span>
<span id="cb13-6">    .when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>).ge(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># &gt;=10</span></span>
<span id="cb13-7">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>))</span>
<span id="cb13-8">)</span>
<span id="cb13-9"></span>
<span id="cb13-10">(</span>
<span id="cb13-11">    GT(df_pl)</span>
<span id="cb13-12">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_selector_expr), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb13-13">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb13-14">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb13-15">)</span></code></pre></div></div>
</div>
<p>For categorical-like columns (e.g., <code>col2</code>), predefined mappings can be applied efficiently using <a href="https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.replace.html#polars.Expr.replace">pl.Expr.replace()</a>:</p>
<div id="5c591fba" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">(</span>
<span id="cb14-2">    GT(df_pl)</span>
<span id="cb14-3">    .tab_style(</span>
<span id="cb14-4">        style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>).replace(color_mapping)),</span>
<span id="cb14-5">        locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body(),</span>
<span id="cb14-6">    )</span>
<span id="cb14-7">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb14-8">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb14-9">)</span></code></pre></div></div>
</div>
<p>Alternatively, if you prefer the conditional approach, <code>pl.when()</code> can still be used:</p>
<div id="3a92eff6" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">color_selector_expr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb15-2">    pl.when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>).eq(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)))</span>
<span id="cb15-3">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightgrey"</span>))</span>
<span id="cb15-4">    .when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>).eq(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>)))</span>
<span id="cb15-5">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>))</span>
<span id="cb15-6">    .when(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>).eq(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z"</span>)))</span>
<span id="cb15-7">    .then(pl.lit(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"papayawhip"</span>))</span>
<span id="cb15-8">)</span>
<span id="cb15-9"></span>
<span id="cb15-10"></span>
<span id="cb15-11">(</span>
<span id="cb15-12">    GT(df_pl)</span>
<span id="cb15-13">    .tab_style(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>style.fill(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>color_selector_expr), locations<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>loc.body())</span>
<span id="cb15-14">    .cols_align(align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb15-15">    .opt_stylize(style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pink"</span>)</span>
<span id="cb15-16">)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="final-notes" class="level2">
<h2 class="anchored" data-anchor-id="final-notes">Final Notes</h2>
<p>In this post, we explored how to use a custom color palette to style table backgrounds with <a href="https://posit-dev.github.io/great-tables/reference/style.fill.html#great_tables.style.fill">style.fill()</a>. The same approach can be applied to customize text color using <a href="https://posit-dev.github.io/great-tables/reference/style.text.html#great_tables.style.text">style.text()</a> or adjust border color with <a href="https://posit-dev.github.io/great-tables/reference/style.borders.html#great_tables.style.borders">style.borders()</a>.</p>
<p>Lastly, remember that <a href="https://posit-dev.github.io/great-tables/reference/loc.body.html#great_tables.loc.body">loc.body()</a> allows you to target specific columns and rows, giving you precise control over table styling.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Disclaimer
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post was drafted by me, with AI assistance to refine the content.</p>
</div>
</div>


</section>

 ]]></description>
  <category>python</category>
  <category>pandas</category>
  <category>polars</category>
  <category>gt</category>
  <guid>https://tech.ycwu.space/posts/table-body-custom-palette/20250225.html</guid>
  <pubDate>Tue, 25 Feb 2025 00:00:00 GMT</pubDate>
  <media:content url="https://tech.ycwu.space/posts/table-body-custom-palette/table_body_custom_palette.png" medium="image" type="image/png" height="190" width="144"/>
</item>
</channel>
</rss>
