Skip to content

Commit

Permalink
use module/global variable instead of ClassVar
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Jan 21, 2024
1 parent 1ff6f5e commit 4ee28e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
Default404Page,
wait_for_client_redirect,
)
from reflex.components.core.upload import Upload
from reflex.components.core.upload import upload_component_used
from reflex.config import get_config
from reflex.event import Event, EventHandler, EventSpec
from reflex.middleware import HydrateMiddleware, Middleware
Expand Down Expand Up @@ -245,7 +245,7 @@ def add_default_endpoints(self):
self.api.get(str(constants.Endpoint.PING))(ping)

# To upload files.
if Upload._used:
if upload_component_used:
self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))

def add_cors(self):
Expand Down
12 changes: 7 additions & 5 deletions reflex/components/core/upload.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A file upload component."""
from __future__ import annotations

from typing import Any, ClassVar, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union

from reflex import constants
from reflex.components.chakra.forms.input import Input
Expand Down Expand Up @@ -99,12 +99,13 @@ class UploadFilesProvider(Component):
tag = "UploadFilesProvider"


# Indicate at least one Upload component is used, to enable the /_upload endpoint.
upload_component_used: bool = False


class Upload(Component):
"""A file upload component."""

# Indicate at least one Upload component is used, to enable the /_upload endpoint.
_used: ClassVar[bool] = False

library = "react-dropzone@14.2.3"

tag = "ReactDropzone"
Expand Down Expand Up @@ -151,7 +152,8 @@ def create(cls, *children, **props) -> Component:
Returns:
The upload component.
"""
cls._used = True
global upload_component_used
upload_component_used = True

# get only upload component props
supported_props = cls.get_props()
Expand Down
6 changes: 3 additions & 3 deletions reflex/components/core/upload.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from typing import Any, Dict, Literal, Optional, Union, overload
from reflex.vars import Var, BaseVar, ComputedVar
from reflex.event import EventChain, EventHandler, EventSpec
from reflex.style import Style
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union
from reflex import constants
from reflex.components.chakra.forms.input import Input
from reflex.components.chakra.layout.box import Box
Expand Down Expand Up @@ -107,13 +107,14 @@ class UploadFilesProvider(Component):
"""
...

upload_component_used: bool

class Upload(Component):
@overload
@classmethod
def create( # type: ignore
cls,
*children,
_used: Optional[ClassVar[bool]] = None,
accept: Optional[
Union[Var[Optional[Dict[str, List]]], Optional[Dict[str, List]]]
] = None,
Expand Down Expand Up @@ -185,7 +186,6 @@ class Upload(Component):
Args:
*children: The children of the component.
_used: Indicate at least one Upload component is used, to enable the /_upload endpoint.
accept: The list of accepted file types. This should be a dictionary of MIME types as keys and array of file formats as values. supported MIME types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
disabled: Whether the dropzone is disabled.
max_files: The maximum number of files that can be uploaded.
Expand Down

0 comments on commit 4ee28e3

Please sign in to comment.