mirror of
https://github.com/kennethreitz/kjvstudy.org.git
synced 2026-06-05 23:00:16 +00:00
Add HTML templates and FastAPI integration for Bible browsing functionality
This commit is contained in:
@@ -10,6 +10,17 @@ class Bible:
|
||||
with open(self.fname, "r") as f:
|
||||
self.verses = json.load(f)
|
||||
|
||||
def __getitem__(self, verse):
|
||||
"""
|
||||
Returns the text of the verse.
|
||||
"""
|
||||
|
||||
# Check if the verse exists in the dictionary.
|
||||
if verse not in self.verses:
|
||||
raise KeyError(f"Verse {verse} not found in the Bible.")
|
||||
|
||||
return self.verses[verse]
|
||||
|
||||
def iter_verses(self):
|
||||
"""
|
||||
Iterates over the verses in the Bible.
|
||||
@@ -38,6 +49,34 @@ class Bible:
|
||||
yielded.add(verse_ref.book)
|
||||
yield verse_ref.book
|
||||
|
||||
def iter_chapters(self):
|
||||
"""
|
||||
Iterates over the chapters in the Bible.
|
||||
"""
|
||||
|
||||
yielded = set()
|
||||
|
||||
for verse in self.verses:
|
||||
verse_ref = VerseReference.from_string(verse)
|
||||
if (verse_ref.book, verse_ref.chapter) in yielded:
|
||||
continue
|
||||
yielded.add((verse_ref.book, verse_ref.chapter))
|
||||
yield verse_ref.book, verse_ref.chapter
|
||||
|
||||
def iter_chapters_by_book(self):
|
||||
"""
|
||||
Iterates over the chapters in the Bible, grouped by book.
|
||||
"""
|
||||
|
||||
yielded = set()
|
||||
|
||||
for verse in self.verses:
|
||||
verse_ref = VerseReference.from_string(verse)
|
||||
if (verse_ref.book, verse_ref.chapter) in yielded:
|
||||
continue
|
||||
yielded.add((verse_ref.book, verse_ref.chapter))
|
||||
yield verse_ref.book, verse_ref.chapter
|
||||
|
||||
def iter_verse_references(self):
|
||||
"""
|
||||
Iterates over the verse references in the Bible.
|
||||
@@ -84,14 +123,15 @@ class VerseReference(BaseModel):
|
||||
return cls(book=book, chapter=int(chapter), verse=int(verse))
|
||||
|
||||
|
||||
print(VerseReference.from_string("Genesis 1:1"))
|
||||
print(VerseReference.from_string("I Corinthians 1:1"))
|
||||
print(VerseReference.from_string("John 3:16"))
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(VerseReference.from_string("Genesis 1:1"))
|
||||
print(VerseReference.from_string("I Corinthians 1:1"))
|
||||
print(VerseReference.from_string("John 3:16"))
|
||||
|
||||
bible = Bible()
|
||||
for verse in bible.iter_books():
|
||||
print(verse)
|
||||
# print()
|
||||
# print(verse)
|
||||
# break # Just print the first verse for demonstration
|
||||
bible = Bible()
|
||||
|
||||
print(bible["Genesis 1:1"])
|
||||
# print()
|
||||
# print(verse)
|
||||
# break # Just print the first verse for demonstration
|
||||
|
||||
@@ -7,6 +7,7 @@ requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"biblepy>=0.1.3",
|
||||
"fastapi>=0.115.12",
|
||||
"jinja2>=3.1.6",
|
||||
"parse>=1.20.2",
|
||||
"pytest>=8.3.5",
|
||||
"uvicorn>=0.34.2",
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
import kjv # assuming your kjv module is available and contains the Bible class
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Set up Jinja2 templates and static files
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
bible = kjv.Bible()
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
def read_root(request: Request):
|
||||
books = list(bible.iter_books())
|
||||
return templates.TemplateResponse(
|
||||
"index.html", {"request": request, "books": books}
|
||||
)
|
||||
|
||||
|
||||
@app.get("/book/{book}", response_class=HTMLResponse)
|
||||
def read_book(request: Request, book: str):
|
||||
chapters = [ch for bk, ch in bible.iter_chapters() if bk == book]
|
||||
if not chapters:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
return templates.TemplateResponse(
|
||||
"book.html", {"request": request, "book": book, "chapters": chapters}
|
||||
)
|
||||
|
||||
|
||||
@app.get("/book/{book}/chapter/{chapter}", response_class=HTMLResponse)
|
||||
def read_chapter(request: Request, book: str, chapter: int):
|
||||
verses = [v for v in bible.iter_verses() if v.book == book and v.chapter == chapter]
|
||||
if not verses:
|
||||
raise HTTPException(status_code=404, detail="Chapter not found")
|
||||
return templates.TemplateResponse(
|
||||
"chapter.html",
|
||||
{"request": request, "book": book, "chapter": chapter, "verses": verses},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ book }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ book }}</h1>
|
||||
<ul>
|
||||
{% for chapter in chapters %}
|
||||
<li><a href="/book/{{ book }}/chapter/{{ chapter }}">{{ book }} {{ chapter }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<a href="/">Back to Books</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ book }} {{ chapter }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ book }} {{ chapter }}</h1>
|
||||
{% for verse in verses %}
|
||||
<div>
|
||||
<p><strong>{{ verse.verse }}</strong> {{ verse.text }}</p>
|
||||
<textarea rows="2" cols="80" placeholder="Your commentary..."></textarea>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<a href="/book/{{ book }}">Back to {{ book }}</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bible Browser</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Select a Book</h1>
|
||||
<ul>
|
||||
{% for book in books %}
|
||||
<li><a href="/book/{{ book }}">{{ book }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@@ -91,6 +91,18 @@ wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jinja2"
|
||||
version = "3.1.6"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "markupsafe" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/df/bf/f7da0350254c0ed7c72f3e33cef02e048281fec7ecec5f032d4aac52226b/jinja2-3.1.6.tar.gz", hash = "sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d", size = 245115 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/62/a1/3d680cbfd5f4b8f15abc1d571870c5fc3e594bb582bc3b64ea099db13e56/jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67", size = 134899 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kjvstudy-org"
|
||||
version = "0.1.0"
|
||||
@@ -98,6 +110,7 @@ source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "biblepy" },
|
||||
{ name = "fastapi" },
|
||||
{ name = "jinja2" },
|
||||
{ name = "parse" },
|
||||
{ name = "pytest" },
|
||||
{ name = "uvicorn" },
|
||||
@@ -107,11 +120,40 @@ dependencies = [
|
||||
requires-dist = [
|
||||
{ name = "biblepy", specifier = ">=0.1.3" },
|
||||
{ name = "fastapi", specifier = ">=0.115.12" },
|
||||
{ name = "jinja2", specifier = ">=3.1.6" },
|
||||
{ name = "parse", specifier = ">=1.20.2" },
|
||||
{ name = "pytest", specifier = ">=8.3.5" },
|
||||
{ name = "uvicorn", specifier = ">=0.34.2" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "markupsafe"
|
||||
version = "3.0.2"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/b2/97/5d42485e71dfc078108a86d6de8fa46db44a1a9295e89c5d6d4a06e23a62/markupsafe-3.0.2.tar.gz", hash = "sha256:ee55d3edf80167e48ea11a923c7386f4669df67d7994554387f84e7d8b0a2bf0", size = 20537 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/83/0e/67eb10a7ecc77a0c2bbe2b0235765b98d164d81600746914bebada795e97/MarkupSafe-3.0.2-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:ba9527cdd4c926ed0760bc301f6728ef34d841f405abf9d4f959c478421e4efd", size = 14274 },
|
||||
{ url = "https://files.pythonhosted.org/packages/2b/6d/9409f3684d3335375d04e5f05744dfe7e9f120062c9857df4ab490a1031a/MarkupSafe-3.0.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f8b3d067f2e40fe93e1ccdd6b2e1d16c43140e76f02fb1319a05cf2b79d99430", size = 12352 },
|
||||
{ url = "https://files.pythonhosted.org/packages/d2/f5/6eadfcd3885ea85fe2a7c128315cc1bb7241e1987443d78c8fe712d03091/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:569511d3b58c8791ab4c2e1285575265991e6d8f8700c7be0e88f86cb0672094", size = 24122 },
|
||||
{ url = "https://files.pythonhosted.org/packages/0c/91/96cf928db8236f1bfab6ce15ad070dfdd02ed88261c2afafd4b43575e9e9/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:15ab75ef81add55874e7ab7055e9c397312385bd9ced94920f2802310c930396", size = 23085 },
|
||||
{ url = "https://files.pythonhosted.org/packages/c2/cf/c9d56af24d56ea04daae7ac0940232d31d5a8354f2b457c6d856b2057d69/MarkupSafe-3.0.2-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f3818cb119498c0678015754eba762e0d61e5b52d34c8b13d770f0719f7b1d79", size = 22978 },
|
||||
{ url = "https://files.pythonhosted.org/packages/2a/9f/8619835cd6a711d6272d62abb78c033bda638fdc54c4e7f4272cf1c0962b/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:cdb82a876c47801bb54a690c5ae105a46b392ac6099881cdfb9f6e95e4014c6a", size = 24208 },
|
||||
{ url = "https://files.pythonhosted.org/packages/f9/bf/176950a1792b2cd2102b8ffeb5133e1ed984547b75db47c25a67d3359f77/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:cabc348d87e913db6ab4aa100f01b08f481097838bdddf7c7a84b7575b7309ca", size = 23357 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ce/4f/9a02c1d335caabe5c4efb90e1b6e8ee944aa245c1aaaab8e8a618987d816/MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:444dcda765c8a838eaae23112db52f1efaf750daddb2d9ca300bcae1039adc5c", size = 23344 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ee/55/c271b57db36f748f0e04a759ace9f8f759ccf22b4960c270c78a394f58be/MarkupSafe-3.0.2-cp313-cp313-win32.whl", hash = "sha256:bcf3e58998965654fdaff38e58584d8937aa3096ab5354d493c77d1fdd66d7a1", size = 15101 },
|
||||
{ url = "https://files.pythonhosted.org/packages/29/88/07df22d2dd4df40aba9f3e402e6dc1b8ee86297dddbad4872bd5e7b0094f/MarkupSafe-3.0.2-cp313-cp313-win_amd64.whl", hash = "sha256:e6a2a455bd412959b57a172ce6328d2dd1f01cb2135efda2e4576e8a23fa3b0f", size = 15603 },
|
||||
{ url = "https://files.pythonhosted.org/packages/62/6a/8b89d24db2d32d433dffcd6a8779159da109842434f1dd2f6e71f32f738c/MarkupSafe-3.0.2-cp313-cp313t-macosx_10_13_universal2.whl", hash = "sha256:b5a6b3ada725cea8a5e634536b1b01c30bcdcd7f9c6fff4151548d5bf6b3a36c", size = 14510 },
|
||||
{ url = "https://files.pythonhosted.org/packages/7a/06/a10f955f70a2e5a9bf78d11a161029d278eeacbd35ef806c3fd17b13060d/MarkupSafe-3.0.2-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:a904af0a6162c73e3edcb969eeeb53a63ceeb5d8cf642fade7d39e7963a22ddb", size = 12486 },
|
||||
{ url = "https://files.pythonhosted.org/packages/34/cf/65d4a571869a1a9078198ca28f39fba5fbb910f952f9dbc5220afff9f5e6/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4aa4e5faecf353ed117801a068ebab7b7e09ffb6e1d5e412dc852e0da018126c", size = 25480 },
|
||||
{ url = "https://files.pythonhosted.org/packages/0c/e3/90e9651924c430b885468b56b3d597cabf6d72be4b24a0acd1fa0e12af67/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c0ef13eaeee5b615fb07c9a7dadb38eac06a0608b41570d8ade51c56539e509d", size = 23914 },
|
||||
{ url = "https://files.pythonhosted.org/packages/66/8c/6c7cf61f95d63bb866db39085150df1f2a5bd3335298f14a66b48e92659c/MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d16a81a06776313e817c951135cf7340a3e91e8c1ff2fac444cfd75fffa04afe", size = 23796 },
|
||||
{ url = "https://files.pythonhosted.org/packages/bb/35/cbe9238ec3f47ac9a7c8b3df7a808e7cb50fe149dc7039f5f454b3fba218/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:6381026f158fdb7c72a168278597a5e3a5222e83ea18f543112b2662a9b699c5", size = 25473 },
|
||||
{ url = "https://files.pythonhosted.org/packages/e6/32/7621a4382488aa283cc05e8984a9c219abad3bca087be9ec77e89939ded9/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:3d79d162e7be8f996986c064d1c7c817f6df3a77fe3d6859f6f9e7be4b8c213a", size = 24114 },
|
||||
{ url = "https://files.pythonhosted.org/packages/0d/80/0985960e4b89922cb5a0bac0ed39c5b96cbc1a536a99f30e8c220a996ed9/MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:131a3c7689c85f5ad20f9f6fb1b866f402c445b220c19fe4308c0b147ccd2ad9", size = 24098 },
|
||||
{ url = "https://files.pythonhosted.org/packages/82/78/fedb03c7d5380df2427038ec8d973587e90561b2d90cd472ce9254cf348b/MarkupSafe-3.0.2-cp313-cp313t-win32.whl", hash = "sha256:ba8062ed2cf21c07a9e295d5b8a2a5ce678b913b45fdf68c32d95d6c1291e0b6", size = 15208 },
|
||||
{ url = "https://files.pythonhosted.org/packages/4f/65/6079a46068dfceaeabb5dcad6d674f5f5c61a6fa5673746f42a9f4c233b3/MarkupSafe-3.0.2-cp313-cp313t-win_amd64.whl", hash = "sha256:e444a31f8db13eb18ada366ab3cf45fd4b31e4db1236a4448f68778c1d1a5a2f", size = 15739 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "packaging"
|
||||
version = "25.0"
|
||||
|
||||
Reference in New Issue
Block a user