77
88
99def 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
1537def 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
2469def 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