Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add global context for session #2934

Merged
merged 3 commits into from
Apr 3, 2024

Conversation

50Bytes-dev
Copy link
Contributor

@50Bytes-dev 50Bytes-dev commented Mar 23, 2024

I was inspired by streamlit library, it has session_state, which is very convenient for me. I made something similar.

# main.py
import flet as ft
from .utils import test_global_context, test_global_context_async

async def main(page: ft.Page):
    print(ft.context.page.session_id)
    test_global_context()
    page.run_thread(test_global_context)
    page.run_task(test_global_context_async)

# utils.py
import flet as ft

async def test_global_context_async():
    print("Work async!", ft.context.page.session_id)

def test_global_context():
    print("Work!", ft.context.page.session_id)

@FeodorFitsner
Copy link
Contributor

Thanks for your PR!

I've been wondering if your solution is the same as https://flet.dev/docs/guides/python/session-storage/ ?

@50Bytes-dev
Copy link
Contributor Author

Thanks for your PR!

I've been wondering if your solution is the same as https://flet.dev/docs/guides/python/session-storage/ ?

No. My solution allows you to access the session without accessing the page variable. I solved the problem when I had to throw the page variable everywhere to access the session. Now you just need to import flet as ft and access ft.context.page from any file in the application.

@50Bytes-dev
Copy link
Contributor Author

50Bytes-dev commented Mar 23, 2024

For example, my application uses the langchain library, which has a huge pile of tools and now instead of throwing a page variable into each tool I can just import flet and access flet.context.page from any tool.

@FeodorFitsner
Copy link
Contributor

OK, interesting. Never used context vars before - time to learn! :)

@FeodorFitsner FeodorFitsner merged commit 9081df5 into flet-dev:main Apr 3, 2024
2 checks passed
@50Bytes-dev 50Bytes-dev deleted the global_context branch April 3, 2024 19:33
@ndonkoHenri
Copy link
Contributor

@50Bytes-dev, will you mind documenting this addition somewhere in the flet-docs? :)

@50Bytes-dev
Copy link
Contributor Author

@50Bytes-dev, will you mind documenting this addition somewhere in the flet-docs? :)

I have no idea how best to do it and in what location. It would be easier for you to do it

@shenzifanhy
Copy link

🆒

50Bytes-dev added a commit to 50Bytes-dev/flet that referenced this pull request May 18, 2024
* add global context for session

* export context
zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* add global context for session

* export context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants