Giter Club home page Giter Club logo

Comments (3)

TangleSpace avatar TangleSpace commented on August 14, 2024

It looks like you are not inheriting from the HydraHeadApp base class, therefore your login app doesn't have access to the session_state. Please see the example login app at https://github.com/TangleSpace/hydralit-example/blob/main/apps/login_app.py.

This is an example of your app not being able to direct as it isn't able to set the session value that is accessed via the base class HydraHeadApp,imported from from hydralit import HydraHeadApp.

from hydralit.

deeplearning987 avatar deeplearning987 commented on August 14, 2024

hello @TangleSpace we already inherited HydraHeadApp base class and also followed similar process but we are struck at

AttributeError: 'LoginApp' object has no attribute 'session_state'
Traceback:
File "C:\Users\anaconda3\lib\site-packages\streamlit\script_runner.py", line 350, in _run_script
exec(code, module.dict)
File "C:\Users\Downloads\hydralit-example-main - Copy\hydralit-example-main\apps\login_app.py", line 166, in
c.run()
File "C:\Users\Downloads\hydralit-example-main - Copy\hydralit-example-main\apps\login_app.py", line 103, in run
self._do_login(form_data, c2)
File "C:\Users\Downloads\hydralit-example-main - Copy\hydralit-example-main\apps\login_app.py", line 132, in _do_login
self.set_access(form_data['access_level'], form_data['username'])
File "C:\Users\anaconda3\lib\site-packages\hydralit\app_template.py", line 54, in set_access
self.session_state.allow_access = allow_access

class LoginApp(HydraHeadApp):
"""
This is an example login application to be used to secure access within a HydraApp streamlit application.

This application implementation uses the allow_access session variable and uses the do_redirect method if the login check is successful.

"""

def __init__(self, title = '', **kwargs):
    self.__dict__.update(kwargs)
    self.title = title


def run(self) -> None:
    """
    Application entry point.

    """

    st.markdown("<h1 style='text-align: center;'>Secure Login</h1>", unsafe_allow_html=True)

    
    
    c1,c2,c3 = st.columns([2,2,2])
    #c3.image("./resources/lock.png",width=100,)
    #c3.image("./resources/hydra.png",width=100,)
    
    form_data = self._create_login_form(c2)

    if form_data['submitted']:
        self._do_login(form_data, c2)


def _create_login_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['password'] = login_form.text_input('Password',type="password")
    form_state['access_level'] = login_form.selectbox('Example Access Level',(1,2))
    form_state['submitted'] = login_form.form_submit_button('Login')

    

    return form_state


def _do_login(self, form_data, msg_container) -> None:

    #access_level = login_data['access_level'] #Access denied!
    access_level = self._check_login(form_data)

    if access_level > 0:
        msg_container.success(f"✔️ Login success")
        with st.spinner("🤓 now redirecting to application...."):
            time.sleep(1)

            #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(form_data['access_level'], form_data['username'])

            #Do the kick to the home page
            self.do_redirect()
    else:
        self.session_state.allow_access = 0
        self.session_state.current_user = None

        msg_container.error(f"❌ Login unsuccessful, 😕 please check your username and password and try again.")


def _check_login(self, login_data) -> int:
    #this method returns a value indicating the success of verifying the login details provided and the permission level, 1 for default access, 0 no access etc.
    #view_all_users()
            #user_result = view_all_users()
            #clean_db = pd.DataFrame(user_result,columns=["Username","Password"])
            #st.dataframe(clean_db)
            
            db2postgres=Db2Postgres()
            pwd=login_data['password']
            hashed_pswd = db2postgres.make_hashes(pwd)
            if (db2postgres.make_hashes(pwd) == hashed_pswd):
                result = db2postgres.login_user(login_data['username'],hashed_pswd)
                return 1
            else:
                return 0
            '''
            if login_data['username'] == 'joe' and login_data['password'] == 'joe':
                return 1
            else:
                return 0
            '''   

if name == 'main':
c=LoginApp()
c.run()

from hydralit.

TangleSpace avatar TangleSpace commented on August 14, 2024

You can't just run the login app on it's own, it has to be a child of a HydraApp, look at the example again, line 37 , the login app is added to the parent. Also, you have to have another app to actually be kicked to. I suggest you study the example app again, as this is not how Hydralit works, if you want a single page application, you don't need Hydralit. https://github.com/TangleSpace/hydralit-example

from hydralit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.