File size: 13,702 Bytes
bee9bbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | """Source-grounded rendering for Coursera's selected search experience."""
from __future__ import annotations
from html import escape
from pathlib import Path
from typing import Any, Iterable
from urllib.parse import quote
from jinja2 import Environment, FileSystemLoader, select_autoescape
_TEMPLATES = Environment(
loader=FileSystemLoader(Path(__file__).resolve().parent / "templates"),
autoescape=select_autoescape(("html", "xml")),
)
SEARCH_RESULTS: tuple[dict[str, Any], ...] = (
{"title": "Google AI", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-1.png", "href": "/professional-certificates/google-ai", "image": "/static/search/frozen-1.png", "badges": ("Beginner",), "skills": "Generative AI, Artificial Intelligence, Prompt Engineering, Machine Learning, Large Language Models", "rating": "4.8", "reviews": "5K reviews", "meta": "Beginner · Professional Certificate · 2 - 6 Months", "credential": True},
{"title": "Google Data Analytics", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-2.png", "href": "/professional-certificates/google-data-analytics", "image": "/static/search/frozen-2.png", "badges": ("Beginner",), "skills": "Data Analysis, SQL, Spreadsheet, Data Visualization, Data Cleansing", "rating": "4.8", "reviews": "147K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Google Project Management", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-3.png", "href": "/professional-certificates/google-project-management", "image": "/static/search/frozen-3.png", "badges": ("Beginner",), "skills": "Project Management, Agile, Risk Management, Strategic Thinking, Planning", "rating": "4.8", "reviews": "88K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Google Cybersecurity", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-4.png", "href": "/professional-certificates/google-cybersecurity", "image": "/static/search/frozen-4.png", "badges": ("Beginner",), "skills": "Cybersecurity, Security Hardening, Risk Assessment, Incident Response", "rating": "4.8", "reviews": "27K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Google AI Essentials", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-5.png", "href": "/professional-certificates/google-ai-essentials", "image": "/static/search/frozen-5.png", "badges": ("Beginner",), "skills": "Generative AI, Artificial Intelligence, Machine Learning, Prompt Design", "rating": "4.8", "reviews": "18K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Machine Learning", "provider": "Stanford University", "provider_mark": "S", "provider_icon": "/static/search/frozen-6.png", "href": "/learn/machine-learning", "image": "/static/search/frozen-6.png", "badges": ("Free Trial",), "skills": "Machine Learning, Supervised Learning, Regression, Neural Networks", "rating": "4.9", "reviews": "24K reviews", "meta": "Beginner · Course · 1 - 3 Months", "credential": False},
{"title": "Google Digital Marketing & E-commerce", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-7.png", "href": "/professional-certificates/google-digital-marketing-ecommerce", "image": "/static/search/frozen-7.png", "badges": ("Beginner",), "skills": "Digital Marketing, E-Commerce, SEO, Marketing Analytics", "rating": "4.8", "reviews": "45K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Google IT Support", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-8.png", "href": "/professional-certificates/google-it-support", "image": "/static/search/frozen-8.png", "badges": ("Beginner",), "skills": "Technical Support, Networking, Operating Systems, Security", "rating": "4.8", "reviews": "190K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "IBM Generative AI Engineering", "provider": "IBM", "provider_mark": "I", "provider_icon": "/static/search/frozen-9.png", "href": "/professional-certificates/ai-engineer", "image": "/static/search/frozen-9.png", "badges": ("Intermediate",), "skills": "Generative AI, Machine Learning, Python, Prompt Engineering, Model Deployment", "rating": "4.7", "reviews": "3K reviews", "meta": "Intermediate · Professional Certificate · 3 - 6 Months", "credential": True},
{"title": "IBM Data Analyst", "provider": "IBM", "provider_mark": "I", "provider_icon": "/static/search/frozen-10.png", "href": "/professional-certificates/ibm-data-analyst", "image": "/static/search/frozen-10.png", "badges": ("Beginner",), "skills": "Python, SQL, Data Analysis, Data Visualization, Excel", "rating": "4.7", "reviews": "21K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "Google UX Design", "provider": "Google", "provider_mark": "G", "provider_icon": "/static/search/frozen-11.png", "href": "/professional-certificates/google-ux-design", "image": "/static/search/frozen-11.png", "badges": ("Beginner",), "skills": "UX Research, Design Thinking, Prototyping, Wireframing", "rating": "4.8", "reviews": "82K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
{"title": "IBM Data Science", "provider": "IBM", "provider_mark": "I", "provider_icon": "/static/search/frozen-12.png", "href": "/professional-certificates/ibm-data-science", "image": "/static/search/frozen-12.png", "badges": ("Beginner",), "skills": "Data Science, Python, Machine Learning, Statistics, SQL", "rating": "4.7", "reviews": "61K reviews", "meta": "Beginner · Professional Certificate · 1 - 3 Months", "credential": True},
)
_AI_STARTER_BEST_FOR: tuple[str, ...] = (
"learners with 3-6 months availability, intermediate skill level, and those "
"seeking specialization credentials eager to master deep learning",
"learners with 1-4 weeks availability, intermediate experience, and those "
"preferring focused courses ready to learn neural networks basics",
"learners with 3-6 months availability, intermediate skill level, and those "
"pursuing professional certificates looking to apply deep learning with "
"popular libraries",
"learners with 1-3 months availability, intermediate experience, and those "
"seeking professional certificates eager to specialize in PyTorch for deep "
"learning",
)
_CARD_EVIDENCE_NOTES = {
"structural-only": "Evidence: public structure observed; details simulated.",
"inferred-architecture": "Evidence: architecture inferred; details simulated.",
"truthful-simulation": "Evidence: offline simulation; not source-verified.",
}
def _generic_result(record: dict[str, Any]) -> dict[str, Any]:
record_id = str(record["id"])
kind = str(record.get("type", "course"))
classification = str(record["source_evidence_classification"])
href = (
f"/specializations/{record_id}"
if kind == "specialization"
else f"/professional-certificates/{record_id}"
if kind == "professional-certificate"
else f"/learn/{record_id}"
)
return {
"title": str(record["title"]),
"provider": str(record["provider"]),
"provider_mark": str(record["provider"])[0].upper(),
"href": href,
"image": "/static/search/deep-learning.png",
"badges": ("Free Trial",),
"skills": str(record.get("topic", "")),
"rating": f"{float(record['rating']):g}",
"reviews": str(record.get("reviews_summary", "")),
"meta": f"{record['level']} · {kind.replace('-', ' ').title()} · {record['duration']}",
"credential": False,
"catalog_id": record_id,
"evidence_classification": classification,
"evidence_note": _CARD_EVIDENCE_NOTES.get(classification, ""),
}
def render_search_body(
*,
query: str,
filtered_records: list[dict[str, Any]],
filters: dict[str, str],
source_selected: bool,
filter_options: dict[str, list[str]],
) -> str:
"""Render search without remote content or unsupported AI output."""
def _apply_filters(records: list[dict[str, Any]]) -> list[dict[str, Any]]:
out = records
if filters.get("level"):
wanted = filters["level"].casefold()
out = [
r
for r in out
if wanted
in " ".join([str(r.get("level", "")), str(r.get("meta", ""))]).casefold()
]
if filters.get("rating"):
try:
minimum = float(filters["rating"])
out = [
r
for r in out
if float(str(r.get("rating", "0")).replace("★", "").strip() or 0)
>= minimum
]
except ValueError:
pass
if filters.get("product") == "courses":
out = [r for r in out if "course" in str(r.get("meta", "")).casefold()]
elif filters.get("product") == "specializations":
out = [
r
for r in out
if "specialization" in str(r.get("meta", "")).casefold()
]
elif filters.get("product") == "professional-certificates":
out = [
r
for r in out
if "professional certificate" in str(r.get("meta", "")).casefold()
]
elif filters.get("product") == "degrees":
out = [r for r in out if "degree" in str(r.get("meta", "")).casefold()]
if filters.get("status") == "free-trial":
out = [
r
for r in out
if "free trial" in str(r.get("badges", "")).casefold()
or "free" in str(r.get("meta", "")).casefold()
]
return out
results = (
[
dict(record, source_result=True)
for record in _apply_filters(list(SEARCH_RESULTS))
]
if source_selected
else [
dict(_generic_result(record), source_result=False)
for record in _apply_filters(list(filtered_records))
]
)
template = _TEMPLATES.get_template("pages/search.html")
ai_starter_cards = [
dict(SEARCH_RESULTS[position], best_for=best_for)
for position, best_for in enumerate(_AI_STARTER_BEST_FOR)
]
return template.render(
query=query,
results=results,
ai_starter_cards=ai_starter_cards,
result_count=len(results),
filters=filters,
filters_active=any(
value
for key, value in filters.items()
if key != "sort"
)
or filters.get("sort") not in {"", "best-match", "title-asc"},
clear_filters_href=f"/search?query={quote(query)}",
filter_options=filter_options,
no_results=not results,
recommended=(
[
dict(_generic_result(record), source_result=False)
for record in _recommended_records()
]
if not results
else []
),
)
def _recommended_records() -> list[dict[str, Any]]:
"""Return top-rated catalog records for the no-results recommendation row."""
from catalog import load_catalog_seed
records = [
record
for record in load_catalog_seed()
if record["type"] in {"course", "specialization"}
and record["id"] != "deep-learning-specialization"
]
records.sort(key=lambda record: -float(record.get("rating", 0)))
return records[:6]
def render_public_landing_body(
*, title: str, description: str, cards: Iterable[object]
) -> str:
"""Render a source-path landing from existing source-backed card records."""
rendered_cards: list[str] = []
seen_hrefs: set[str] = set()
for position, card in enumerate(cards):
href = str(getattr(card, "href"))
if not href.startswith("/") or href.startswith("//") or href in seen_hrefs:
continue
seen_hrefs.add(href)
card_title = str(getattr(card, "title"))
provider = str(getattr(card, "provider", ""))
metadata = str(getattr(card, "metadata", ""))
image = str(getattr(card, "image"))
rendered_cards.append(
f"""<article data-public-landing-record="{position}"><a class="source-learning-card" href="{escape(href, quote=True)}"><img class="source-card-image" src="{escape(image, quote=True)}" alt=""><span><small>{escape(provider)}</small><strong>{escape(card_title)}</strong><em>{escape(metadata)}</em></span></a></article>"""
)
if not rendered_cards:
raise ValueError("public landing requires at least one source-backed record")
return f"""
<section class="source-category-shell public-source-landing">
<nav class="source-category-breadcrumb" aria-label="Breadcrumb"><a href="/browse">Browse</a><span aria-hidden="true">›</span><span>{escape(title)}</span></nav>
<header class="source-category-hero"><div><h1>{escape(title)}</h1><p>{escape(description)}</p></div></header>
<section aria-labelledby="public-landing-results"><h2 id="public-landing-results">Explore local learning records</h2><div class="source-mini-grid">{''.join(rendered_cards)}</div></section>
<p><a href="/browse">Back to Browse</a></p>
</section>"""
|