Spaces:
Runtime error
Runtime error
Remove obsolete file
Browse filesThis script has been replaced by `json_to_md.py`, which converts JSON output from `reformat_json.py` directly to the Markdown format specified by Data Cards Labs.
- formatting/format_as_md.py +0 -78
formatting/format_as_md.py
DELETED
|
@@ -1,78 +0,0 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
import json
|
| 3 |
-
import pathlib
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
parser = argparse.ArgumentParser(
|
| 7 |
-
description="Format the output of the data card tool as .md for the hub."
|
| 8 |
-
)
|
| 9 |
-
parser.add_argument("--input_path", "-i", type=pathlib.Path, required=True)
|
| 10 |
-
parser.add_argument("--output_path", "-o", type=pathlib.Path, required=True)
|
| 11 |
-
args = parser.parse_args()
|
| 12 |
-
|
| 13 |
-
def read_json_file(json_path: pathlib.Path):
|
| 14 |
-
"""Load a json file and return it as object."""
|
| 15 |
-
with open(json_path, "r") as f:
|
| 16 |
-
data = json.load(f)
|
| 17 |
-
return data
|
| 18 |
-
|
| 19 |
-
def save_md_file(md_path: pathlib.Path, md_string: str):
|
| 20 |
-
"""Takes a string and saves it as .md file."""
|
| 21 |
-
with open(md_path, 'w') as f:
|
| 22 |
-
f.write(md_string)
|
| 23 |
-
|
| 24 |
-
def construct_md(data_card_data: dict, text_by_key: dict):
|
| 25 |
-
"""Constructs the markdown file
|
| 26 |
-
|
| 27 |
-
This function iterates through text_by_key and extracts all answers from
|
| 28 |
-
the data_card_data object. It uses the levels of hierarchy as indicator for
|
| 29 |
-
the heading indentation and does not change the order in which anything
|
| 30 |
-
appears.
|
| 31 |
-
|
| 32 |
-
Args:
|
| 33 |
-
data_card_data: Output from the data card tool
|
| 34 |
-
text_by_key: configuration defined in key_to_question.json
|
| 35 |
-
|
| 36 |
-
Returns:
|
| 37 |
-
data_card_md_string: Markdown-formatted content
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
data_card_md_string = ""
|
| 41 |
-
|
| 42 |
-
for main_key, main_content in text_by_key.items():
|
| 43 |
-
section_header = main_content['section-title']
|
| 44 |
-
# Remove it so that we don't iterate over it.
|
| 45 |
-
del main_content['section-title']
|
| 46 |
-
# Add it to string.
|
| 47 |
-
data_card_md_string += f"## {section_header} \n\n"
|
| 48 |
-
# Iterate over the subsections.
|
| 49 |
-
for second_key, second_content in main_content.items():
|
| 50 |
-
subsection_header = second_content['section-title']
|
| 51 |
-
# Remove it so that we don't iterate over it.
|
| 52 |
-
del second_content['section-title']
|
| 53 |
-
# Add it to string.
|
| 54 |
-
data_card_md_string += f"### {subsection_header} \n\n"
|
| 55 |
-
# Finally, iterate over actual questions.
|
| 56 |
-
for question_key, question_text in second_content.items():
|
| 57 |
-
# Now grab the answer text.
|
| 58 |
-
reply = data_card_data[main_key][second_key].get(question_key, "N/A")
|
| 59 |
-
if reply and reply != "N/A":
|
| 60 |
-
data_card_md_string += f"#### {question_text} \n\n"
|
| 61 |
-
# Special parsing options here.
|
| 62 |
-
if question_key == question_key == "paper-bibtext":
|
| 63 |
-
data_card_md_string += f"```\n{reply}\n``` \n\n"
|
| 64 |
-
elif question_key == "structure-example":
|
| 65 |
-
data_card_md_string += f"```json\n{reply}\n``` \n\n"
|
| 66 |
-
elif isinstance(reply, list):
|
| 67 |
-
data_card_md_string += f"{' '.join(reply)} \n\n"
|
| 68 |
-
else:
|
| 69 |
-
data_card_md_string += f"{reply} \n\n"
|
| 70 |
-
|
| 71 |
-
return data_card_md_string
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if __name__ == "__main__":
|
| 75 |
-
data_card_data = read_json_file(args.input_path)
|
| 76 |
-
text_by_key = read_json_file(os.path.join(os.path.dirname(__file__), "key_to_question.json"))
|
| 77 |
-
data_card_md_string = construct_md(data_card_data, text_by_key)
|
| 78 |
-
save_md_file(args.output_path, data_card_md_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|