Skip to content

Allow absolute URL navigation #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mesop/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mesop.examples import index as index
from mesop.examples import integrations as integrations
from mesop.examples import many_checkboxes as many_checkboxes
from mesop.examples import navigate_absolute as navigate_absolute
from mesop.examples import navigate_advanced as navigate_advanced
from mesop.examples import nested as nested
from mesop.examples import on_load as on_load
Expand Down
23 changes: 23 additions & 0 deletions mesop/examples/navigate_absolute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mesop as me


@me.page(path="/navigate_absolute")
def page1():
me.text("Navigate to absolute URLs")
me.button("navigate https", on_click=navigate_https)
me.button("navigate http", on_click=navigate_http)
me.button("navigate yield", on_click=navigate_yield)


def navigate_https(e: me.ClickEvent):
me.navigate("https://google.com/")


def navigate_http(e: me.ClickEvent):
me.navigate("http://example.com/")


def navigate_yield(e: me.ClickEvent):
yield
me.navigate("https://google.com/")
yield
4 changes: 4 additions & 0 deletions mesop/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def generate_data(ui_request: pb.UiRequest) -> Generator[str, None, None]:
)
for command in runtime().context().commands():
if command.HasField("navigate"):
if command.navigate.url.startswith(("http://", "https://")):
yield from render_loop(path=path)
yield STREAM_END
return
path = command.navigate.url
page_config = runtime().get_page_config(path=path)
if (
Expand Down
30 changes: 30 additions & 0 deletions mesop/tests/e2e/navigation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ test('navigation inside generator handler function', async ({page}) => {
// Check the text has changed which means navigation succeeded.
expect(await page.getByText('page2').textContent()).toEqual('page2');
});

test('navigation absolute - https', async ({page}) => {
await page.goto('/navigate_absolute');

// Trigger a navigation.
await page.getByRole('button', {name: 'navigate https', exact: true}).click();

// Wait for navigation to complete
await expect(page).toHaveURL('https://www.google.com/');
});

test('navigation absolute - http', async ({page}) => {
await page.goto('/navigate_absolute');

// Trigger a navigation.
await page.getByRole('button', {name: 'navigate http', exact: true}).click();

// Wait for navigation to complete
await expect(page).toHaveURL('http://example.com/');
});

test('navigation yield', async ({page}) => {
await page.goto('/navigate_absolute');

// Trigger a navigation.
await page.getByRole('button', {name: 'navigate yield', exact: true}).click();

// Wait for navigation to complete
await expect(page).toHaveURL('https://www.google.com/');
});
7 changes: 6 additions & 1 deletion mesop/web/src/shell/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export class Shell {
},
onCommand: (command) => {
if (command.hasNavigate()) {
this.router.navigateByUrl(command.getNavigate()!.getUrl()!);
const url = command.getNavigate()!.getUrl()!;
if (url.startsWith('http://') || url.startsWith('https://')) {
window.location.href = url;
} else {
this.router.navigateByUrl(command.getNavigate()!.getUrl()!);
}
} else if (command.hasScrollIntoView()) {
// Scroll into view
const key = command.getScrollIntoView()!.getKey();
Expand Down
Loading