Ter is a simple CLI framework for Python App
Project description
Ter CLI Framework
Ter is the CLI Framework that will build a CLI application with cool syntax which will amaze your school coding projects.
In Ter, we are strongly amaze the syntax and terminologies of other modern UI framework. Our main purpose of creating this framework is to make the terminal app more interactive and fancy with pages, command prompt, and built-in UI.
Main concept of Ter
Everything that you can see on the terminal screen is the result of print statement in Python. Then the screen is paused to wait for the user command by input statement. We consider everytime that printing out and waiting for input for future change is a state (inspired by React).
Each state includes:
clear screen -> render component -> pause program -> run logic
The state will continuously loop until the exit method is called.
- Clear screen phase: Create your terminal and ready to print new content.
- Render component phase: Render your content with new data. (State is changed)
- Pause programm phase: Pause program to listen input from user.
- Run logic phase: Execute logic based on user input to change the data.
Quick start
- Install
terappin Python package.
$ pip install terapp
- Create a simple
Terapp
# Import libs
import terapp
# Create app instance
app = terapp.Ter()
# Define Screen
class MyScreen(ter.Component):
def __init__(self):
self.count = 0
# Override render method
def render(self, context):
print("Counting app")
print(self.count) # Print count value
# Override prompt method
def prompt(self):
return "Enter your number: "
# Override logic method
def logic(self, command, context):
# Add user number to count
self.count += int(command)
# Add Screen to route. Ter will get the first route as init screen
app.register_routes({
'home': MyScreen
})
# Begin loop of your app
app.run()
Folder Structure
Ter is strongly recommending uses organize app's files. By using this structure, your app is more scalable and easy to maintain in the future.
your_folder
└ screens
| └ your_screen.py
└ state
| └ your_state.py
└ main.py
- screens folder contains all your screen of your app.
- state folder contains all global state of your app. (Inspired by Redux)
- main.py contains the
appinstance and some config for your app.
Ter App
Ter manages every things in the app instance. Include all the screen in your app.
from terapp import Ter
app = Ter()
Ter provides the Config class which includes all the custom config of app. Ter config includes:
default_pause_message(str): If you don't override thepromptmethod, this string will be displayed instead.
from terapp import Ter, Config
app = Ter(
config=Config(default_pause_message="Press enter to continue...")
)
# Or you can provide config by config method
app.config(
config=Config(default_pause_message="Press enter to continue...")
)
Component
In ter, we define every screen is a component which always have methods:
render(required): This method will run first and do all code inside it before program will be pause.prompt(optional): This method must return a string for a user prompt message. If not, the prompt message will usedefault_pause_messageinstead.logic(optional): This method will run after the user complete typing the prompt to run your desired logic before the new loop.
from terapp import Component
class YourScreen(Component):
def __init__(self):
# Define your local variable here
self.local_variable = ...
# Render method
def render(self, context):
# Print something
...
# Prompt method, optional
def prompt(self):
return "Some message"
# Logic method, optional
def logic(self, command, context):
# Do somthing with use input at command params
...
To add your screen into app, register routes in main.py file
# Import YourScreen class from screens folder
from screens import YourScreen
# The register_routes method requires a dictionary of Components
app.register_routes({
'identify_of_route': YourScreen
})
Navigation
Consider we have 2 screens as below in main.py file.
# Import Screens class from screens folder
from screens import Screen1, Screen2
# The register_routes method requires a dictionary of Components
app.register_routes({
's1': Screen1
's2': Screen2
})
We want to navigate from Screen1 to Screen2 at logic method of Screen1
class Screen1:
...
def logic(self, command, context):
# Navigate to Screen 2 if user type `2`
if command == '2':
# Use navigate method of Context and passing the key of Screen
context.navigate('s2')
Global State
The term of state management is strongly inspired by Redux library in React framework. It stores the value of and be accessing in diffirent Screens. Unlike the local varable that we define at the __init__ method of a Screen which only accessed inside the class, this state can be store, update anywhere in your app.
Create your state at state/your_state.py file.
from terapp import State
# Initialize store instance
store = State(init_state=0)
# Create a name for an event and this action
store.reducers(
reducer='increase_count',
action=lambda state, payload: state += payload
)
Import the store instance into the Screen
from state.your_state import store
class Screen:
...
def logic(self, command, context):
# Increase the count value
# Use dispatch method, access by the event name and passing new data
store.dispatch(
reducer='increase_count',
payload=int(command)
)
The init_state can be a dictionary or anything
store = State(init_state={
'name': "Ming Doan",
'age': 20
})
store.reducers(
reducer='change_name',
action=lambda state, payload: {
'name': payload # Update name
}
)
High level UI
Ter is providing some quick UI in terminal app.
- Header
from terapp import Header
class Screen:
...
def render(self, contex):
print(Header(context, "This is header"))
Future development
Ter was only the alpha version, the future update will arrive soon. (Depend on Author 😁🙌)
Author & Maintainer Ming Doan
- Email: quangminh57dng@gmail.com
- Github: https://github.com/Ming-doan
- Facebook: https://www.facebook.com/ming.doan/
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 terapp-0.1.0.tar.gz.
File metadata
- Download URL: terapp-0.1.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d061231103eecb3622d7c533c345473b39857b2c17ea9bac794ce72c13b234ce
|
|
| MD5 |
15a82d9456f374c60b0a11f90e956404
|
|
| BLAKE2b-256 |
c01cd44436d3c400de72285d338f41c7862c4eadfdced8944cd07bf30ff41584
|
File details
Details for the file terapp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: terapp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65478060404fc2cc95416975c288a716da94f50a5d987d474507f0299c341bf
|
|
| MD5 |
055ed623f73d3a022b7e3dc09f2afc7e
|
|
| BLAKE2b-256 |
35665e57f703218a18aa010b845dd638e7e4201ee11e7ce1e2e0676e677df476
|