Skip to content

Commit 36c62c1

Browse files
committed
cleanups
1 parent 0b563cb commit 36c62c1

4 files changed

Lines changed: 66 additions & 40 deletions

File tree

reflex/app.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Callable,
2626
Coroutine,
2727
Dict,
28+
Mapping,
2829
MutableMapping,
2930
Type,
3031
get_args,
@@ -87,7 +88,7 @@
8788
replace_brackets_with_keywords,
8889
verify_route_validity,
8990
)
90-
from reflex.sitemap import generate_sitemaps, read_sitemap_file
91+
from reflex.sitemap import PageConfig, generate_sitemaps, read_sitemap_file
9192
from reflex.state import (
9293
BaseState,
9394
RouterData,
@@ -412,7 +413,7 @@ class App(MiddlewareMixin, LifespanMixin):
412413
# Put the toast provider in the app wrap.
413414
toaster: Component | None = dataclasses.field(default_factory=toast.provider)
414415

415-
_sitemap_properties: Dict[str, Dict] = dataclasses.field(default_factory=dict)
416+
_sitemap_properties: Dict[str, PageConfig] = dataclasses.field(default_factory=dict)
416417

417418
@property
418419
def api(self) -> FastAPI | None:
@@ -715,8 +716,7 @@ def add_page(
715716
image: The image to display on the page.
716717
on_load: The event handler(s) that will be called each time the page load.
717718
meta: The metadata of the page.
718-
sitemap_priority: The priority of the page in the sitemap. If None, the priority is calculated based on the
719-
depth of the route.
719+
sitemap_priority: The priority of the page in the sitemap. If None, the priority is calculated based on the depth of the route.
720720
sitemap_changefreq: The change frequency of the page in the sitemap. Default to 'weekly'
721721
context: Values passed to page for custom page-specific logic.
722722
@@ -819,9 +819,15 @@ def _compile_page(self, route: str, save_page: bool = True):
819819
if save_page:
820820
self._pages[route] = component
821821

822-
def get_sitemap_properties(self) -> Dict[str, Dict]:
823-
"""Get the sitemap properties."""
824-
return self._sitemap_properties
822+
def get_sitemap_properties(self) -> Mapping[str, PageConfig]:
823+
"""Get the sitemap properties.
824+
825+
Returns:
826+
The sitemap properties.
827+
"""
828+
return {
829+
route: value.copy() for route, value in self._sitemap_properties.items()
830+
}
825831

826832
def get_load_events(self, route: str) -> list[IndividualEventType[()]]:
827833
"""Get the load events for a route.

reflex/sitemap.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""This module contains functions to generate and manage the sitemap.xml file."""
22

33
from pathlib import Path
4-
from typing import Dict, List
4+
from typing import Dict, List, TypedDict
55
from xml.dom import minidom
66
from xml.etree.ElementTree import Element, SubElement, tostring
77

@@ -61,7 +61,14 @@ def generate_xml(links: List[Dict[str, str]]) -> str:
6161
return reparsed.toprettyxml(indent=" ")
6262

6363

64-
def generate_sitemaps(sitemap_config: Dict[str, Dict[str, str]]) -> None:
64+
class PageConfig(TypedDict):
65+
"""TypedDict for page configuration in sitemap."""
66+
67+
priority: float
68+
changefreq: str
69+
70+
71+
def generate_sitemaps(sitemap_config: Dict[str, PageConfig]) -> None:
6572
"""Generate the sitemap.xml file.
6673
6774
This function generates the sitemap.xml file by crawling through the available pages in the app and generating a list
@@ -76,7 +83,7 @@ def generate_sitemaps(sitemap_config: Dict[str, Dict[str, str]]) -> None:
7683

7784

7885
def generate_links_for_sitemap(
79-
sitemap_config: Dict[str, Dict[str, str]],
86+
sitemap_config: Dict[str, PageConfig],
8087
) -> List[dict[str, str]]:
8188
"""Generate a list of links for which sitemaps are generated.
8289

tests/units/test_app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,12 @@ def test_add_duplicate_page_route_error(app, first_page, second_page, route):
352352
app.add_page(second_page, route="/" + route.strip("/") if route else None)
353353

354354

355-
def test_add_page_with_sitemap_properties(app):
356-
"""Test if the sitemap properties of the app instance is set properly or not."""
355+
def test_add_page_with_sitemap_properties(app: App):
356+
"""Test if the sitemap properties of the app instance is set properly or not.
357+
358+
Args:
359+
app: The app to test.
360+
"""
357361
# check with given values.
358362
app.add_page(
359363
page1, route="/page1", sitemap_priority=0.9, sitemap_changefreq="daily"

tests/units/test_sitemap.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
import reflex as rx
7-
from reflex import Component, constants
7+
from reflex import constants
88
from reflex.app import App
99
from reflex.sitemap import (
1010
generate_links_for_sitemap,
@@ -23,42 +23,45 @@
2323

2424
@pytest.fixture
2525
def app_instance():
26-
"""Fixture to create an instance of the app."""
26+
"""Fixture to create an instance of the app.
27+
28+
Returns:
29+
An instance of the App class.
30+
"""
2731
app = App()
2832
return app
2933

3034

31-
class Page(Component):
32-
"""A simple Page component."""
35+
def page(text: str):
36+
"""A simple page component for testing.
3337
34-
def __init__(self, text, **kwargs):
35-
"""Initialize the Page component."""
36-
super().__init__(**kwargs)
37-
self.text = text
38+
Args:
39+
text: The text to display on the page.
3840
39-
def render(self):
40-
"""Render the Page component."""
41-
return rx.box(self.text)
41+
Returns:
42+
A Reflex component with the given text.
43+
"""
44+
return rx.box(text)
4245

4346

4447
@pytest.fixture
45-
def index_page() -> Page:
48+
def index_page():
4649
"""Fixture that returns an IndexPage instance.
4750
4851
Returns:
4952
An instance of IndexPage.
5053
"""
51-
return Page(text="Index")
54+
return page(text="Index")
5255

5356

5457
@pytest.fixture
55-
def about_page() -> Page:
58+
def about_page():
5659
"""Fixture that returns an AboutPage instance.
5760
5861
Returns:
5962
An instance of AboutPage.
6063
"""
61-
return Page(text="About")
64+
return page(text="About")
6265

6366

6467
mock_xml = """<?xml version="1.0" ?>
@@ -89,7 +92,13 @@ def test_generate_xml():
8992

9093

9194
def test_generate_static_sitemaps(app_instance, index_page, about_page):
92-
"""Test if the generated sitemap file is currently stored in static website or not."""
95+
"""Test if the generated sitemap file is currently stored in static website or not.
96+
97+
Args:
98+
app_instance: The app instance.
99+
index_page: The index page fixture.
100+
about_page: The about page fixture.
101+
"""
93102
pages = {"index": index_page, "about": about_page}
94103
# remove the sitemap.xml file if it exists.
95104
sitemap_file_path.unlink(missing_ok=True)
@@ -107,12 +116,12 @@ def test_generate_links_for_sitemap():
107116
"""Test if the links are generated correctly for the sitemap from the sitemap config file when no deploy url is
108117
given.
109118
"""
110-
sitemap_properties = {
111-
"index": {"priority": 0.9, "changefreq": "weekly"},
112-
"about": {"priority": 0.9, "changefreq": "weekly"},
113-
}
114-
115-
links = generate_links_for_sitemap(sitemap_properties)
119+
links = generate_links_for_sitemap(
120+
{
121+
"index": {"priority": 0.9, "changefreq": "weekly"},
122+
"about": {"priority": 0.9, "changefreq": "weekly"},
123+
}
124+
)
116125

117126
# Assert that the links are generated correctly
118127
assert links == [
@@ -129,15 +138,15 @@ def test_generate_links_for_sitemap_deploy_url():
129138
"""Test if the links are generated correctly for the sitemap from the sitemap config file when a deploy url is
130139
given.
131140
"""
132-
sitemap_properties = {
133-
"index": {"priority": 0.9, "changefreq": "weekly"},
134-
"about": {"priority": 0.9, "changefreq": "weekly"},
135-
}
136-
137141
with unittest.mock.patch("reflex.sitemap.get_config") as mock_get_config:
138142
mock_get_config().deploy_url = "http://www.google.com"
139143

140-
links = generate_links_for_sitemap(sitemap_properties)
144+
links = generate_links_for_sitemap(
145+
{
146+
"index": {"priority": 0.9, "changefreq": "weekly"},
147+
"about": {"priority": 0.9, "changefreq": "weekly"},
148+
}
149+
)
141150

142151
# Assert that the links are generated correctly
143152
assert links == [

0 commit comments

Comments
 (0)