Skip to content

Commit

Permalink
get fallback image url for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkersigirci committed Dec 9, 2024
1 parent 2353efd commit 7bb3d71
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ run-streamlit: ## Run streamlit app
uv run streamlit run src/movie_guess/streamlit_app.py

run-fasthtml: ## Run fasthtml app
uv run streamlit run src/movie_guess/fasthtml_app.py
uv run python src/movie_guess/fasthtml_app.py
5 changes: 2 additions & 3 deletions docs/steps.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
I want to create a movie name
guess app with following steps
I want to create a movie name guess app with following steps

- Use Python streamlit package
- Use Python fasthtml package
- Get the movie backdrops from tmdb randomly
- Show each backdrop with card component
- Create a text input for the user to type in movie name
Expand Down
13 changes: 9 additions & 4 deletions src/movie_guess/fasthtml_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

app, rt = fast_app()

# Fallback image URL when no backdrop is found
FALLBACK_IMAGE_URL = "https://placehold.co/500x281/808080/FFFFFF/png?text=No+Image"


@rt("/")
def get():
Expand Down Expand Up @@ -62,11 +65,13 @@ def post(query: str = ""):
for movie in results:
backdrop_img = ""
if movie["backdrop_path"]:
# TMDB image base URL
img_url = f"https://image.tmdb.org/t/p/w500{movie['backdrop_path']}"
backdrop_img = Img(
src=img_url, cls="movie-backdrop", alt=f"{movie['title']} backdrop"
)
else:
img_url = FALLBACK_IMAGE_URL

backdrop_img = Img(
src=img_url, cls="movie-backdrop", alt=f"{movie['title']} backdrop"
)

movie_items.append(
Card(
Expand Down
18 changes: 18 additions & 0 deletions src/movie_guess/utils/movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,21 @@ def fuzzy_search_movies(
sorted_matches = sorted(fuzzy_matches, key=lambda x: x["similarity"], reverse=True)

return sorted_matches[:limit]


def get_random_movie_with_details() -> dict:
"""Get a random movie with all its details including backdrops.
Returns:
A dictionary containing movie details including title, backdrops, etc.
"""
movie = get_random_movie()
backdrops = get_movie_backdrops(movie.id)

return {
"id": movie.id,
"title": movie.title,
"backdrops": backdrops,
"overview": getattr(movie, "overview", "N/A"),
"release_date": getattr(movie, "release_date", "N/A"),
}

0 comments on commit 7bb3d71

Please sign in to comment.