import json
import requests
from datetime import datetime
def json_to_html(url: str, output_file: str = "chembl_purchasable.html"):
"""Fetch JSON and render it as nice HTML."""
print("Fetching JSON from:", url)
response = requests.get(url)
response.raise_for_status()
data = response.json()
html = f"""
Purchasable Bioactives - CHEMBL178
Purchasable Bioactives for CHEMBL178
Generated on: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
"""
# Sort vendors alphabetically for nicer display
for vendor in sorted(data.keys()):
compounds = data[vendor]
html += f"""
"""
for compound in compounds:
html += f"""
Vendor ID: {compound.get('vendor_id', 'N/A')}
Index: {compound.get('index', 'N/A')}
SMILES:
{compound.get('smiles', 'No SMILES available')}
| Base | {compound.get('base', '')} |
| Anonymous Graph | {compound.get('anonymous_graph', '')} |
| Similarity Code | {compound.get('similarity_code', '')} |
| Source File | {compound.get('source_file', '')} |
"""
html += "
\n"
html += """
"""
with open(output_file, "w", encoding="utf-8") as f:
f.write(html)
print(f"HTML successfully written to: {output_file}")
print("Open the file in your browser to view the rendered data.")
# ========================
# Usage
# ========================
if __name__ == "__main__":
url = "https://fe.docking.org/PurchasableBioactives/inVitro/chembl/A/ABCC1/iso/csv/KT_test/CHEMBL178/0.json"
json_to_html(url)