Move is_used to Upload component rather than UploadFilesProvider (#2514)

This commit is contained in:
Masen Furer 2024-02-01 17:57:43 -08:00 committed by GitHub
parent 1b4229691a
commit 6d33156d15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 27 deletions

View File

@ -46,7 +46,7 @@ from reflex.components.core.client_side_routing import (
Default404Page,
wait_for_client_redirect,
)
from reflex.components.core.upload import UploadFilesProvider
from reflex.components.core.upload import Upload
from reflex.components.radix import themes
from reflex.config import get_config
from reflex.event import Event, EventHandler, EventSpec
@ -185,6 +185,7 @@ class App(Base):
# Set up the API.
self.api = FastAPI()
self.add_cors()
self.add_default_endpoints()
if self.state:
# Set up the state manager.
@ -241,12 +242,14 @@ class App(Base):
return self.api
def add_default_endpoints(self):
"""Add the default endpoints."""
"""Add default api endpoints (ping)."""
# To test the server.
self.api.get(str(constants.Endpoint.PING))(ping)
def add_optional_endpoints(self):
"""Add optional api endpoints (_upload)."""
# To upload files.
if UploadFilesProvider.is_used:
if Upload.is_used:
self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))
def add_cors(self):
@ -655,6 +658,9 @@ class App(Base):
if constants.Page404.SLUG not in self.pages:
self.add_custom_404_page()
# Add the optional endpoints (_upload)
self.add_optional_endpoints()
if not self._should_compile():
return
@ -824,8 +830,6 @@ class App(Base):
for output_path, code in compile_results:
compiler_utils.write_page(output_path, code)
self.add_default_endpoints()
@contextlib.asynccontextmanager
async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
"""Modify the state out of band.

View File

@ -98,23 +98,6 @@ class UploadFilesProvider(Component):
library = f"/{Dirs.CONTEXTS_PATH}"
tag = "UploadFilesProvider"
is_used: ClassVar[bool] = False
@classmethod
def create(cls, *children, **props) -> Component:
"""Create an UploadFilesProvider component.
Args:
*children: The children of the component.
**props: The properties of the component.
Returns:
The UploadFilesProvider component.
"""
cls.is_used = True
return super().create(*children, **props)
class Upload(Component):
"""A file upload component."""
@ -154,6 +137,9 @@ class Upload(Component):
# Whether to disable using the space/enter keys to upload.
no_keyboard: Var[bool]
# Marked True when any Upload component is created.
is_used: ClassVar[bool] = False
@classmethod
def create(cls, *children, **props) -> Component:
"""Create an upload component.
@ -165,6 +151,9 @@ class Upload(Component):
Returns:
The upload component.
"""
# Mark the Upload component as used in the app.
cls.is_used = True
# get only upload component props
supported_props = cls.get_props()
upload_props = {

View File

@ -29,8 +29,6 @@ def clear_selected_files(id_: str = DEFAULT_UPLOAD_ID) -> EventSpec: ...
def cancel_upload(upload_id: str) -> EventSpec: ...
class UploadFilesProvider(Component):
is_used: ClassVar[bool] = False
@overload
@classmethod
def create( # type: ignore
@ -90,7 +88,7 @@ class UploadFilesProvider(Component):
] = None,
**props
) -> "UploadFilesProvider":
"""Create an UploadFilesProvider component.
"""Create the component.
Args:
*children: The children of the component.
@ -101,14 +99,19 @@ class UploadFilesProvider(Component):
autofocus: Whether the component should take the focus once the page is loaded
_rename_props: props to change the name of
custom_attrs: custom attribute
**props: The properties of the component.
**props: The props of the component.
Returns:
The UploadFilesProvider component.
The component.
Raises:
TypeError: If an invalid child is passed.
"""
...
class Upload(Component):
is_used: ClassVar[bool] = False
@overload
@classmethod
def create( # type: ignore