-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from statisticsnorway/functions
Added template and test for template
- Loading branch information
Showing
7 changed files
with
213 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# %% [markdown] | ||
# # Demo | ||
# | ||
# Creating some Plotly figures and a table with the ssb_plotly_template | ||
|
||
# %% | ||
# import | ||
|
||
import os | ||
import pandas as pd | ||
|
||
# %% | ||
# Change directory until find project root | ||
notebook_path = os.getcwd() | ||
for folder_level in range(50): | ||
if "pyproject.toml" in os.listdir(): | ||
break | ||
os.chdir("../") | ||
|
||
# %% | ||
# import plotly template | ||
# ruff: noqa: F401 | ||
import ssb_plotly_template.ssb_plotly_template | ||
|
||
# %% | ||
import plotly.graph_objects as go | ||
|
||
data = { | ||
"Category": ["A", "B", "C", "D"], | ||
"Values": [10, 23, 15, 7], | ||
"Values2": [13, 22, 25, 17], | ||
"Values3": [23, 23, 28, 27], | ||
"Dates": ["2024-10-01", "2024-10-05", "2024-10-10", "2024-10-15"], | ||
} | ||
|
||
df = pd.DataFrame(data) | ||
df["Dates"] = pd.to_datetime(df["Dates"]) | ||
|
||
|
||
bar_chart = go.Figure( | ||
data=[go.Bar(x=df["Category"], y=df["Values"])], | ||
layout_title_text="Bar Chart", | ||
layout_template="ssb_plotly_template", | ||
) | ||
|
||
|
||
bar_chart.show() | ||
|
||
# %% | ||
# Create a timeline (scatter plot) with multiple lines | ||
timeline_chart = go.Figure( | ||
data=[ | ||
go.Scatter(x=df["Dates"], y=df["Values"], mode="lines+markers", name="Line 1"), | ||
go.Scatter(x=df["Dates"], y=df["Values2"], mode="lines+markers", name="Line 2"), | ||
go.Scatter(x=df["Dates"], y=df["Values3"], mode="lines+markers", name="Line 2"), | ||
], | ||
layout_title_text="Timeline", | ||
layout_template="ssb_plotly_template", | ||
) | ||
|
||
timeline_chart.show() | ||
|
||
# %% | ||
# Create Plotly Table | ||
fig_table = go.Figure( | ||
data=[ | ||
go.Table( | ||
header=dict(values=list(df.columns)), | ||
cells=dict(values=[df[col].tolist() for col in df.columns]), | ||
) | ||
] | ||
) | ||
|
||
fig_table.update_layout(template="ssb_plotly_template", title="Table") | ||
|
||
fig_table.show() | ||
|
||
# %% |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import plotly.graph_objects as go | ||
import plotly.io as pio | ||
|
||
pio.templates["ssb_plotly_template"] = go.layout.Template( | ||
layout={ | ||
"title": { | ||
"font": {"family": "Roboto, Sans-serif", "size": 20, "color": "#162327"}, | ||
"pad": {"b": 28}, # (linjeavstand) | ||
}, | ||
# Fonts for other text and labels | ||
"font": {"family": "Open Sans, Sans-serif", "size": 16, "color": "#162327"}, | ||
# SSB sorted priority colours | ||
"colorway": [ | ||
"#1A9D49", | ||
"#075745", | ||
"#1D9DE2", | ||
"#0F2080", | ||
"#C78800", | ||
"#471F00", | ||
"#C775A7", | ||
"#A3136C", | ||
"#909090", | ||
], | ||
# Framework lines and background colors | ||
"xaxis": { | ||
"showgrid": False, | ||
"linecolor": "#274247", | ||
"ticks": "outside", | ||
"zeroline": False, | ||
"showline": True, | ||
"tickmode": "linear", | ||
}, | ||
"yaxis": { | ||
"showgrid": True, | ||
"gridcolor": "#C3DCDC", | ||
"linecolor": "#274247", | ||
"ticks": "outside", | ||
"zeroline": False, | ||
"showline": True, | ||
}, | ||
"plot_bgcolor": "#FFFFFF", | ||
}, | ||
data={ | ||
# Scatter plot settings | ||
"scatter": [ | ||
go.Scatter( | ||
mode="lines+markers", | ||
marker=dict(symbol="circle", size=8), | ||
line=dict(width=2), | ||
) | ||
], | ||
# Table settings | ||
"table": [ | ||
go.Table( | ||
header=dict( | ||
fill_color="#FFFFFF", | ||
font=dict(color="#162327", size=18), | ||
align="left", | ||
height=40, | ||
line=dict(color="#C3DCDC", width=1), | ||
), | ||
cells=dict( | ||
fill_color=[["#ECFEED", "#FFFFFF"] * 100], | ||
font=dict(color="#162327", size=16), | ||
align="right", | ||
height=40, | ||
line=dict(color="#C3DCDC", width=1), | ||
), | ||
) | ||
], | ||
}, | ||
) | ||
|
||
pio.templates.default = "ssb_plotly_template" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
from ssb_plotly_template.functions import example_function | ||
# test_plotly_template.py | ||
import plotly.io as pio | ||
import ssb_plotly_template.ssb_plotly_template # noqa: F401 | ||
|
||
|
||
def test_example_function() -> None: | ||
assert example_function(1, 2) == "1 is less than 2" | ||
assert example_function(1, 0) == "1 is greater than or equal to 0" | ||
def test_ssb_plotly_template() -> None: | ||
|
||
# Check if the template is correctly applied | ||
template = pio.templates["ssb_plotly_template"] | ||
|
||
# Check that the font family in the layout is set correctly | ||
assert ( | ||
template.layout.font.family == "Open Sans, Sans-serif" | ||
), "Font family should be 'Open Sans, Sans-serif'" | ||
|
||
# Check that the title font color is correct | ||
assert ( | ||
template.layout.title.font.color == "#162327" | ||
), "Title font color should be '#162327'" | ||
|
||
# Check that the x-axis line color is correct | ||
assert ( | ||
template.layout.xaxis.linecolor == "#274247" | ||
), "x-axis line color should be '#274247'" |
This file was deleted.
Oops, something went wrong.