Skip to content

Commit 9ab39c9

Browse files
committed
Standardize plotly rendering function
1 parent 117f23e commit 9ab39c9

File tree

6 files changed

+42
-35
lines changed

6 files changed

+42
-35
lines changed

book/cloud.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ import datetime
5050
from pathlib import Path
5151
import plotly.express as px
5252
import plotly.graph_objects as go
53-
import plotly.io as pio
5453
from plotly.subplots import make_subplots
5554
from textwrap import dedent
56-
57-
pio.renderers.default = "notebook"
5855
from IPython.display import Markdown, display
5956
import requests
6057
from rich.progress import track
58+
import twoc
6159
from twoc import colors
60+
61+
twoc.set_plotly_defaults()
6262
```
6363

6464
+++ {"editable": true, "slideshow": {"slide_type": ""}}
@@ -172,13 +172,7 @@ slideshow:
172172
slide_type: ''
173173
tags: [remove-cell]
174174
---
175-
import pandas as pd
176-
import pandas as pd
177-
import plotly.express as px
178-
import numpy as np
179175
from plotly.express.colors import qualitative
180-
import plotly.io as pio
181-
pio.renderers.default = "notebook"
182176
```
183177

184178
```{code-cell} ipython3
@@ -479,7 +473,7 @@ for i, scale in enumerate(scale_ordering, 1):
479473
fig_bins.update_xaxes(title_text=f"{scale.capitalize()} Active Users", tickangle=-45, row=1, col=i)
480474
fig_bins.update_yaxes(title_text="Number of communities", range=[0, max_y_bins], row=1, col=i)
481475
fig_bins.update_layout(title_text="Number of communities in bins of active users", showlegend=False)
482-
display(fig_bins)
476+
fig_bins.show()
483477
484478
# Create subplots for percentage breakdown
485479
fig_perc = make_subplots(rows=1, cols=len(scale_ordering), subplot_titles=[s.capitalize() for s in scale_ordering])
@@ -500,7 +494,7 @@ for i, scale in enumerate(scale_ordering, 1):
500494
fig_perc.update_xaxes(title_text="Bin", tickangle=-45, row=1, col=i)
501495
fig_perc.update_yaxes(title_text="% of users", tickformat='.0%', range=[0, 1], row=1, col=i)
502496
fig_perc.update_layout(title_text="% Total Active Users by community size", showlegend=False)
503-
display(fig_perc)
497+
fig_perc.show()
504498
```
505499

506500
### Date of First Value

book/finances.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ import twoc
6161
from twoc.dates import round_to_nearest_month
6262
6363
twoc.set_plotly_defaults()
64-
import plotly.io as pio
65-
# Use notebook renderer to output HTML for Jupyter Book
66-
pio.renderers.default = "notebook"
6764
6865
# This just suppresses a warning
6966
pd.set_option("future.no_silent_downcasting", True)
@@ -367,6 +364,7 @@ for fig in figures.values():
367364
fig.show()
368365
```
369366

367+
(undertanding-revenue)=
370368
## Understanding revenue
371369

372370
The plots above show our revenue projections under different assumptions, going 18 months into the future:

book/marketing.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ import xml.etree.ElementTree as ET
3131
from datetime import datetime
3232
import plotly.express as px
3333
import pandas as pd
34-
import plotly.io as pio
34+
import twoc
3535
36-
# Set plotly renderer for static HTML output
37-
pio.renderers.default = "plotly_mimetype+notebook"
36+
twoc.set_plotly_defaults()
3837
3938
# Fetch and parse RSS feed
4039
response = requests.get("https://2i2c.org/blog/index.xml")

book/people.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ slideshow:
202202
slide_type: ''
203203
tags: [remove-input]
204204
---
205-
import plotly.io as pio
206205
import plotly.express as px
207-
pio.renderers.default = "notebook"
206+
import twoc
207+
208+
twoc.set_plotly_defaults()
208209
209210
df["last day plus one"] = df["last day"] + timedelta(days=1)
210211

book/twoc/__init__.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,39 @@
22
A helper module to quickly generate visualizations that use 2i2c's colors and style.
33
Importing this modifies plotly defaults to use these colors.
44
"""
5+
56
colors = dict(
6-
bigblue = "#1D4EF5",
7-
paleblue = "#F2F5FC",
8-
midnight = "#230344",
9-
mauve = "#B86BFC",
10-
forest = "#057761",
11-
lightgreen = "#0CEFAE",
12-
magenta = "#C60A76",
13-
pink = "#FF808B",
14-
coral = "#FF4E4F",
15-
yellow = "#FFDE17",
7+
bigblue="#1D4EF5",
8+
paleblue="#F2F5FC",
9+
midnight="#230344",
10+
mauve="#B86BFC",
11+
forest="#057761",
12+
lightgreen="#0CEFAE",
13+
magenta="#C60A76",
14+
pink="#FF808B",
15+
coral="#FF4E4F",
16+
yellow="#FFDE17",
1617
)
18+
19+
1720
def set_plotly_defaults():
18-
"""Update plotly defaults to use these colors."""
21+
"""Update plotly defaults to use 2i2c colors and notebook renderer for Jupyter Book."""
1922
import plotly.io as pio
23+
24+
# Use notebook renderer for HTML output in Jupyter Book
25+
pio.renderers.default = "notebook"
2026
pio.templates.default = "plotly_white"
2127
custom_template = pio.templates["plotly_white"].layout.template
22-
custom_template.layout.update(plot_bgcolor=colors["paleblue"], paper_bgcolor=colors["paleblue"])
23-
custom_template.layout.colorway = [colors["bigblue"], colors["coral"], colors["lightgreen"], colors["magenta"], colors["pink"], colors["yellow"]]
28+
custom_template.layout.update(
29+
plot_bgcolor=colors["paleblue"], paper_bgcolor=colors["paleblue"]
30+
)
31+
custom_template.layout.colorway = [
32+
colors["bigblue"],
33+
colors["coral"],
34+
colors["lightgreen"],
35+
colors["magenta"],
36+
colors["pink"],
37+
colors["yellow"],
38+
]
2439
pio.templates["custom_theme"] = custom_template
25-
pio.templates.default = "custom_theme"
40+
pio.templates.default = "custom_theme"

book/upstream.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ from subprocess import run
3838
import pandas as pd
3939
import plotly.express as px
4040
import plotly.graph_objects as go
41-
import plotly.io as pio
4241
from IPython.display import Markdown, display
42+
import twoc
4343
from twoc import colors as twoc_colors
4444
from yaml import safe_load
4545
46-
pio.renderers.default = "notebook"
46+
twoc.set_plotly_defaults()
4747
4848
# Colors we'll re-use
4949
TWOC_PALETTE = [

0 commit comments

Comments
 (0)