React.js-like framework with components for native user interfaces
Project description
turbosnake - a react.js-like framework for ui in python
Motivation
There are lots of nice graphical applications written in python. But code that creates UI (at least in simple examples I could find and in few projects I tried to contribute to) looks like some sort of imperative mess. And it gets even uglier when UI should be changed dynamically. So, I decided to try to reproduce the declarative approach which I am used to as a professional web-developer.
Syntax
Unlike JavaScript frameworks with JSX, we cannot just modify Python's syntax for our purposes. Instead, turbosnake uses unmodified Python's syntax to represent its concepts.
Instantiating components
Instead of JSX's <Component ... /> turbosnake uses a plain function call:
from turbosnake import Component
...
Component(...)
In most cases, return value of such calls is useless. The main effect of such call is a component being appended to a list in specific runtime context. Calls of components outside of proper runtime context are prohibited and will cause error.
Nesting components
Children are added to a component using with operator:
with Component1(...):
Component11(...)
Component12(...)
this fragment of code is kinda equivalent to the following JSX:
<Component1 ...>
<Component11 .../>
<Component12 .../>
</Component1>
but unlike it, any loops and conditions are allowed within body of with operator.
Components that support nesting implement context provider interface.
When used in with operator they substitute previously mentioned runtime context with the one that adds all created components to list of their children.
Functional components
The easiest way to compose few components is to create a functional component. In some simple cases (component doesn't use hooks or is always rendered constant number of times within it's parent) a plain function can serve as a functional component:
def foo(**props):
with Component1():
Component2(**props)
...
foo(...)
But a function can be converted into a full-fledged component with its own state using functional_component decorator:
from turbosnake import functional_component
...
@functional_component
def foo(**props):
with Component1():
Component2(**props)
...
foo(...)
Depending on function signature and settings passed to the decorator, it may add (or don't add) support for nesting and hooks.
Hooks
Hooks are supported in functional components (if not disabled explicitly) and in components inheriting ComponentWithHooks.
Some of implemented hooks are: use_toggle, use_state, use_memo, use_effect, use_callback, use_previous, use_ref, use_callback_proxy, use_self.
Hooks that accept a function can be used as decorators on a local function:
from turbosnake import functional_component, use_callback
...
@functional_component
def foo():
...
@use_callback([...])
def callback():
...
...
UI
Core of turbosnake isn't bound to any UI library or framework. With some effort applied, it can be used with any UI library or even for purposes different from user interface rendering.
Package turbosnake.ttk provides adapters for tkinter (mostly ttk) UI components.
For examples see TODO-list application example.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file turbosnake-0.1.324a6.tar.gz.
File metadata
- Download URL: turbosnake-0.1.324a6.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1a48ecfbd54ecb74d539df1748056e90233944881fd534a2a93291085ac54b2
|
|
| MD5 |
7c5654fb36047d4e3ed3a79ea8662e9e
|
|
| BLAKE2b-256 |
50e74966a13753397e57739f36f33411c283d207d2267bcd8b8abb201f42e191
|
File details
Details for the file turbosnake-0.1.324a6-py3-none-any.whl.
File metadata
- Download URL: turbosnake-0.1.324a6-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c73ab610f53d261e18894b05047ab73a4092296ad02a933b7717e31ee9f6a239
|
|
| MD5 |
a4c225e62b7d9b37f2ca039b303add5c
|
|
| BLAKE2b-256 |
a05e9e0fed6470ab586817959f6b43a14acd23665541972d7f49d1ce3d657782
|