-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (49 loc) · 2.21 KB
/
main.py
File metadata and controls
66 lines (49 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import inspect
import pathlib as pl
import pandas as pd
import pandas.io.formats.style
import df2html.part_s.p00_get_code_name_var as p00
import df2html.part_s.p01_set_styles as p01
def save(
df: pd.DataFrame | pandas.io.formats.style.Styler,
name: str | None = None,
styles_extended: None | list | dict = None, # api/pandas.io.formats.style.Styler.set_table_styles.html
) -> tuple[str, str]:
if not isinstance(df, pd.DataFrame | pandas.io.formats.style.Styler):
raise RuntimeError('df should be pd.DataFrame OR pandas.io.formats.style.Styler type')
caller_frame = inspect.stack()[1]
if name is None:
_, name = p00.var_name_from_frame(
caller_frame=caller_frame,
trg=df,
default_name='df',
)
the_path = pl.Path(name if name.endswith('.html') else f'{name}.html')
style = df.style.format(hyperlinks='html') if isinstance(df, pd.DataFrame) else df
style = p01.set_jupyter_styles(style, styles_extended)
the_html = style.to_html()
the_path.write_text(the_html)
return (
f'file://{str(the_path.resolve())}', # link to saved file
f'File "{caller_frame.filename}", line {caller_frame.lineno}', # for quick navigation in code (some IDEs)
)
if __name__ == '__main__':
import df2html
the_df: pd.DataFrame = pd.DataFrame({
'planet': ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'],
'link': [
'https://en.wikipedia.org/wiki/Mercury_(planet)',
'https://en.wikipedia.org/wiki/Venus',
'https://en.wikipedia.org/wiki/Earth',
'https://en.wikipedia.org/wiki/Mars',
'https://en.wikipedia.org/wiki/Jupiter',
'https://en.wikipedia.org/wiki/Saturn',
'https://en.wikipedia.org/wiki/Uranus',
'https://en.wikipedia.org/wiki/Neptune',
],
'radius_km': [2440, 6052, 6371, 3390, 69911, 58232, 25362, 24622],
})
the_style = the_df.style
the_style.format(hyperlinks='html')
df2html.save(the_style, name='df')
print(df2html.save(the_df.head(3), styles_extended=[{'selector': '', 'props': [('font-size', '10px')]}]))