:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Any, cast
from django.core.cache import cache
import structlog
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import status, viewsets
from rest_framework.request import Request
from rest_framework.response import Response
from posthog.api.mixins import TypedRequest, ValidatedRequest, validated_request
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
from posthog.models.user import User
from posthog.rate_limit import ClickHouseBurstRateThrottle
from products.streamlit_apps.backend.facade import api
from products.streamlit_apps.backend.facade.contracts import (
CreateAppInput,
CreateVersionFromSourceInput,
EditVersionSourceInput,
UpdateAppInput,
)
from products.streamlit_apps.backend.presentation.serializers import (
ActivateVersionRequestSerializer,
ActivateVersionResponseSerializer,
CreateAppInputSerializer,
CreateVersionFromSourceInputSerializer,
EditVersionSourceInputSerializer,
SourceEditErrorSerializer,
StreamlitAppMinimalSerializer,
StreamlitAppsAccessPermission,
StreamlitAppSandboxSerializer,
StreamlitAppSerializer,
StreamlitAppStatusSerializer,
StreamlitAppVersionListSerializer,
StreamlitAppVersionSerializer,
StreamlitAppVersionSourceSerializer,
StreamlitConnectInfoSerializer,
UpdateAppInputSerializer,
UploadVersionRequestSerializer,
VersionConflictSerializer,
VersionSourceQuerySerializer,
)
logger = structlog.get_logger(__name__)
_STATUS_CACHE_TTL_SECONDS = 2
# Actions that let a token ship app code, choose which code runs, or make it run.
# The sandbox bridge executes the active version's HogQL as that version's author,
# so tokens driving these paths must themselves hold query:read — otherwise a
# streamlit_app-only key escalates to HogQL reads through code it uploads, or by
# activating a version authored by someone with broader access. activate_version
# counts without an explicit start of its own: the next start by anyone, or a live
# sandbox its best-effort stop failed to reclaim, runs the newly activated code.
_QUERY_CAPABLE_WRITE_ACTIONS = frozenset(
{"upload_version", "create_version_from_source", "edit_source", "activate_version", "start", "restart"}
)
class StreamlitAppViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
scope_object = "streamlit_app"
# Custom actions are invisible to the default CRUD scope derivation, so
# without these lists personal-API-key (and MCP) calls to them are refused.
scope_object_read_actions = ["list", "retrieve", "versions", "source", "get_status", "connect_info"]
scope_object_write_actions = [
"create",
"update",
"partial_update",
"destroy",
"upload_version",
"create_version_from_source",
"edit_source",
"activate_version",
"start",
"stop",
"restart",
]
permission_classes = [StreamlitAppsAccessPermission]
lookup_field = "short_id"
def dangerously_get_required_scopes(self, request: Request, view: "StreamlitAppViewSet") -> list[str] | None:
action = getattr(view, "action", None)
if action in _QUERY_CAPABLE_WRITE_ACTIONS:
return ["streamlit_app:write", "query:read"]
if action == "connect_info":
# The iframe renders whatever the app queried — reading it is reading query results.
return ["streamlit_app:read", "query:read"]
return None
@extend_schema(summary="List streamlit apps", responses={200: StreamlitAppMinimalSerializer(many=True)})
def list(self, request: Request, **kwargs: Any) -> Response:
apps = api.list_apps(self.team_id)
page = self.paginate_queryset(apps)
if page is not None:
return self.get_paginated_response(StreamlitAppMinimalSerializer(page, many=True).data)
return Response(StreamlitAppMinimalSerializer(apps, many=True).data)
@validated_request(
request_serializer=CreateAppInputSerializer,
summary="Create a streamlit app",
responses={201: OpenApiResponse(response=StreamlitAppSerializer)},
)
def create(self, request: TypedRequest[CreateAppInput], **kwargs: Any) -> Response:
try:
app = api.create_app(
team_id=self.team_id,
user=cast(User, request.user),
data=request.validated_data,
)
except ValueError as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(StreamlitAppSerializer(app).data, status=status.HTTP_201_CREATED)
@extend_schema(summary="Retrieve a streamlit app", responses={200: StreamlitAppSerializer})
def retrieve(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
app = api.get_app(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(StreamlitAppSerializer(app).data)
def _update(self, request: TypedRequest[UpdateAppInput], short_id: str) -> Response:
try:
app = api.update_app(
team_id=self.team_id,
short_id=short_id,
data=request.validated_data,
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except ValueError as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(StreamlitAppSerializer(app).data)
@validated_request(
request_serializer=UpdateAppInputSerializer,
summary="Update a streamlit app",
responses={200: OpenApiResponse(response=StreamlitAppSerializer)},
)
def update(self, request: TypedRequest[UpdateAppInput], short_id: str, **kwargs: Any) -> Response:
return self._update(request, short_id)
@validated_request(
request_serializer=UpdateAppInputSerializer,
summary="Partially update a streamlit app",
responses={200: OpenApiResponse(response=StreamlitAppSerializer)},
)
def partial_update(self, request: TypedRequest[UpdateAppInput], short_id: str, **kwargs: Any) -> Response:
return self._update(request, short_id)
@extend_schema(summary="Delete a streamlit app", request=None, responses={204: None})
def destroy(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
api.delete_app(team_id=self.team_id, short_id=short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(status=status.HTTP_204_NO_CONTENT)
@extend_schema(
summary="List app versions",
responses={200: StreamlitAppVersionListSerializer},
)
@action(methods=["GET"], detail=True, url_path="versions")
def versions(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
versions = api.list_versions(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response({"results": StreamlitAppVersionSerializer(versions, many=True).data})
@extend_schema(
summary="Upload a new app version",
request=UploadVersionRequestSerializer,
responses={201: StreamlitAppVersionSerializer},
)
@action(methods=["POST"], detail=True, url_path="upload_version", throttle_classes=[ClickHouseBurstRateThrottle])
def upload_version(self, request: Request, short_id: str, **kwargs: Any) -> Response:
zip_file = request.FILES.get("file")
if not zip_file:
return Response({"detail": "No file provided."}, status=status.HTTP_400_BAD_REQUEST)
# Reject oversized uploads by their declared size before reading the body
# into memory, so a large multipart POST can't force a full allocation.
try:
api.check_zip_size(zip_file.size)
except api.ZipTooLargeError as e:
return Response({"detail": str(e)}, status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)
file_content = zip_file.read()
try:
version = api.upload_version(
team_id=self.team_id,
short_id=short_id,
user=cast(User, request.user),
file_content=file_content,
declared_size=zip_file.size,
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.ZipTooLargeError as e:
return Response({"detail": str(e)}, status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)
except api.InvalidZipError as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
except api.ConcurrentUploadError as e:
return Response({"detail": str(e)}, status=status.HTTP_409_CONFLICT)
return Response(StreamlitAppVersionSerializer(version).data, status=status.HTTP_201_CREATED)
@validated_request(
request_serializer=CreateVersionFromSourceInputSerializer,
summary="Create an app version from source code",
responses={201: OpenApiResponse(response=StreamlitAppVersionSerializer)},
)
@action(
methods=["POST"],
detail=True,
url_path="create_version_from_source",
throttle_classes=[ClickHouseBurstRateThrottle],
)
def create_version_from_source(
self, request: TypedRequest[CreateVersionFromSourceInput], short_id: str, **kwargs: Any
) -> Response:
try:
version = api.create_version_from_source(
team_id=self.team_id,
short_id=short_id,
user=cast(User, request.user),
data=request.validated_data,
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.ZipTooLargeError as e:
# The serializer bounds the raw bytes, but compression overhead and per-entry
# headers can still push the built archive past the zip limit.
return Response({"detail": str(e)}, status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)
except api.InvalidZipError as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
except api.ConcurrentUploadError as e:
return Response({"detail": str(e)}, status=status.HTTP_409_CONFLICT)
return Response(StreamlitAppVersionSerializer(version).data, status=status.HTTP_201_CREATED)
@validated_request(
query_serializer=VersionSourceQuerySerializer,
summary="Read the source of an app version",
description=(
"Returns the file manifest of a version and the text of each text file. "
"Binary files appear in the manifest without content."
),
responses={200: OpenApiResponse(response=StreamlitAppVersionSourceSerializer)},
)
@action(methods=["GET"], detail=True, url_path="source")
def source(self, request: ValidatedRequest, short_id: str, **kwargs: Any) -> Response:
query = request.validated_query_data
try:
version_source = api.get_version_source(
team_id=self.team_id,
short_id=short_id,
version_number=query.get("version_number"),
paths=query.get("paths"),
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except (api.VersionNotFoundError, api.NoActiveVersionError) as e:
return Response({"detail": str(e)}, status=status.HTTP_404_NOT_FOUND)
return Response(StreamlitAppVersionSourceSerializer(version_source).data)
@validated_request(
request_serializer=EditVersionSourceInputSerializer,
summary="Create an app version by editing an existing version",
description=(
"Applies exact text edits, file creations, and file deletions to base_version, then stores the result "
"as a new active version. Files that no change touches stay byte-for-byte the same."
),
responses={
201: OpenApiResponse(response=StreamlitAppVersionSerializer),
400: OpenApiResponse(response=SourceEditErrorSerializer),
409: OpenApiResponse(response=VersionConflictSerializer),
},
)
@action(methods=["POST"], detail=True, url_path="edit_source", throttle_classes=[ClickHouseBurstRateThrottle])
def edit_source(self, request: TypedRequest[EditVersionSourceInput], short_id: str, **kwargs: Any) -> Response:
try:
version = api.edit_version_source(
team_id=self.team_id,
short_id=short_id,
user=cast(User, request.user),
data=request.validated_data,
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.VersionNotFoundError as e:
return Response({"detail": str(e)}, status=status.HTTP_404_NOT_FOUND)
except api.VersionConflictError as e:
return Response({"detail": str(e), "current_version": e.current_version}, status=status.HTTP_409_CONFLICT)
except api.SourceEditError as e:
return Response(
{"detail": str(e), "path": e.path, "edit_index": e.edit_index}, status=status.HTTP_400_BAD_REQUEST
)
except api.ZipTooLargeError as e:
return Response({"detail": str(e)}, status=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE)
except api.InvalidZipError as e:
return Response({"detail": str(e), "path": None, "edit_index": None}, status=status.HTTP_400_BAD_REQUEST)
return Response(StreamlitAppVersionSerializer(version).data, status=status.HTTP_201_CREATED)
@extend_schema(
summary="Activate an existing app version",
request=ActivateVersionRequestSerializer,
responses={200: ActivateVersionResponseSerializer},
)
@action(methods=["POST"], detail=True, url_path="activate_version")
def activate_version(self, request: Request, short_id: str, **kwargs: Any) -> Response:
version_number = request.data.get("version_number")
if version_number is None:
return Response({"detail": "version_number is required."}, status=status.HTTP_400_BAD_REQUEST)
# Guard the type before the facade call: a non-integer (e.g. "abc" or 1.5)
# would otherwise raise ValueError deep in the ORM and surface as a 500.
if not isinstance(version_number, int) or isinstance(version_number, bool):
return Response({"detail": "version_number must be an integer."}, status=status.HTTP_400_BAD_REQUEST)
try:
version = api.activate_version(
team_id=self.team_id,
short_id=short_id,
user=cast(User, request.user),
version_number=version_number,
)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.VersionNotFoundError as e:
return Response({"detail": str(e)}, status=status.HTTP_404_NOT_FOUND)
return Response({"active_version": StreamlitAppVersionSerializer(version).data})
@extend_schema(
summary="Get app sandbox status",
responses={200: StreamlitAppStatusSerializer},
)
@action(methods=["GET"], detail=True, url_path="status")
def get_status(self, request: Request, short_id: str, **kwargs: Any) -> Response:
cache_key = f"streamlit_sandbox_status:{self.team_id}:{short_id}"
cached = cache.get(cache_key)
if cached is not None:
return Response(cached)
try:
sandbox = api.get_status(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
payload = StreamlitAppSandboxSerializer(sandbox).data
cache.set(cache_key, payload, _STATUS_CACHE_TTL_SECONDS)
return Response(payload)
@extend_schema(
summary="Start the app sandbox",
request=None,
responses={200: StreamlitAppSerializer, 202: StreamlitAppSerializer},
)
@action(methods=["POST"], detail=True, url_path="start", throttle_classes=[ClickHouseBurstRateThrottle])
def start(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
app, already_running = api.start_app(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.NoActiveVersionError as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
if already_running:
return Response(StreamlitAppSerializer(app).data)
return Response(StreamlitAppSerializer(app).data, status=status.HTTP_202_ACCEPTED)
@extend_schema(
summary="Stop the app sandbox",
request=None,
responses={200: StreamlitAppSerializer},
)
@action(methods=["POST"], detail=True, url_path="stop")
def stop(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
app = api.stop_app(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except Exception as e:
logger.exception("streamlit_app_stop_failed", short_id=short_id, error=str(e))
return Response({"detail": "Failed to stop app."}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
return Response(StreamlitAppSerializer(app).data)
@extend_schema(
summary="Get iframe connection info for a running app",
responses={200: StreamlitConnectInfoSerializer},
)
@action(methods=["GET"], detail=True, url_path="connect_info", throttle_classes=[ClickHouseBurstRateThrottle])
def connect_info(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
connect_info = api.get_connect_info(self.team_id, short_id, cast(User, request.user))
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
except api.AppNotRunningError as e:
return Response({"detail": str(e)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
except api.ConnectUnavailableError as e:
return Response({"detail": str(e)}, status=status.HTTP_502_BAD_GATEWAY)
return Response(StreamlitConnectInfoSerializer(connect_info).data)
@extend_schema(
summary="Restart the app sandbox",
request=None,
responses={202: StreamlitAppSerializer},
)
@action(methods=["POST"], detail=True, url_path="restart", throttle_classes=[ClickHouseBurstRateThrottle])
def restart(self, request: Request, short_id: str, **kwargs: Any) -> Response:
try:
app, _transitioning = api.restart_app(self.team_id, short_id)
except api.AppNotFoundError:
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(StreamlitAppSerializer(app).data, status=status.HTTP_202_ACCEPTED)