JustPy Redirect won't redirect

104 Views Asked by At

I feel like I'm doing something rediculously stupid here, but I've been banging my head against the wall all day, and don't seem to be making any progress! I have this function:

def init_create_reports(self, msg):
    if not common.create_report_in_progress and len(common.tracks_selected_for_report) > 0:
        common.create_report_in_progress = True
        return jp.redirect('/yeeha')

That is called when the user clicks a button. Stepping through it, everything works fine until the redirect function, which seems to fire from within justPy, but then I get nothing. It never redirects to the '/yeeha' route. It seems simple enough according to the godawful justPy documentation, but agaghhhhh!

I've also tried passing the redirect the current webpage: wp.redirect('/yeeha') with the same outcome. Honestly all I need is to set that variable and navigate to another page, and at this point I'm prepared to just do it from a tag.

1

There are 1 best solutions below

0
On

It seems that there is currently a problem in Justpy with the handling of the event_result of an event callback function. I opened an issue for this problem. See https://github.com/justpy-org/justpy/issues/657

Alternatively to jp.redirect() you can also set the redirect attribute of the webpage.

In your example it would be:

def init_create_reports(self, msg):
    if not common.create_report_in_progress and len(common.tracks_selected_for_report) > 0:
        common.create_report_in_progress = True
        msg.page.redirect = '/yeeha'

Here is a complete example showing a redirect from one justpy webpage to another route:

import justpy as jp


def hello_function() -> jp.WebPage:
    wp = jp.WebPage()
    jp.P(a=wp, text='Hello there!', classes='text-5xl m-2')
    jp.Button(
            a=wp,
            text="redirect",
            on_click=handle_click,
            classes="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full"
    )
    return wp


def handle_click(self, msg):
    """
    handle redirect button click
    Args:
        self: justpy component that triggered the event
        msg: event message
    """
    wp = msg.page
    wp.redirect = "/bye"


def bye_function() -> jp.WebPage:
    wp = jp.WebPage()
    wp.add(jp.P(text='Goodbye!', classes='text-5xl m-2'))
    return wp


jp.Route('/bye', bye_function)

jp.justpy(hello_function)

See also Login Example