Skip to content
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

allow external link for redirect #1902

Merged
merged 2 commits into from
Oct 2, 2023
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
7 changes: 5 additions & 2 deletions reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export const getAllLocalStorageItems = () => {
export const applyEvent = async (event, socket) => {
// Handle special events
if (event.name == "_redirect") {
Router.push(event.payload.path);
if (event.payload.external)
window.open(event.payload.path, "_blank");
else
Router.push(event.payload.path);
return false;
}

Expand Down Expand Up @@ -185,7 +188,7 @@ export const applyEvent = async (event, socket) => {
if (event.name == "_call_script") {
try {
eval(event.payload.javascript_code);
} catch(e) {
} catch (e) {
console.log("_call_script", e);
}
return false;
Expand Down
7 changes: 5 additions & 2 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,19 @@ def fn():
)


def redirect(path: str | Var[str]) -> EventSpec:
def redirect(path: str | Var[str], external: Optional[bool] = False) -> EventSpec:
"""Redirect to a new path.

Args:
path: The path to redirect to.
external: Whether to open in new tab or not.

Returns:
An event to redirect to the path.
"""
return server_side("_redirect", get_fn_signature(redirect), path=path)
return server_side(
"_redirect", get_fn_signature(redirect), path=path, external=external
)


def console_log(message: str | Var[str]) -> EventSpec:
Expand Down
38 changes: 30 additions & 8 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,38 @@ def test_fn_with_args(_, arg1, arg2):
assert event.payload == {"arg1": arg1, "arg2": arg2}


def test_event_redirect():
"""Test the event redirect function."""
spec = event.redirect("/path")
@pytest.mark.parametrize(
"input,output",
[
(("/path", None), 'Event("_redirect", {path:"/path",external:false})'),
(("/path", True), 'Event("_redirect", {path:"/path",external:true})'),
(("/path", False), 'Event("_redirect", {path:"/path",external:false})'),
(
(Var.create_safe("path"), None),
'Event("_redirect", {path:path,external:false})',
),
],
)
def test_event_redirect(input, output):
"""Test the event redirect function.

Args:
input: The input for running the test.
output: The expected output to validate the test.
"""
path, external = input
if external is None:
spec = event.redirect(path)
else:
spec = event.redirect(path, external=external)
assert isinstance(spec, EventSpec)
assert spec.handler.fn.__qualname__ == "_redirect"
assert spec.args[0][0].equals(Var.create_safe("path"))
assert spec.args[0][1].equals(Var.create_safe("/path"))
assert format.format_event(spec) == 'Event("_redirect", {path:"/path"})'
spec = event.redirect(Var.create_safe("path"))
assert format.format_event(spec) == 'Event("_redirect", {path:path})'

# this asserts need comment about what it's testing (they fail with Var as input)
# assert spec.args[0][0].equals(Var.create_safe("path"))
# assert spec.args[0][1].equals(Var.create_safe("/path"))

assert format.format_event(spec) == output


def test_event_console_log():
Expand Down
Loading