Skip to content

Commit

Permalink
allow external link for redirect (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor authored Oct 2, 2023
1 parent 418f9ad commit 7df3f2f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
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

0 comments on commit 7df3f2f

Please sign in to comment.