Skip to content

Commit

Permalink
Merge pull request #3 from gokberksahin/adds-jinja-support
Browse files Browse the repository at this point in the history
updates requirements.txt with Jina library
  • Loading branch information
gokberksahin authored Jan 21, 2024
2 parents a1ea31a + 1d1868e commit 026556a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
20 changes: 8 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from session_cache_handler import SessionCacheHandler
from fastapi.templating import Jinja2Templates

# Jinja2 template engine
templates = Jinja2Templates(directory="templates")


load_dotenv('.env')
Expand All @@ -34,20 +38,12 @@

@app.get("/")
def index(request: Request):
# Get spotify token info from session
spotify_token_info = request.session.get("spotify_token_info")
# Get the SpotifyOAuth object
auth_manager = _get_spotify_oauth(request)
# Check if token is valid
if auth_manager.validate_token(spotify_token_info):
# Use access token to create Spotify client
spotify_client = spotipy.Spotify(auth=spotify_token_info['access_token'])

# Example: Fetch user profile data
user_data = spotify_client.me()
return user_data

return {"Hello": "World"}
# Check if we have a cached token
is_authenticated = bool(auth_manager.get_cached_token())
# Render index.html template
return templates.TemplateResponse("index.html", {"request": request, "is_authenticated": is_authenticated})

@app.get("/login")
def login(request: Request):
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ h11==0.14.0
httptools==0.6.1
idna==3.6
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.4
pydantic==2.5.3
pydantic_core==2.14.6
python-dotenv==1.0.0
Expand Down
43 changes: 43 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 h-screen">
<div class="container mx-auto p-8">
<div class="max-w-md mx-auto bg-white rounded-lg overflow-hidden md:max-w-lg">
<div class="md:flex">
<div class="w-full p-4">
<div class="relative">
<h1 class="text-2xl font-bold text-gray-700 mb-2">Welcome to the Home Page</h1>
{% if is_authenticated %}
<form action="/submit" method="post" class="pt-2">
{% else %}
<form action="/login" method="get" class="pt-2">
{% endif %}
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="text_field">
Enter a sentence
</label>
<input type="text" name="text_field" id="text_field"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
placeholder="Enter text">
</div>
<div class="flex justify-end">
<button type="submit"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
{% if is_authenticated %}
Submit
{% else %}
Log in with Spotify
{% endif %}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

0 comments on commit 026556a

Please sign in to comment.