feat: Add light/dark mode support with backend persistence

This commit is contained in:
Yunxiao Xu
2026-02-17 00:32:15 -08:00
parent 3881ca6fd8
commit de25dc8a4d
17 changed files with 253 additions and 18 deletions

View File

@@ -91,7 +91,7 @@ async def stream_agent_events(
encoded_plots: list[str] = []
for fig in plots:
buf = io.BytesIO()
fig.savefig(buf, format="png")
fig.savefig(buf, format="png", transparent=True)
plot_bytes = buf.getvalue()
assistant_plots.append(plot_bytes)
encoded_plots.append(base64.b64encode(plot_bytes).decode('utf-8'))

View File

@@ -4,7 +4,7 @@ from fastapi.security import OAuth2PasswordRequestForm
from ea_chatbot.api.utils import create_access_token, settings
from ea_chatbot.api.dependencies import history_manager, oidc_client, get_current_user
from ea_chatbot.history.models import User as UserDB
from ea_chatbot.api.schemas import Token, UserCreate, UserResponse
from ea_chatbot.api.schemas import Token, UserCreate, UserResponse, ThemeUpdate
from ea_chatbot.auth import OIDCSession
import logging
@@ -45,7 +45,8 @@ async def register(user_in: UserCreate, response: Response):
return {
"id": str(user.id),
"email": user.username,
"display_name": user.display_name
"display_name": user.display_name,
"theme_preference": user.theme_preference
}
@router.post("/login", response_model=Token)
@@ -155,5 +156,25 @@ async def get_me(current_user: UserDB = Depends(get_current_user)):
return {
"id": str(current_user.id),
"email": current_user.username,
"display_name": current_user.display_name
"display_name": current_user.display_name,
"theme_preference": current_user.theme_preference
}
@router.patch("/theme", response_model=UserResponse)
async def update_theme(
theme_in: ThemeUpdate,
current_user: UserDB = Depends(get_current_user)
):
"""Update the current user's theme preference."""
user = history_manager.update_user_theme(current_user.id, theme_in.theme)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return {
"id": str(user.id),
"email": user.username,
"display_name": user.display_name,
"theme_preference": user.theme_preference
}

View File

@@ -17,6 +17,10 @@ class UserResponse(BaseModel):
id: str
email: str
display_name: str
theme_preference: str
class ThemeUpdate(BaseModel):
theme: str
# --- History Schemas ---

View File

@@ -420,7 +420,7 @@ def main():
st.pyplot(fig)
# Convert fig to bytes
buf = io.BytesIO()
fig.savefig(buf, format="png")
fig.savefig(buf, format="png", transparent=True)
plot_bytes_list.append(buf.getvalue())
if final_state.get("dfs"):

View File

@@ -70,6 +70,16 @@ class HistoryManager:
except VerifyMismatchError:
return None
def update_user_theme(self, user_id: str, theme: str) -> Optional[User]:
"""Update the user's theme preference."""
with self.get_session() as session:
user = session.get(User, user_id)
if user:
user.theme_preference = theme
session.commit()
session.refresh(user)
return user
def sync_user_from_oidc(self, email: str, display_name: Optional[str] = None) -> User:
"""
Synchronize a user from an OIDC provider.

View File

@@ -14,6 +14,7 @@ class User(Base):
username: Mapped[str] = mapped_column(String, unique=True, index=True)
password_hash: Mapped[Optional[str]] = mapped_column(String, nullable=True)
display_name: Mapped[Optional[str]] = mapped_column(String, nullable=True)
theme_preference: Mapped[str] = mapped_column(String, default="light")
conversations: Mapped[List["Conversation"]] = relationship(back_populates="user", cascade="all, delete-orphan")