Skip to content

Commit db23825

Browse files
committed
move custom components to their own directory
1 parent 13d6113 commit db23825

File tree

2 files changed

+203
-234
lines changed

2 files changed

+203
-234
lines changed

reflex/compiler/templates.py

Lines changed: 0 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -651,209 +651,6 @@ def _render_hooks(
651651
return hooks_code
652652

653653

654-
def _custom_components_pyproject_toml_template(**kwargs) -> str:
655-
"""Template for custom components pyproject.toml.
656-
657-
Args:
658-
**kwargs: Template context variables including package_name, module_name, reflex_version.
659-
660-
Returns:
661-
Rendered pyproject.toml content as string.
662-
"""
663-
package_name = kwargs.get("package_name", "")
664-
module_name = kwargs.get("module_name", "")
665-
reflex_version = kwargs.get("reflex_version", "")
666-
667-
return f"""[build-system]
668-
requires = ["setuptools", "wheel"]
669-
build-backend = "setuptools.build_meta"
670-
671-
[project]
672-
name = "{package_name}"
673-
version = "0.0.1"
674-
description = "Reflex custom component {module_name}"
675-
readme = "README.md"
676-
license = {{ text = "Apache-2.0" }}
677-
requires-python = ">=3.10"
678-
authors = [{{ name = "", email = "YOUREMAIL@domain.com" }}]
679-
keywords = ["reflex","reflex-custom-components"]
680-
681-
dependencies = ["reflex>={reflex_version}"]
682-
683-
classifiers = ["Development Status :: 4 - Beta"]
684-
685-
[project.urls]
686-
687-
[project.optional-dependencies]
688-
dev = ["build", "twine"]
689-
690-
[tool.setuptools.packages.find]
691-
where = ["custom_components"]
692-
"""
693-
694-
695-
def _custom_components_readme_template(**kwargs) -> str:
696-
"""Template for custom components README.
697-
698-
Args:
699-
**kwargs: Template context variables including module_name, package_name.
700-
701-
Returns:
702-
Rendered README.md content as string.
703-
"""
704-
module_name = kwargs.get("module_name", "")
705-
package_name = kwargs.get("package_name", "")
706-
707-
return f"""# {module_name}
708-
709-
A Reflex custom component {module_name}.
710-
711-
## Installation
712-
713-
```bash
714-
pip install {package_name}
715-
```
716-
"""
717-
718-
719-
def _custom_components_source_template(**kwargs) -> str:
720-
"""Template for custom components source.
721-
722-
Args:
723-
**kwargs: Template context variables including component_class_name, module_name.
724-
725-
Returns:
726-
Rendered custom component source code as string.
727-
"""
728-
component_class_name = kwargs.get("component_class_name", "")
729-
module_name = kwargs.get("module_name", "")
730-
731-
return rf'''
732-
"""Reflex custom component {component_class_name}."""
733-
734-
# For wrapping react guide, visit https://reflex.dev/docs/wrapping-react/overview/
735-
736-
import reflex as rx
737-
738-
# Some libraries you want to wrap may require dynamic imports.
739-
# This is because they they may not be compatible with Server-Side Rendering (SSR).
740-
# To handle this in Reflex, all you need to do is subclass `NoSSRComponent` instead.
741-
# For example:
742-
# from reflex.components.component import NoSSRComponent
743-
# class {component_class_name}(NoSSRComponent):
744-
# pass
745-
746-
747-
class {component_class_name}(rx.Component):
748-
"""{component_class_name} component."""
749-
750-
# The React library to wrap.
751-
library = "Fill-Me"
752-
753-
# The React component tag.
754-
tag = "Fill-Me"
755-
756-
# If the tag is the default export from the module, you must set is_default = True.
757-
# This is normally used when components don't have curly braces around them when importing.
758-
# is_default = True
759-
760-
# If you are wrapping another components with the same tag as a component in your project
761-
# you can use aliases to differentiate between them and avoid naming conflicts.
762-
# alias = "Other{component_class_name}"
763-
764-
# The props of the React component.
765-
# Note: when Reflex compiles the component to Javascript,
766-
# `snake_case` property names are automatically formatted as `camelCase`.
767-
# The prop names may be defined in `camelCase` as well.
768-
# some_prop: rx.Var[str] = "some default value"
769-
# some_other_prop: rx.Var[int] = 1
770-
771-
# By default Reflex will install the library you have specified in the library property.
772-
# However, sometimes you may need to install other libraries to use a component.
773-
# In this case you can use the lib_dependencies property to specify other libraries to install.
774-
# lib_dependencies: list[str] = []
775-
776-
# Event triggers declaration if any.
777-
# Below is equivalent to merging `{{ "on_change": lambda e: [e] }}`
778-
# onto the default event triggers of parent/base Component.
779-
# The function defined for the `on_change` trigger maps event for the javascript
780-
# trigger to what will be passed to the backend event handler function.
781-
# on_change: rx.EventHandler[lambda e: [e]]
782-
783-
# To add custom code to your component
784-
# def _get_custom_code(self) -> str:
785-
# return "const customCode = 'customCode';"
786-
787-
788-
{module_name} = {component_class_name}.create
789-
'''
790-
791-
792-
def _custom_components_init_template(**kwargs) -> str:
793-
"""Template for custom components __init__.py.
794-
795-
Args:
796-
**kwargs: Template context variables including module_name.
797-
798-
Returns:
799-
Rendered __init__.py content as string.
800-
"""
801-
module_name = kwargs.get("module_name", "")
802-
return f"from .{module_name} import *"
803-
804-
805-
def _custom_components_demo_app_template(**kwargs) -> str:
806-
"""Template for custom components demo app.
807-
808-
Args:
809-
**kwargs: Template context variables including custom_component_module_dir, module_name.
810-
811-
Returns:
812-
Rendered demo app source code as string.
813-
"""
814-
custom_component_module_dir = kwargs.get("custom_component_module_dir", "")
815-
module_name = kwargs.get("module_name", "")
816-
817-
return rf'''
818-
"""Welcome to Reflex! This file showcases the custom component in a basic app."""
819-
820-
from rxconfig import config
821-
822-
import reflex as rx
823-
824-
from {custom_component_module_dir} import {module_name}
825-
826-
filename = f"{{config.app_name}}/{{config.app_name}}.py"
827-
828-
829-
class State(rx.State):
830-
"""The app state."""
831-
pass
832-
833-
def index() -> rx.Component:
834-
return rx.center(
835-
rx.theme_panel(),
836-
rx.vstack(
837-
rx.heading("Welcome to Reflex!", size="9"),
838-
rx.text(
839-
"Test your custom component by editing ",
840-
rx.code(filename),
841-
font_size="2em",
842-
),
843-
{module_name}(),
844-
align="center",
845-
spacing="7",
846-
),
847-
height="100vh",
848-
)
849-
850-
851-
# Add state and page to the app.
852-
app = rx.App()
853-
app.add_page(index)
854-
'''
855-
856-
857654
# Template instances
858655
class TemplateFunction:
859656
"""Wrapper for template functions to match Jinja Template interface."""
@@ -928,20 +725,3 @@ def render(self, **kwargs) -> str:
928725
kwargs.get("memo", None),
929726
)
930727
)
931-
932-
# Code that generate the pyproject.toml file for custom components.
933-
CUSTOM_COMPONENTS_PYPROJECT_TOML = TemplateFunction(
934-
_custom_components_pyproject_toml_template
935-
)
936-
937-
# Code that generates the README file for custom components.
938-
CUSTOM_COMPONENTS_README = TemplateFunction(_custom_components_readme_template)
939-
940-
# Code that generates the source file for custom components.
941-
CUSTOM_COMPONENTS_SOURCE = TemplateFunction(_custom_components_source_template)
942-
943-
# Code that generates the init file for custom components.
944-
CUSTOM_COMPONENTS_INIT_FILE = TemplateFunction(_custom_components_init_template)
945-
946-
# Code that generates the demo app main py file for testing custom components.
947-
CUSTOM_COMPONENTS_DEMO_APP = TemplateFunction(_custom_components_demo_app_template)

0 commit comments

Comments
 (0)