Skip to content

Commit

Permalink
moved fasthtml codes under api folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkersigirci committed Dec 14, 2024
1 parent 40ae21f commit 1980f72
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ export-requirements: ## Export requirements to requirements.txt
@uv export --frozen --no-hashes --only-group dev --format requirements-txt --output-file requirements.txt

run-fasthtml: ## Run fasthtml app
uv run python src/movie_guess/fasthtml_app.py
uv run python src/movie_guess/api/fasthtml_app.py
58 changes: 1 addition & 57 deletions notebooks/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"from pprint import pprint\n",
"\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Image, display\n",
"\n",
"load_dotenv()"
]
Expand Down Expand Up @@ -64,63 +63,8 @@
"metadata": {},
"outputs": [],
"source": [
"pprint(backdrops)"
"pprint(backdrops) # noqa: T203"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from thefuzz import fuzz\n",
"\n",
"\n",
"def fuzzy_search_movies(query, threshold=60):\n",
" \"\"\"\n",
" Search movies with fuzzy matching\n",
" Args:\n",
" query (str): Search term\n",
" threshold (int): Minimum similarity score (0-100)\n",
" Returns:\n",
" list: Matched movies\n",
" \"\"\"\n",
" # Get initial results from TMDB\n",
" results = search.movies(query)\n",
"\n",
" # Apply fuzzy matching\n",
" fuzzy_matches = []\n",
" for result in results:\n",
" title = result.title\n",
" # Calculate similarity ratio\n",
" ratio = fuzz.ratio(query.lower(), title.lower())\n",
"\n",
" if ratio >= threshold:\n",
" fuzzy_matches.append(\n",
" {\n",
" \"title\": title,\n",
" \"similarity\": ratio,\n",
" \"id\": result.id,\n",
" \"release_date\": getattr(result, \"release_date\", \"N/A\"),\n",
" \"overview\": getattr(result, \"overview\", \"N/A\"),\n",
" }\n",
" )\n",
"\n",
" # Sort by similarity score\n",
" return sorted(fuzzy_matches, key=lambda x: x[\"similarity\"], reverse=True)\n",
"\n",
"\n",
"query = \"interstellar\"\n",
"\n",
"fuzzy_search_movies(query=query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
16 changes: 9 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ classifiers = [

dependencies = [
"python-fasthtml>=0.10.1",
"ruff==0.8.0",
"thefuzz[speedup]>=0.22.1",
"tmdbv3api>=1.9.0",
]
Expand Down Expand Up @@ -66,7 +67,7 @@ log_cli_format = "%(asctime)-20s :: %(name)-8s :: %(levelname)-8s :: %(filename)
log_cli_date_format = "%Y-%m-%d %H:%M:%S"

[tool.ruff]
target-version = "py311"
# target-version = "py312"
line-length = 88
src = ["src"]
respect-gitignore = true
Expand All @@ -78,11 +79,6 @@ extend-exclude = [
"tests",
]

# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "F401"]
# "*.ipynb" = ["E501"] # disable line-too-long in notebooks

# "path/to/file.py" = ["E402"]
# 'movie_guess/__init__.py' = ['F405', 'F403']

Expand All @@ -97,7 +93,6 @@ skip-magic-trailing-comma = false
# preview = true

[tool.ruff.lint]

select = [
"B", # flake8-bugbear
"C", # flake8-comprehensions
Expand All @@ -123,6 +118,7 @@ select = [
ignore = [
"E501", # Line too long, handled by ruff formatter
"D107", # "Missing docstring in __init__",
"F811", # "redefinition of the same function"
]

unfixable = [
Expand All @@ -131,6 +127,12 @@ unfixable = [

#extend-select = []

# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "F401"]
# "*.ipynb" = ["E501"] # disable line-too-long in notebooks
"src/movie_guess/api/*" = ["D"]

[tool.ruff.lint.flake8-import-conventions.extend-aliases]
some_alias = ""

Expand Down
Empty file added src/movie_guess/api/__init__.py
Empty file.
File renamed without changes.
70 changes: 34 additions & 36 deletions src/movie_guess/game_app.py → src/movie_guess/api/game_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,42 +162,40 @@ def post(query: str = ""):
Button("Play Again", hx_post="/new-game", hx_target="body"),
)
)
else:
# Show next backdrop if wrong guess
if (
len(current_game["movie"]["backdrops"])
> current_game["current_backdrop_index"] + 1
):
# Increment the index and get the next backdrop
current_game["current_backdrop_index"] += 1
next_backdrop = current_game["movie"]["backdrops"][
current_game["current_backdrop_index"]
]
# Replace the current backdrop with the new one
current_game["shown_backdrops"] = [next_backdrop]
backdrop_url = f"https://image.tmdb.org/t/p/w1280{next_backdrop}"
return (
Div(
P(f"Wrong guess: {movie['title']}", cls="wrong-guess"),
id="search-results",
),
# Add id to match the container we want to replace
Div(
Img(src=backdrop_url, cls="backdrop-img"),
cls="backdrop-container",
id="backdrop-container",
hx_swap_oob="true",
),
)
else:
return Card(
Div(
H2("Game Over!", cls="wrong-guess"),
P(f"The correct movie was: {current_movie['title']}"),
P(current_movie["overview"]),
Button("Play Again", hx_post="/new-game", hx_target="body"),
)
)
# Show next backdrop if wrong guess
if (
len(current_game["movie"]["backdrops"])
> current_game["current_backdrop_index"] + 1
):
# Increment the index and get the next backdrop
current_game["current_backdrop_index"] += 1
next_backdrop = current_game["movie"]["backdrops"][
current_game["current_backdrop_index"]
]
# Replace the current backdrop with the new one
current_game["shown_backdrops"] = [next_backdrop]
backdrop_url = f"https://image.tmdb.org/t/p/w1280{next_backdrop}"
return (
Div(
P(f"Wrong guess: {movie['title']}", cls="wrong-guess"),
id="search-results",
),
# Add id to match the container we want to replace
Div(
Img(src=backdrop_url, cls="backdrop-img"),
cls="backdrop-container",
id="backdrop-container",
hx_swap_oob="true",
),
)
return Card(
Div(
H2("Game Over!", cls="wrong-guess"),
P(f"The correct movie was: {current_movie['title']}"),
P(current_movie["overview"]),
Button("Play Again", hx_post="/new-game", hx_target="body"),
)
)


@rt("/new-game")
Expand Down
44 changes: 23 additions & 21 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"rewrites": [
{
"source": "/(.*)",
"destination": "/src/movie_guess/game_app"
"destination": "/src/movie_guess/api/game_app"
}
]
}
}

0 comments on commit 1980f72

Please sign in to comment.