Skip to content

Commit

Permalink
Add top-level index.html to redirect to viewer index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Jan 29, 2025
1 parent b327067 commit 08971ce
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions e3sm_diags/viewer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ def create_viewer(parameters: List[CoreParameter]) -> str:
certain extension and create the viewer in root_dir.
"""
root_dir = parameters[0].results_dir
viewer_dir = os.path.join(root_dir, "viewer")

if not os.path.exists(root_dir):
os.makedirs(root_dir)
if not os.path.exists(viewer_dir):
os.makedirs(viewer_dir)

# Group each parameter object based on the `sets` parameter.
set_to_parameters = collections.defaultdict(list)
Expand All @@ -137,17 +138,48 @@ def create_viewer(parameters: List[CoreParameter]) -> str:

# Now call the viewers with the list of parameters as the arguments.
for set_name, parameters in set_to_parameters.items():
logger.info(f"{set_name} {root_dir}")
logger.info(f"{set_name} {viewer_dir}")
viewer_function = SET_TO_VIEWER[set_name]
result = viewer_function(root_dir, parameters)
result = viewer_function(viewer_dir, parameters)
logger.info(result)
title_and_url_list.append(result)

# Add the provenance in the index as well.
prov_tuple = ("Provenance", "../prov")
title_and_url_list.append(prov_tuple)

index_url = create_index(root_dir, title_and_url_list)
utils.add_header(root_dir, index_url, parameters)
index_url = create_index(viewer_dir, title_and_url_list)
_create_root_index(root_dir, index_url)

utils.add_header(viewer_dir, index_url, parameters)

return index_url


def _create_root_index(root_dir: str, viewer_index_url: str):
"""Create a root level `index.html` file that redirects to the viewer index.
Parameters
----------
root_dir : str
The root directory.
index_url : str
The url to the viewer index.html file.
"""
root_index_path = os.path.join(root_dir, "index.html")
relative_viewer_index_url = os.path.relpath(viewer_index_url, root_dir)
root_soup = BeautifulSoup(
f"""
<html>
<head>
<meta http-equiv='refresh' content='0; url={relative_viewer_index_url}' />
</head>
<body></body>
</html>
""",
"lxml",
)

# Write the root index file
with open(root_index_path, "wb") as f:
f.write(root_soup.prettify("utf-8"))

0 comments on commit 08971ce

Please sign in to comment.