Skip to content

Commit a622f15

Browse files
committed
✨ make file path relative w.r.t. to report_manager.py
- currently the section_dir is set with the knowleadge of the streamlit report structure - Allows to execute report from everywhere on that operating system - folder with streamlit report cannot be copied though.
1 parent 402e842 commit a622f15

File tree

15 files changed

+176
-78
lines changed

15 files changed

+176
-78
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,17 @@ def _generate_plot_content(self, plot) -> List[str]:
666666
# If the file path is a URL, keep the file path as is
667667
if is_url(plot.file_path):
668668
plot_file_path = plot.file_path
669+
plot_content.append(f"plot_file_path = '{plot_file_path}'")
669670
else: # If it's a local file
670-
plot_file_path = get_relative_file_path(plot.file_path).as_posix()
671+
plot_file_path = get_relative_file_path(
672+
plot.file_path, relative_to=self.section_dir
673+
).as_posix()
674+
plot_content.append(
675+
f"plot_file_path = (section_dir / '{plot_file_path}')"
676+
".resolve().as_posix()"
677+
)
671678
plot_content.append(
672-
f"\nst.image('{plot_file_path}', "
679+
"st.image(plot_file_path,"
673680
f" caption='{plot.caption}', use_column_width=True)\n"
674681
)
675682
elif plot.plot_type == r.PlotType.PLOTLY:
@@ -708,11 +715,14 @@ def _generate_plot_content(self, plot) -> List[str]:
708715
)
709716
)
710717
else:
711-
fpath = get_relative_file_path(html_plot_file).as_posix()
718+
fpath = get_relative_file_path(
719+
html_plot_file, relative_to=self.section_dir
720+
).as_posix()
712721
plot_content.append(
713722
textwrap.dedent(
714723
f"""
715-
with open('{fpath}', 'r') as html_file:
724+
file_path = (section_dir / '{fpath}').resolve().as_posix()
725+
with open(file_path, 'r') as html_file:
716726
html_content = html_file.read()
717727
"""
718728
)
@@ -779,10 +789,13 @@ def _generate_plot_code(self, plot) -> str:
779789
plot_json = json.loads(response.text)\n"""
780790
)
781791
else: # If it's a local file
782-
plot_rel_path = get_relative_file_path(plot.file_path)
792+
plot_rel_path = get_relative_file_path(
793+
plot.file_path, relative_to=self.section_dir
794+
).as_posix()
783795
plot_code = textwrap.dedent(
784796
f"""
785-
with open('{plot_rel_path.as_posix()}', 'r') as plot_file:
797+
file_path = (section_dir / '{plot_rel_path}').resolve().as_posix()
798+
with open(file_path, 'r') as plot_file:
786799
plot_json = json.load(plot_file)\n"""
787800
)
788801

@@ -873,11 +886,14 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
873886
sheet_names = table_utils.get_sheet_names(df_file_path.as_posix())
874887
if len(sheet_names) > 1:
875888
# If there are multiple sheets, ask the user to select one
876-
fpath = df_file_path.as_posix()
889+
fpath = get_relative_file_path(
890+
dataframe.file_path, relative_to=self.section_dir
891+
).as_posix()
877892
dataframe_content.append(
878893
textwrap.dedent(
879894
f"""\
880-
sheet_names = table_utils.get_sheet_names("{fpath}")
895+
file_path = (section_dir / '{fpath}').resolve().as_posix()
896+
sheet_names = table_utils.get_sheet_names(file_path)
881897
selected_sheet = st.selectbox("Select a sheet to display",
882898
options=sheet_names,
883899
)
@@ -886,18 +902,27 @@ def _generate_dataframe_content(self, dataframe) -> List[str]:
886902
)
887903

