figoyouwei / taipy_success

This is a sample code that tests the deployment on heroku
2 stars 2 forks source link

Auth Sample Code #20

Closed figoyouwei closed 1 month ago

figoyouwei commented 2 months ago

Hey boy,

Could you give a simple script that contains some text and a table, with Alex and Florian as two users and only Alex can see both text and table, Florian only sees the text.

So I could start with your code.

AlexandreSajus commented 2 months ago

Sure! Here is a piece of code. When you login with "florian" "password", you'll see the table. If you login with "Alexandre" "password" you don't see the table:

import os
import pandas as pd

from taipy.gui import Gui, State, navigate, notify
import taipy.gui.builder as tgb
from taipy.config import Config
from taipy.auth import hash_taipy_password, AnyOf, Credentials
import taipy.enterprise.gui as tp_enterprise

os.environ["TAIPY_AUTH_HASH"] = "taipy"

username = "Login"

credentials = Credentials(user_name=username, roles=[])

passwords = {
    "florian": hash_taipy_password("password"),
    "alexandre": hash_taipy_password("password"),
}

roles = {
    "florian": ["admin", "TAIPY_ADMIN"],
    "alexandre": ["TAIPY_READER"],
}

Config.configure_authentication("taipy", passwords=passwords, roles=roles)

is_admin = AnyOf("admin", True, False)

def on_login(state: State, id, login_args):
    state.username, password = login_args["args"][:2]
    try:
        state.credentials = tp_enterprise.login(state, state.username, password)
        notify(state, "success", f"Logged in as {state.username}...")
        navigate(state, "Overview", force=True)
    except Exception as e:
        notify(state, "error", f"Login failed: {e}")
        print(f"Login exception: {e}")
        navigate(state, "Login", force=True)

def go_to_login(state: State):
    navigate(state, "Login", force=True)

root_page = ""

data = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

with tgb.Page() as root_page:
    tgb.button("Account", on_action=go_to_login, class_name="login_button plain")

with tgb.Page() as login_page:
    tgb.login("Welcome to Taipy!")

with tgb.Page() as Overview:
    tgb.text(value="# Hello", mode="md")
    with tgb.part(render="{is_admin.get_traits(credentials)}"):
        tgb.table(data="{data}")

pages = {
    "/": root_page,
    "Login": login_page,
    "Overview": Overview,
}

if __name__ == "__main__":
    Gui(pages=pages).run(title="Dynamic chart")
figoyouwei commented 1 month ago

I will try this tomorrow. Now when I install enterprise, shall I install taipy-enterprise-4.0 link? I'd like to keep it consistent.

If taipy-enterprise-4.0 > taipy 4.0 and support chat and everything like auth. Then I will go with taipy-enterprise-4.0 for my production.

AlexandreSajus commented 1 month ago

Yes, Taipy Enterprise 4.0 has the features of taipy 4.0. When using the link, don't forget to modify the version in the link with 4.0 instead of 3.1 like: pip install https://taipyeu002.blob.core.windows.net/taipy/taipy-enterprise-4.0.tar.gz...

figoyouwei commented 1 month ago

And how about we meet tonight at 20:00 Shanghai Time?

AlexandreSajus commented 1 month ago

I'm not available today. How about tomorrow at 20:00 Shanghai time?

figoyouwei commented 1 month ago

Sure, let's do it tomorrow, I close this issue now as the auth code is working good, I will open new one if I encounter more advanced issues.