Skip to content

Commit 2261961

Browse files
authored
Merge pull request #82 from eliteportal/DPE-1247-workflow-download-update
[DPE-1247] Update template generation script to include download link
2 parents c0ceb1a + 4a833fa commit 2261961

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

processes/page_manager.py

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import os
2323
import re
2424
import sys
25+
import json
2526
from pathlib import Path
2627
import frontmatter
2728
import pandas as pd
2829
from mdutils import fileutils
2930
from dotenv import dotenv_values
3031
from glob import glob
32+
from typing import Optional
3133

3234
from toolbox import utils
3335

@@ -77,7 +79,7 @@ def get_info(data_model: pd.DataFrame, term: str, column: str = "Attribute") ->
7779
return results[0]
7880

7981

80-
def create_template_page(term: str, term_dict: dict) -> frontmatter.Post:
82+
def create_template_page(term: str, term_dict: dict, schema_names_dict: dict[str, list[str]]) -> frontmatter.Post:
8183
"""
8284
Creates a new markdown page for a specific template within the website's documentation.
8385
@@ -86,6 +88,7 @@ def create_template_page(term: str, term_dict: dict) -> frontmatter.Post:
8688
term_dict: A dictionary containing information about the template, including:
8789
'Description' (str): The description of the template.
8890
'Source' (str): The source of the template information. (URL or reference)
91+
schema_names_dict: A dictionary containing the schema names and display names {display_name: [schema_name]}.
8992
9093
Returns:
9194
A `frontmatter.Post` object representing the created template page metadata.
@@ -108,14 +111,16 @@ def create_template_page(term: str, term_dict: dict) -> frontmatter.Post:
108111
post.metadata["title"] = re.sub("_", r" ", term).strip()
109112
post.metadata["parent"] = term_dict["module"]
110113

114+
template_url = get_template_download_link(term=term, schema_names_dict=schema_names_dict)
115+
111116
# Inject term information into template content
112117
content_prefix = (
113118
"{% assign mydata=site.data."
114119
+ re.sub("\s|/", "_", term)
115120
+ " %} \n{: .note-title } \n"
116121
+ f">{post.metadata['title']}\n"
117122
+ ">\n"
118-
+ f">{term_dict['Description']} [[Source]]({term_dict['Source']})\n"
123+
+ f">{term_dict['Description']} [[Download]]({template_url})\n"
119124
)
120125
post.content = content_prefix + post.content
121126

@@ -127,6 +132,49 @@ def create_template_page(term: str, term_dict: dict) -> frontmatter.Post:
127132

128133
# return post
129134

135+
def get_manifest_schemas_name_dict(template_config_path: Optional[str] = "dca-template-config.json") -> dict[str, list[str]]:
136+
"""
137+
Loads the manifest schemas from a JSON configuration file into a pandas DataFrame.
138+
139+
Args:
140+
template_config_path: The path to the JSON configuration file (str).
141+
142+
Returns:
143+
A dictionary containing the schema names and display names {display_name: [schema_name]}.
144+
"""
145+
# The config contains a dictionary of the displaynames and schema names of the components that are in production use,
146+
# some from the schema are excluded
147+
with open(template_config_path, "r") as f:
148+
json_template_configdata = json.load(f)
149+
150+
# Process the dataframe to remove unneeded information and to be stored as a dictionary
151+
# to remove need for more complex df indexing when used later
152+
schema_names_frame = pd.DataFrame.from_dict(json_template_configdata["manifest_schemas"])
153+
schema_names_frame.drop('type',axis=1,inplace=True)
154+
schema_names_frame = schema_names_frame.set_index('display_name').T
155+
156+
157+
return schema_names_frame.to_dict(orient='list')
158+
159+
def get_template_download_link(term: str, schema_names_dict: dict[str, list[str]]) -> str:
160+
"""
161+
Constructs the download URL for a specific template based on its schema name.
162+
163+
Args:
164+
term: The name of the template (str).
165+
schema_names_dict: A dictionary containing the schema names and display names {display_name: [schema_name]}.
166+
167+
Returns:
168+
The download URL for the template (str).
169+
"""
170+
base_url = "https://github.com/eliteportal/data-models/raw/refs/heads/"
171+
templates_path = "main/elite-data/manifest-templates/"
172+
template_prefix = "EL_template_"
173+
174+
# Build the url to directly trigger a download of the template
175+
download_url = base_url + templates_path + template_prefix + schema_names_dict[term][0] + ".xlsx"
176+
177+
return download_url
130178

131179
def create_table_page(term: str, term_dict: dict) -> fileutils.MarkDownFile:
132180
"""
@@ -319,15 +367,21 @@ def delete_page(term: str) -> list[str]:
319367
for module in modules:
320368
create_module_page(module)
321369

370+
schema_names_dict = get_manifest_schemas_name_dict()
371+
322372
# Creating template pages
323373
print('---- Creating Template pages ----')
324374
templates = list(
325375
data_model[data_model["Parent"] == "Component"]["Attribute"].unique()
326376
)
377+
378+
# assay_phenotype_human_template is currently unused so remove from list to avoid attempting to generate template for it
379+
templates.remove('assay_phenotype_human_template')
380+
327381
for template in templates:
328382
# term_attr = re.sub("_", " ", template)
329383
term_info = get_info(data_model, template, column="Attribute")
330-
create_template_page(template, term_dict=term_info)
384+
create_template_page(template, term_dict=term_info,schema_names_dict=schema_names_dict)
331385

332386
# create attribute pages
333387
print("---- Creating attribute pages ----")

0 commit comments

Comments
 (0)