[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.
This commit is contained in:
PeterYusuke 2023-02-23 14:33:04 +09:00 committed by GitHub
parent fc29202b71
commit f76acb2d9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 11 deletions

View File

@ -199,6 +199,7 @@ class App(Base):
image=constants.DEFAULT_IMAGE, image=constants.DEFAULT_IMAGE,
on_load: Optional[EventHandler] = None, on_load: Optional[EventHandler] = None,
path: Optional[str] = None, path: Optional[str] = None,
meta: List[Dict] = constants.DEFAULT_META_LIST,
): ):
"""Add a page to the app. """Add a page to the app.
@ -213,6 +214,7 @@ class App(Base):
description: The description of the page. description: The description of the page.
image: The image to display on the page. image: The image to display on the page.
on_load: The event handler that will be called each time the page load. 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: if path is not None:
utils.deprecate( utils.deprecate(
@ -238,7 +240,7 @@ class App(Base):
# Add meta information to the component. # Add meta information to the component.
compiler_utils.add_meta( compiler_utils.add_meta(
component, title=title, image=image, description=description component, title=title, image=image, description=description, meta=meta
) )
# Format the route. # Format the route.
@ -284,7 +286,14 @@ class App(Base):
f"You cannot use multiple catchall for the same dynamic route ({route} !== {new_route})" 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. """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. 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. title: The title of the page.
description: The description of the page. description: The description of the page.
image: The image to display on the page. image: The image to display on the page.
meta: The metadata of the page.
""" """
title = title or constants.TITLE_404 title = title or constants.TITLE_404
image = image or constants.FAVICON_404 image = image or constants.FAVICON_404
@ -303,7 +313,7 @@ class App(Base):
component = component if isinstance(component, Component) else component() component = component if isinstance(component, Component) else component()
compiler_utils.add_meta( compiler_utils.add_meta(
component, title=title, image=image, description=description component, title=title, image=image, description=description, meta=meta
) )
froute = utils.format_route froute = utils.format_route

View File

@ -16,6 +16,7 @@ from pynecone.components.base import (
Image, Image,
Link, Link,
Main, Main,
Meta,
Script, Script,
Title, Title,
) )
@ -277,7 +278,9 @@ def get_components_path() -> str:
return os.path.join(constants.WEB_UTILS_DIR, "components" + constants.JS_EXT) 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. """Add metadata to a page.
Args: Args:
@ -285,15 +288,19 @@ def add_meta(page: Component, title: str, image: str, description: str) -> Compo
title: The title of the page. title: The title of the page.
image: The image for the page. image: The image for the page.
description: The description of the page. description: The description of the page.
meta: The metadata list.
Returns: Returns:
The component with the metadata added. The component with the metadata added.
""" """
meta_tags = [Meta.create(**item) for item in meta]
page.children.append( page.children.append(
Head.create( Head.create(
Title.create(title), Title.create(title),
Description.create(content=description), Description.create(content=description),
Image.create(content=image), Image.create(content=image),
*meta_tags,
) )
) )

View File

@ -4,4 +4,4 @@ from .body import Body
from .document import ColorModeScript, DocumentHead, Html, Main, Script from .document import ColorModeScript, DocumentHead, Html, Main, Script
from .head import Head from .head import Head
from .link import Link from .link import Link
from .meta import Description, Image, Title from .meta import Description, Image, Meta, Title

View File

@ -30,13 +30,22 @@ class Meta(Component):
tag = "meta" 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): class Description(Meta):
"""A component that displays the title of the current page.""" """A component that displays the title of the current page."""
# The description of the page.
content: Optional[str] = None
# The type of the description. # The type of the description.
name: str = "description" name: str = "description"
@ -44,8 +53,5 @@ class Description(Meta):
class Image(Meta): class Image(Meta):
"""A component that displays the title of the current page.""" """A component that displays the title of the current page."""
# The image of the page.
content: Optional[str] = None
# The type of the image. # The type of the image.
property: str = "og:image" property: str = "og:image"

View File

@ -124,6 +124,8 @@ DEFAULT_TITLE = "Pynecone App"
DEFAULT_DESCRIPTION = "A Pynecone app." DEFAULT_DESCRIPTION = "A Pynecone app."
# The default image to show for Pynecone apps. # The default image to show for Pynecone apps.
DEFAULT_IMAGE = "favicon.ico" DEFAULT_IMAGE = "favicon.ico"
# The default meta list to show for Pynecone apps.
DEFAULT_META_LIST = []
# The gitignore file. # The gitignore file.