From f76acb2d9c7239c6fd4ba31a7616f95e35311af0 Mon Sep 17 00:00:00 2001 From: PeterYusuke <58464065+PeterYusuke@users.noreply.github.com> Date: Thu, 23 Feb 2023 14:33:04 +0900 Subject: [PATCH] [Fix issue 563] Add meta arg to page_add method (#589) * Add meta arg to page_add method * Remove TypeError of "|". Set default param value. --- pynecone/app.py | 16 +++++++++++++--- pynecone/compiler/utils.py | 9 ++++++++- pynecone/components/base/__init__.py | 2 +- pynecone/components/base/meta.py | 18 ++++++++++++------ pynecone/constants.py | 2 ++ 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/pynecone/app.py b/pynecone/app.py index 7cc676ee0..f831091c1 100644 --- a/pynecone/app.py +++ b/pynecone/app.py @@ -199,6 +199,7 @@ class App(Base): image=constants.DEFAULT_IMAGE, on_load: Optional[EventHandler] = None, path: Optional[str] = None, + meta: List[Dict] = constants.DEFAULT_META_LIST, ): """Add a page to the app. @@ -213,6 +214,7 @@ class App(Base): description: The description of the page. image: The image to display on the page. on_load: The event handler that will be called each time the page load. + meta: The metadata of the page. """ if path is not None: utils.deprecate( @@ -238,7 +240,7 @@ class App(Base): # Add meta information to the component. compiler_utils.add_meta( - component, title=title, image=image, description=description + component, title=title, image=image, description=description, meta=meta ) # Format the route. @@ -284,7 +286,14 @@ class App(Base): f"You cannot use multiple catchall for the same dynamic route ({route} !== {new_route})" ) - def add_custom_404_page(self, component, title=None, image=None, description=None): + def add_custom_404_page( + self, + component, + title=None, + image=None, + description=None, + meta=constants.DEFAULT_META_LIST, + ): """Define a custom 404 page for any url having no match. If there is no page defined on 'index' route, add the 404 page to it. @@ -295,6 +304,7 @@ class App(Base): title: The title of the page. description: The description of the page. image: The image to display on the page. + meta: The metadata of the page. """ title = title or constants.TITLE_404 image = image or constants.FAVICON_404 @@ -303,7 +313,7 @@ class App(Base): component = component if isinstance(component, Component) else component() compiler_utils.add_meta( - component, title=title, image=image, description=description + component, title=title, image=image, description=description, meta=meta ) froute = utils.format_route diff --git a/pynecone/compiler/utils.py b/pynecone/compiler/utils.py index 7263df013..17acf9b01 100644 --- a/pynecone/compiler/utils.py +++ b/pynecone/compiler/utils.py @@ -16,6 +16,7 @@ from pynecone.components.base import ( Image, Link, Main, + Meta, Script, Title, ) @@ -277,7 +278,9 @@ def get_components_path() -> str: return os.path.join(constants.WEB_UTILS_DIR, "components" + constants.JS_EXT) -def add_meta(page: Component, title: str, image: str, description: str) -> Component: +def add_meta( + page: Component, title: str, image: str, description: str, meta: List[Dict] +) -> Component: """Add metadata to a page. Args: @@ -285,15 +288,19 @@ def add_meta(page: Component, title: str, image: str, description: str) -> Compo title: The title of the page. image: The image for the page. description: The description of the page. + meta: The metadata list. Returns: The component with the metadata added. """ + meta_tags = [Meta.create(**item) for item in meta] + page.children.append( Head.create( Title.create(title), Description.create(content=description), Image.create(content=image), + *meta_tags, ) ) diff --git a/pynecone/components/base/__init__.py b/pynecone/components/base/__init__.py index 8c83f8fbf..4095e1932 100644 --- a/pynecone/components/base/__init__.py +++ b/pynecone/components/base/__init__.py @@ -4,4 +4,4 @@ from .body import Body from .document import ColorModeScript, DocumentHead, Html, Main, Script from .head import Head from .link import Link -from .meta import Description, Image, Title +from .meta import Description, Image, Meta, Title diff --git a/pynecone/components/base/meta.py b/pynecone/components/base/meta.py index a7fd114c2..99d0c9167 100644 --- a/pynecone/components/base/meta.py +++ b/pynecone/components/base/meta.py @@ -30,13 +30,22 @@ class Meta(Component): tag = "meta" + # The description of character encoding. + char_set: Optional[str] = None + + # The value of meta. + content: Optional[str] = None + + # The name of metadata. + name: Optional[str] = None + + # The type of metadata value. + property: Optional[str] = None + class Description(Meta): """A component that displays the title of the current page.""" - # The description of the page. - content: Optional[str] = None - # The type of the description. name: str = "description" @@ -44,8 +53,5 @@ class Description(Meta): class Image(Meta): """A component that displays the title of the current page.""" - # The image of the page. - content: Optional[str] = None - # The type of the image. property: str = "og:image" diff --git a/pynecone/constants.py b/pynecone/constants.py index 80943ae3f..f43c029c4 100644 --- a/pynecone/constants.py +++ b/pynecone/constants.py @@ -124,6 +124,8 @@ DEFAULT_TITLE = "Pynecone App" DEFAULT_DESCRIPTION = "A Pynecone app." # The default image to show for Pynecone apps. DEFAULT_IMAGE = "favicon.ico" +# The default meta list to show for Pynecone apps. +DEFAULT_META_LIST = [] # The gitignore file.