-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python-sdk): user sessions for frameworks (#47)
Adds framework specific functions to access a user `Session` object, which represents a session of an authenticated Numerous user accessing the app. Through the `Session` cookies, user information and a specific user collection can be accessed. --------- Co-authored-by: Jens Feodor Nielsen <[email protected]>
- Loading branch information
1 parent
17fbca5
commit 4cc900a
Showing
17 changed files
with
460 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Module for integrating Numerous with various frameworks.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Module for integrating Numerous with Dash.""" | ||
|
||
from numerous import user_session | ||
from numerous.frameworks.flask import FlaskCookieGetter | ||
|
||
|
||
class DashCookieGetter(FlaskCookieGetter): | ||
pass | ||
|
||
|
||
def get_session() -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=DashCookieGetter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Module for integrating Numerous with FastAPI.""" | ||
|
||
from fastapi import Request | ||
|
||
from numerous import user_session | ||
from numerous.local import is_local_mode, local_user | ||
|
||
|
||
class FastAPICookieGetter: | ||
def __init__(self, request: Request) -> None: | ||
self.request = request | ||
|
||
def cookies(self) -> dict[str, str]: | ||
"""Get the cookies associated with the current request.""" | ||
if is_local_mode(): | ||
# Update the cookies on the fastapi server | ||
user_session.set_user_info_cookie(self.request.cookies, local_user) | ||
|
||
return {str(key): str(val) for key, val in self.request.cookies.items()} | ||
|
||
|
||
def get_session(request: Request) -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=FastAPICookieGetter(request)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Module for integrating Numerous with Flask.""" | ||
|
||
from flask import request | ||
|
||
from numerous import user_session | ||
from numerous.local import is_local_mode, local_user | ||
|
||
|
||
class FlaskCookieGetter: | ||
def cookies(self) -> dict[str, str]: | ||
"""Get the cookies associated with the current request.""" | ||
cookies = {key: str(val) for key, val in request.cookies.items()} | ||
if is_local_mode(): | ||
# Update the cookies on the flask server | ||
user_session.set_user_info_cookie(cookies, local_user) | ||
return cookies | ||
|
||
|
||
def get_session() -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=FlaskCookieGetter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Module for integrating Numerous with Marimo.""" | ||
|
||
from typing import Any | ||
|
||
from numerous import user_session | ||
from numerous.experimental import marimo | ||
from numerous.local import is_local_mode, local_user | ||
|
||
|
||
class MarimoCookieGetter: | ||
def cookies(self) -> dict[str, Any]: | ||
"""Get the cookies associated with the current request.""" | ||
if is_local_mode(): | ||
# Update the cookies on the marimo server | ||
user_session.set_user_info_cookie(marimo.cookies(), local_user) | ||
return {key: str(val) for key, val in marimo.cookies().items()} | ||
|
||
|
||
def get_session() -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=MarimoCookieGetter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Module for integrating Numerous with Panel.""" | ||
|
||
import panel as pn | ||
|
||
from numerous import user_session | ||
from numerous.local import is_local_mode, local_user | ||
|
||
|
||
class PanelCookieGetter: | ||
@staticmethod | ||
def cookies() -> dict[str, str]: | ||
"""Get the cookies associated with the current request.""" | ||
if is_local_mode(): | ||
# Add the user info to the cookies on panel server | ||
user_session.set_user_info_cookie(pn.state.cookies, local_user) | ||
|
||
if pn.state.curdoc and pn.state.curdoc.session_context: | ||
return {key: str(val) for key, val in pn.state.cookies.items()} | ||
return {} | ||
|
||
|
||
def get_session() -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=PanelCookieGetter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Module for integrating Numerous with Streamlit.""" | ||
|
||
import streamlit as st | ||
|
||
from numerous import user_session | ||
from numerous.local import is_local_mode, local_user | ||
|
||
|
||
class StreamlitCookieGetter: | ||
def cookies(self) -> dict[str, str]: | ||
"""Get the cookies associated with the current request.""" | ||
cookies = {key: str(val) for key, val in st.context.cookies.items()} | ||
if is_local_mode(): | ||
# Update the cookies on the streamlit server | ||
user_session.set_user_info_cookie(cookies, local_user) | ||
return cookies | ||
|
||
|
||
def get_session() -> user_session.Session: | ||
""" | ||
Get the session for the current user. | ||
Returns: | ||
Session: The session for the current user. | ||
""" | ||
return user_session.Session(cg=StreamlitCookieGetter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.