888904
# Load the DataFrame using the correct function
889-
read_function = read_function_mapping[file_extension]
905+
df_file_path = get_relative_file_path(
906+
dataframe.file_path, relative_to=self.section_dir
907+
).as_posix()
908+
read_function = read_function_mapping[file_extension].__name__
890909
if file_extension in [
891910
r.DataFrameFormat.XLS.value_with_dot,
892911
r.DataFrameFormat.XLSX.value_with_dot,
893912
]:
894913
dataframe_content.append(
895-
f"df = pd.{read_function.__name__}('{df_file_path.as_posix()}',"
896-
" sheet_name=selected_sheet)\n"
914+
textwrap.dedent(
915+
f"""\
916+
file_path = (section_dir / '{df_file_path}').resolve()
917+
df = pd.{read_function}(file_path, sheet_name=selected_sheet)
918+
"""
919+
)
897920
)
898921
else:
899922
dataframe_content.append(
900-
f"df = pd.{read_function.__name__}('{df_file_path.as_posix()}')\n"
923+
f"file_path = (section_dir / '{df_file_path}'"
924+
").resolve().as_posix()\n"
925+
f"df = pd.{read_function}(file_path)\n"
901926
)
902927
# ! Alternative to select box: iterate over sheets in DataFrame
903928
# Displays a DataFrame using AgGrid with configurable options.
@@ -991,11 +1016,15 @@ def _generate_markdown_content(self, markdown) -> List[str]:
9911016
)
9921017
)
9931018
else: # If it's a local file
994-
md_rel_path = get_relative_file_path(markdown.file_path)
1019+
md_rel_path = get_relative_file_path(
1020+
markdown.file_path, relative_to=self.section_dir
1021+
).as_posix()
1022+
9951023
markdown_content.append(
9961024
textwrap.dedent(
9971025
f"""
998-
with open('{md_rel_path.as_posix()}', 'r') as markdown_file:
1026+
file_path = (section_dir / '{md_rel_path}').resolve().as_posix()
1027+
with open(file_path, 'r') as markdown_file:
9991028
markdown_content = markdown_file.read()
10001029
"""
10011030
)
@@ -1061,13 +1090,16 @@ def _generate_html_content(self, html) -> List[str]:
10611090
)
10621091
)
10631092
else: # If it's a local file
1064-
html_rel_path = get_relative_file_path(html.file_path).as_posix()
1093+
html_rel_path = get_relative_file_path(
1094+
html.file_path, relative_to=self.section_dir
1095+
).as_posix()
10651096
html_content.append(
10661097
textwrap.dedent(
1067-
f"""
1068-
with open('{html_rel_path}', 'r', encoding='utf-8') as f:
1069-
html_content = f.read()
1070-
"""
1098+
f"""\
1099+
file_path = (section_dir / '{html_rel_path}').resolve().as_posix()
1100+
with open(file_path, 'r', encoding='utf-8') as f:
1101+
html_content = f.read()
1102+
"""
10711103
)
10721104
)
10731105

@@ -1385,7 +1417,11 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
13851417
}
13861418

13871419
component_type = component.component_type
1388-
component_imports = ["import streamlit as st"]
1420+
component_imports = [
1421+
"import streamlit as st",
1422+
"from pathlib import Path",
1423+
"section_dir = Path(__file__).resolve().parent.parent",
1424+
]
13891425

13901426
# Add relevant imports based on component type and visualization tool
13911427
if component_type == r.ComponentType.PLOT:

tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/sections/Dataframes/All_Formats.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from pathlib import Path
12
from st_aggrid import AgGrid, GridOptionsBuilder
23
from vuegen import table_utils
34
import pandas as pd
45
import streamlit as st
56
df_index = 1
7+
section_dir = Path(__file__).resolve().parent.parent
68

79

810
st.markdown(
@@ -20,7 +22,8 @@
2022
),
2123
unsafe_allow_html=True)
2224

23-
df = pd.read_csv('docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/1_phyla_correlation_network_csv.csv')
25+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/1_phyla_correlation_network_csv.csv').resolve().as_posix()
26+
df = pd.read_csv(file_path)
2427

2528

2629
# Displays a DataFrame using AgGrid with configurable options.
@@ -59,12 +62,14 @@
5962
unsafe_allow_html=True)
6063

6164
selected_sheet = 0
62-
sheet_names = table_utils.get_sheet_names("docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/2_abundance_table_example_xls.xls")
65+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/2_abundance_table_example_xls.xls').resolve().as_posix()
66+
sheet_names = table_utils.get_sheet_names(file_path)
6367
selected_sheet = st.selectbox("Select a sheet to display",
6468
options=sheet_names,
6569
)
6670

67-
df = pd.read_excel('docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/2_abundance_table_example_xls.xls', sheet_name=selected_sheet)
71+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/2_abundance_table_example_xls.xls').resolve()
72+
df = pd.read_excel(file_path, sheet_name=selected_sheet)
6873

6974

7075
# Displays a DataFrame using AgGrid with configurable options.
@@ -102,7 +107,8 @@
102107
),
103108
unsafe_allow_html=True)
104109

105-
df = pd.read_table('docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/3_sample_info_example_txt.txt')
110+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/3_sample_info_example_txt.txt').resolve().as_posix()
111+
df = pd.read_table(file_path)
106112

107113

108114
# Displays a DataFrame using AgGrid with configurable options.
@@ -140,7 +146,8 @@
140146
),
141147
unsafe_allow_html=True)
142148

143-
df = pd.read_parquet('docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/4_sample_info_example_parquet.parquet')
149+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/4_sample_info_example_parquet.parquet').resolve().as_posix()
150+
df = pd.read_parquet(file_path)
144151

145152

