[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,
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

View File

@ -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,
)
)

View File

@ -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

View File

@ -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"

View File

@ -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.