Skip to content

Commit

Permalink
Merge pull request #3 from statisticsnorway/functions
Browse files Browse the repository at this point in the history
Added template and test for template
  • Loading branch information
lalelisealstad authored Oct 18, 2024
2 parents e1cd9eb + 86da728 commit 2e2ab7e
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 58 deletions.
78 changes: 78 additions & 0 deletions demos/demo.py
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()

# %%
32 changes: 31 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changelog = "https://github.com/statisticsnorway/ssb-plotly-template/releases"
[tool.poetry.dependencies]
python = "^3.10"
click = ">=8.0.1"
plotly = "^5.24.1"

[tool.poetry.group.dev.dependencies]
pygments = ">=2.10.0"
Expand Down Expand Up @@ -124,3 +125,10 @@ classmethod-decorators = ["classmethod", "validator", "root_validator", "pydanti
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"


[[tool.mypy.overrides]]
module = [
"plotly.*",
]
ignore_missing_imports = true
35 changes: 0 additions & 35 deletions src/ssb_plotly_template/functions.py

This file was deleted.

74 changes: 74 additions & 0 deletions src/ssb_plotly_template/ssb_plotly_template.py
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"
26 changes: 22 additions & 4 deletions tests/test_functions.py
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'"
18 changes: 0 additions & 18 deletions tests/test_main.py

This file was deleted.

0 comments on commit 2e2ab7e

Please sign in to comment.