146153
# Displays a DataFrame using AgGrid with configurable options.
@@ -179,7 +186,8 @@
179186
unsafe_allow_html=True)
180187

181188
selected_sheet = 0
182-
df = pd.read_excel('docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/5_example_xlsx.xlsx', sheet_name=selected_sheet)
189+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/2_Dataframes/1_All_formats/5_example_xlsx.xlsx').resolve()
190+
df = pd.read_excel(file_path, sheet_name=selected_sheet)
183191

184192

185193
# Displays a DataFrame using AgGrid with configurable options.

tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/sections/Html/All_Html.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
from pathlib import Path
2+
13
import requests
24
import streamlit as st
35

6+
section_dir = Path(__file__).resolve().parent.parent
7+
48

59
st.markdown(
610
(
@@ -17,8 +21,8 @@
1721
),
1822
unsafe_allow_html=True)
1923

20-
21-
with open('docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/1_plot.html', 'r', encoding='utf-8') as f:
24+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/1_plot.html').resolve().as_posix()
25+
with open(file_path, 'r', encoding='utf-8') as f:
2226
html_content = f.read()
2327

2428
st.components.v1.html(html_content, height=600, scrolling=True)
@@ -32,7 +36,8 @@
3236
unsafe_allow_html=True)
3337

3438

35-
with open('docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/2_ckg_network.html', 'r') as html_file:
39+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/2_ckg_network.html').resolve().as_posix()
40+
with open(file_path, 'r') as html_file:
3641
html_content = html_file.read()
3742

3843

@@ -58,8 +63,8 @@
5863
),
5964
unsafe_allow_html=True)
6065

61-
62-
with open('docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/3_multiqc_report.html', 'r', encoding='utf-8') as f:
66+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/4_Html/1_All_html/3_multiqc_report.html').resolve().as_posix()
67+
with open(file_path, 'r', encoding='utf-8') as f:
6368
html_content = f.read()
6469

6570
st.components.v1.html(html_content, height=600, scrolling=True)

tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/sections/Markdown/All_Markdown.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from pathlib import Path
12
import requests
23
import streamlit as st
4+
section_dir = Path(__file__).resolve().parent.parent
35

46

57
st.markdown(
@@ -18,7 +20,8 @@
1820
unsafe_allow_html=True)
1921

2022

21-
with open('docs/example_data/Basic_example_vuegen_demo_notebook/5_Markdown/1_All_markdown/README.md', 'r') as markdown_file:
23+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/5_Markdown/1_All_markdown/README.md').resolve().as_posix()
24+
with open(file_path, 'r') as markdown_file:
2225
markdown_content = markdown_file.read()
2326

2427
st.markdown(markdown_content, unsafe_allow_html=True)

tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/sections/Networks/Interactive_Networks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from pathlib import Path
12
import requests
23
import streamlit as st
4+
section_dir = Path(__file__).resolve().parent.parent
35

46

57
st.markdown(
@@ -26,7 +28,8 @@
2628
unsafe_allow_html=True)
2729

2830

29-
with open('tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/static/Man_Example.html', 'r') as html_file:
31+
file_path = (section_dir / '../static/Man_Example.html').resolve().as_posix()
32+
with open(file_path, 'r') as html_file:
3033
html_content = html_file.read()
3134

3235

@@ -53,7 +56,8 @@
5356
unsafe_allow_html=True)
5457

5558

56-
with open('docs/example_data/Basic_example_vuegen_demo_notebook/3_Networks/1_Interactive_networks/description.md', 'r') as markdown_file:
59+
file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/3_Networks/1_Interactive_networks/description.md').resolve().as_posix()
60+
with open(file_path, 'r') as markdown_file:
5761
markdown_content = markdown_file.read()
5862

5963
st.markdown(markdown_content, unsafe_allow_html=True)

tests/report_examples/Basic_example_vuegen_demo_notebook/streamlit_report/sections/Networks/Static_Networks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from pathlib import Path
12
import streamlit as st
3+
section_dir = Path(__file__).resolve().parent.parent
24

35

46
st.markdown(
@@ -16,8 +18,8 @@
1618
),
1719
unsafe_allow_html=True)
1820

19-
20-
st.image('docs/example_data/Basic_example_vuegen_demo_notebook/3_Networks/2_Static_networks/1_phyla_correlation_network.png', caption='', use_column_width=True)
21+
plot_file_path = (section_dir / '../../../../../docs/example_data/Basic_example_vuegen_demo_notebook/3_Networks/2_Static_networks/1_phyla_correlation_network.png').resolve().as_posix()
22+
st.image(plot_file_path, caption='', use_column_width=True)
2123

2224
footer = '''
2325
<style type="text/css">

0 commit comments

Comments
 (0)