A react type module for nice visuals in the console
Project description
PythonReact
Did you ever think: "We need a JS library turned into a python module used for console output in stead of rendering a webpage."? No? Well I did. So here it is.
Installation
This package is on pypi.
Installing can be done via pip
Windows:
pip install pythonreact
Mac/ Linux:
pip3 install pythonreact
Usage
If you are familliar with the JS library react this should be very easy.
Component
Everything is a component.
To create a new component, make a class that inherits from PythonReact.Component
import PythonReact
class MyComponent(PythonReact.Component):
pass
Hello python react
To show something in the console we will add a render function. This render function should return a list of printable items or other Components.
import PythonReact
class MyComponent(PythonReact.Component):
def render(self) -> list:
return [
"Hello",
"Python React"
]
Let's make an object of that type:
# snip
my_comp = MyComponent()
It will automatically start printing now. And it will render your function.
State
If you have used React you know about state.
To create a state add it to the __init__
of your component.
And then simply call the variables in your render function.
import PythonReact
class MyComponent(PythonReact.Component):
def __init__(self):
self.state = {
"name": "Donkere",
}
super().__init__()
def render(self) -> list:
return [
f"Hello {self.state['name']}!",
]
my_comp = MyComponent()
>>>
Hello Donkere!
Set state
Just as in React there is a set state function.
import PythonReact
class MyComponent(PythonReact.Component):
def __init__(self):
self.state = {
"name": "Donkere",
}
super().__init__()
def render(self) -> list:
return [
f"Hello {self.state['name']}!",
]
my_comp = MyComponent()
my_comp.set_state({"name": "New Name"})
>>>
Hello Donkere!
# Screen get's cleared
Hello New Name!
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
Hashes for pythonreact-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7806b7eaa7d7111bb2073635b6071f1bd2faf5e0179dd01619e333a942bbdf38 |
|
MD5 | 30485b85a9886939544341d7e5850dbb |
|
BLAKE2b-256 | 4ac452020f7043f3c4bea5981497adbd066357353d732a89309ce05291bce83e |