Skip to content

Commit 2fd4bbe

Browse files
authored
Add doctests in utils.py (#18)
1 parent 025593b commit 2fd4bbe

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
addopts = --verbose --color=yes --durations=5 --showlocals
2+
addopts = --verbose --color=yes --durations=5 --showlocals --doctest-modules sphinx_plotly_directive/utils.py

sphinx_plotly_directive/utils.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,57 @@
77

88

99
def save_plotly_figure(fig, path):
10+
r"""
11+
Save a Plotly figure.
12+
13+
Parameters
14+
----------
15+
fig : plotly figure
16+
A plotly figure to save.
17+
path : str
18+
A file path.
19+
20+
Returns
21+
-------
22+
None
23+
24+
Examples
25+
--------
26+
>>> import plotly.express as px
27+
>>> import tempfile
28+
>>> fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
29+
>>> path = tempfile.NamedTemporaryFile(suffix=".html").name
30+
>>> save_plotly_figure(fig, path)
31+
"""
1032
fig_html = plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn", auto_open=False)
1133
with open(path, "w") as f:
1234
f.write(fig_html)
1335

1436

1537
def assign_last_line_into_variable(code, variable_name):
38+
r"""
39+
Save a Plotly figure.
40+
41+
Parameters
42+
----------
43+
code : str
44+
A string representing code.
45+
name : str
46+
A variable name.
47+
48+
Returns
49+
-------
50+
str
51+
Mew code.
52+
53+
Examples
54+
--------
55+
>>> code = "a = 1\nfunc(a)"
56+
>>> new_code = assign_last_line_into_variable(code, "b")
57+
>>> print(new_code)
58+
a = 1
59+
b = func(a)
60+
"""
1661
lines = code.split("\n")
1762
for idx in range(len(lines) - 1, -1, -1):
1863
if lines[idx].strip() != "":
@@ -22,6 +67,41 @@ def assign_last_line_into_variable(code, variable_name):
2267

2368

2469
def create_directive_block(name, arguments, options, content):
70+
r"""
71+
Create a directive block.
72+
73+
Parameters
74+
----------
75+
name : str
76+
A directive name.
77+
arguments : list of str
78+
Arguments of the directive.
79+
option : dict
80+
Option of the directive.
81+
content : list of str
82+
Content of the directive.
83+
84+
Returns
85+
-------
86+
str
87+
A directive block.
88+
89+
Examples
90+
--------
91+
>>> block = create_directive_block(
92+
... "plotly",
93+
... ["f1", "f2"],
94+
... {"a": 0, "b": 1},
95+
... ["l1", "l2"],
96+
... )
97+
>>> print(block)
98+
.. plotly:: f1 f2
99+
:a: 0
100+
:b: 1
101+
<BLANKLINE>
102+
l1
103+
l2
104+
"""
25105
header = ".. {}:: ".format(name) + " ".join(arguments)
26106
code = "\n".join(map(str, content))
27107

0 commit comments

Comments
 (0)