TangleSpace / hydralit

A library to create multi-page Streamlit applications with ease.
Apache License 2.0
202 stars 19 forks source link

Redirect from Signup to loginapp #12

Closed deeplearning987 closed 3 years ago

deeplearning987 commented 3 years ago

Hello @TangleSpace

I've made changes to your code. I just included the database connectivity to save the username and password.

I'm trying to redirect from Signup App to Login App.

I've read in hydralit(app_template) that if nothing is mentioned in self.do_redirect() present in def _do_signup(self, form_data, msg_container) -> None: then it will be redirected to homepage by default.

But I've mentioned (apps.login_app) in self.do_redirect(apps.login_app) which is in init.py file. I've also mentioned login_app in self.do_redirect(login_app) but it is still redirecting to home page.

hydralit(app_template)

def do_redirect(self,redirect_target_app=None): """ Used to redirect to another app within the parent application. If the redirect_target is a valid url, a new window will open and the browser will set focus on the new window while leaving this app in it's current state.

    Parameters
    ------------
    redirect_target_app: str, None
        The name of the target app or a valid url, this must be the registered name when the app was added to the parent. If no target is provided, it will redirect to the HydraApp home app. If the redirect_target is a valid url, a new window will open and the browser will set focus on the new window while leaving this app in it's current state.

Signup app code:

def _create_signup_form(self, parent_container) -> Dict:

    login_form = parent_container.form(key="login_form")

    form_state = {}
    form_state['username'] = login_form.text_input('Username')
    form_state['fullname'] = login_form.text_input('Fullname')
    form_state['password'] = login_form.text_input('Password',type="password")
    form_state['password2'] = login_form.text_input('Confirm Password',type="password")
    form_state['access_level'] = login_form.selectbox('Example Access Level',(1,2))
    form_state['submitted'] = login_form.form_submit_button('Sign Up')

    if parent_container.button('Login',key='loginbtn'):
        # set access level to a negative number to allow a kick to the unsecure_app set in the parent
        self.set_access(0, None)

        #Do the kick to the signup app
        self.do_redirect('LoginApp')

    return form_state

def _do_signup(self, form_data, msg_container) -> None:
    if form_data['submitted'] and (form_data['password'] != form_data['password2']):
        st.error('Passwords do not match, please try again.')
    else:
        with st.spinner("🤓 now redirecting to login...."):
            self._save_signup(form_data)
            time.sleep(2)

            #access control uses an int value to allow for levels of permission that can be set for each user, this can then be checked within each app seperately.
            self.set_access(0, None)
            self.set_access(form_data['access_level'], form_data['username'])
            #Do the kick back to the login screen
            self.do_redirect("apps.login_app")

Can you please guide me.

deeplearning987 commented 3 years ago

hello @TangleSpace can you please suggest me what are the changes to be made?

TangleSpace commented 3 years ago

look at the example code, that EXACT same code is what the live demo is running, it works perfectly. If you can't follow the code, I'm not here to teach you to program.