Skip to content

Add script to generate content_catalog.json (similar to llms-full.txt) #1299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions python/apply_image_effects.py

This file was deleted.

Binary file removed python/blurmask.png
Binary file not shown.
84 changes: 84 additions & 0 deletions python/generate_content_catalog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# /// script
# dependencies = [
# "python-frontmatter>=1.0.0",
# ]
# ///
#
# This file is used to generate content_catalog.json, which contains
# the markdown contents for every page of Streamlit's documentation in
# the format [{url: "the_url", content: "the_content"}, ...]
#
# content_catalog.json is used in the RAG pipeline for st-assistant.streamlit.app.
#
# In the future, we may also want to use this file to generate llms.txt
# and llms-full.txt, since they serve a very similar purpose to content_catalog.json.
#
# Usage:
# uv run generate_content_catalog.py

import json
from typing import List, Dict, Optional
import frontmatter
from pathlib import Path


def process_markdown_files(content_dir: Path) -> List[Dict[str, Optional[str]]]:
"""Process all markdown files in the content directory and its subdirectories.

Args:
content_dir: Path to the content directory containing markdown files.

Returns:
List of dictionaries containing 'url' (from frontmatter slug) and 'content' for each markdown file.
"""
content_catalog: List[Dict[str, Optional[str]]] = []

# Walk through all directories and files
for file_path in content_dir.rglob("*.md"):
try:
# Read the content of the markdown file with frontmatter
post = frontmatter.load(file_path)

# Get the URL from frontmatter slug if it exists, otherwise set to null
url = post.get("slug")

if not url:
continue

url = f"https://docs.streamlit.io{url}"

# Add to catalog
content_catalog.append({"url": url, "content": post.content})
except frontmatter.FrontmatterError as e:
print(f"Error parsing frontmatter in {file_path}: {str(e)}")
except Exception as e:
print(f"Error processing {file_path}: {str(e)}")

return content_catalog


def main() -> None:
"""Generate a content catalog JSON file from markdown files in the content directory."""
# Get the content directory path (sibling to the python directory)
content_dir = Path(__file__).parent.parent / "content"

# Process all markdown files
content_catalog = process_markdown_files(content_dir)

# Write the catalog to a JSON file in the python directory
output_file = Path(__file__).parent / "content_catalog.json"
try:
output_file.write_text(
json.dumps(content_catalog, ensure_ascii=False, indent=2), encoding="utf-8"
)
print(
f"Successfully generated {output_file} with {len(content_catalog)} entries"
)
except json.JSONEncodeError as e:
print(f"Error encoding JSON for {output_file}: {str(e)}")
except Exception as e:
print(f"Error writing {output_file}: {str(e)}")


if __name__ == "__main__":
main()