๐Ÿš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more โ†’
Socket
Sign inDemoInstall
Socket

Neostate

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Neostate

Welcome to Neostate! ๐ŸŒˆ A lightweight and intuitive library for managing shared states in Flet applications. With StateCraft, you can bind widgets to a shared state effortlessly, enabling seamless updates across your UI components with minimal boilerplate.

0.1.5
PyPI
Maintainers
1

๐Ÿ”ฅNeostate ๐ŸŽจโœจ

githubpypi

image image uv linting - Ruff Downloads

Neostate is a package designed to simplify and optimize state management in Flet apps. It provides a clean and easy way to handle global and scoped states, allowing you to build apps with a simpler and more powerful state management system.

Elegant Routing ,State Management & More for Flet Applications

Welcome to Neostate! ๐ŸŒˆ A lightweight and intuitive library for managing shared states in Flet applications,Use routing with Ease and much more, With Neostate, you can bind widgets to a shared state effortlessly, enabling seamless updates across your UI components with minimal boilerplate.

๐Ÿ’ก Installation

Install the package from PyPI:

pip install Neostate

๐Ÿ”„ Shared State ๐Ÿ”„

๐Ÿ›  StateNotifier Class

What is it?
Think of StateNotifier like a magic box where we store important information, like a score in a game. When the score changes, it shouts out to everyone that it's been updated!

How it works:

  • We create a StateNotifier with a starting score.
  • If the score changes, StateNotifier tells everyone whoโ€™s listening, like other parts of your app, to update and show the new score!

Fun part ๐ŸŽ‰: You can add or remove listeners, like telling certain parts of the app to pay attention when the score changes.

๐Ÿ”ง Features

  • ๐Ÿ”„ Reactive State Management: Automatically update UI components when the state changes.
  • ๐Ÿ’ช Simple Widget Binding: Use the Shared class to bind Flet widgets dynamically to shared states.
  • ๐Ÿ”ง Formatter Support: Customize how state values are displayed with flexible formatting strings.
  • ๐ŸŒ Complex Widget Attributes: Update attributes like content, value, or controls dynamically.
  • โณ Detachable Listeners: Add or remove widgets from state listeners as needed.
  • ๐Ÿš€ Inline State Updates: Use intuitive operations like shared_state.value += 1.
  • ๐Ÿ““ Easy to Learn and Use: Minimal learning curve with a clean, developer-friendly

๐Ÿ”— Shared Class

What is it?
Imagine you have a cool widget (like a score display) and you want it to automatically change when the score updates. Shared makes this happen!

How it works:

  • Shared links the score widget to the StateNotifier (the magic box from above).
  • When the score changes, the widget instantly updates itself to show the new score. No need to do anything manually!

Whatโ€™s special ๐Ÿ’ซ:

  • If you have a special formatting (like showing the score as โ€œScore: 10โ€), you can tell Shared to format it for you.
  • It will always keep the widget in sync with the score!

๐Ÿง‘โ€๐Ÿซ How They Work Together:

  • StateNotifier keeps the score (or any value) safe.
  • Shared makes sure your widgets (like score displays) stay updated with the new value whenever it changes!

Itโ€™s like a team where one member holds the information (StateNotifier) and the other makes sure everything looks correct and up-to-date (Shared).

And thatโ€™s how you make your app super interactive! ๐ŸŽฎ

Let me know if you'd like more details or adjustments! ๐Ÿ˜Š๐Ÿ“š

Explore Documentation

Routing Modes ๐Ÿšฅ

Basic Routing ๐Ÿšถโ€โ™‚๏ธ

Basic routing allows you to manage navigation between pages in a simple way, using predefined methods like swap, reach, back, and refresh.

How it works:

here function means : the function which return ui like return [ft.Container(height=100) ]

  • swap(route, function): This method adds a new page to the views stack and navigates to it. It appends the new view without removing the existing ones.
  • reach(route, function): This method replaces the entire view stack with a new page.
  • back(): This method allows you to go back to the previous view.
  • refresh(): This method refreshes the current view.

To activate Basic Routing, you just need to call the app with advanced_routing=False:

enhanced_app(target, advanced_routing=False)

Example:

def home_page(page):
    return Column([Text("Welcome to Home!")])

def about_page(page):
    return Column([Text("About Us")])

# Basic Routing with swap and reach methods
page.swap("/home", home_page)
page.reach("/about", about_page)

Advanced Routing ๐Ÿš€

Advanced routing gives you more control by using RoutingConfig to register routes, middlewares, and error pages. This mode is ideal for complex applications where you need more flexibility.

How it works:
  • RoutingConfig.register_route: Register routes with optional guards, custom redirects, and custom error pages.
  • apply_middlewares: Apply middleware functions to routes for conditions like authentication.
  • Global error pages: Define custom error pages for "404", "not allowed", etc.

To activate Advanced Routing, you call the app with advanced_routing=True:

enhanced_app(target, advanced_routing=True)

Example:

page.register_route("/home", home_page)
page.register_route("/about", about_page)
page.set_global_error_page("404", page_not_found)
page.register_middleware(auth_middleware)

Switch Between Routing Modes ๐Ÿ”„

You can easily switch between Basic and Advanced routing modes:

  • For Basic Routing, set advanced_routing=False:

    enhanced_app(target, advanced_routing=False)
    
  • For Advanced Routing, set advanced_routing=True:

    enhanced_app(target, advanced_routing=True)
    

When to Choose Which? ๐Ÿค”

๐Ÿš— Basic Routing: Best for mobile and desktop apps, providing simple and fast navigation.
๐ŸŽ๏ธ Advanced Routing: Perfect for websites and web apps, supporting complex setups with middleware and role-based access.

Extras

Hereโ€™s the updated changelog with the emojis added back in:

  • ๐Ÿ› ๏ธ Neostate App Creation Command
    Creating a new app template is now easier than ever! Use the neostate create appname command in the terminal to generate your app structure. Here's the generated directory structure:

    <appname>/
    โ”œโ”€โ”€ assets/
    โ”‚   โ”œโ”€โ”€ fonts/
    โ”‚   โ””โ”€โ”€ images/
    โ”œโ”€โ”€ App/
    โ”‚   โ”œโ”€โ”€ components/
    โ”‚   โ”œโ”€โ”€ pages/
    โ”‚   โ”œโ”€โ”€ services/
    โ”‚   โ”œโ”€โ”€ state/
    โ”‚   โ”œโ”€โ”€ utils/
    โ”‚   โ””โ”€โ”€ main.py
    โ”œโ”€โ”€ main.py
    โ”œโ”€โ”€ requirements.txt
    โ””โ”€โ”€ README.md
    

Just Use this command inside terminal/cmd in empty folder

Neostate create <appname> 

<appname> can be anything like firstapp

  • ๐ŸŽฏ Center Widget Support
    Added a Center widget to simplify centering any widget. Whether it's a Column, Row, or Container simply wrap it with Center(ft.column([])) to achieve perfect centering. The force and expand arguments are optional.
#usage
from Neostate Import Center
Center(ft.Column([])  #or can be any widget

)

it has two optional argument force and expand , True or False can be passed , test it out yourself

Enjoy these new features to enhance your app development workflow!

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts