Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alvinwan/clooey
Python command line interface (CLI) to an HTML form
https://github.com/alvinwan/clooey
Last synced: 3 months ago
JSON representation
Python command line interface (CLI) to an HTML form
- Host: GitHub
- URL: https://github.com/alvinwan/clooey
- Owner: alvinwan
- License: mit
- Created: 2024-03-28T06:23:38.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-06-19T08:06:05.000Z (7 months ago)
- Last Synced: 2024-09-20T00:50:29.589Z (3 months ago)
- Language: Python
- Homepage: https://alvinwan.com/clooey/js/
- Size: 25.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# clooey
Python Command Line Interface (CLI) to an HTML form.
> For a Javascript API that does not depend on a Python runtime to
> *generate* the form, see the `js/` directory in this repository.## Getting Started
Start by installing clooey using pip.
```bash
pip install clooey
```Here's a sample Python script that has `input` calls.
```python
name = input("Enter your name: ")
city = input("Enter your city: ")print(f"Welcome to {city}, {name}!")
```clooey can convert this Python script into a form, based on its calls to
`input`.```python
import clooeyTEMPLATE_FORM = """
{% for input in cli.inputs %}
{{ input.label }}
{% endfor %}
"""
cli = clooey.parse('sample.py')
html = clooey.generate(cli, TEMPLATE_FORM)
print(html)
```Running the above conversion script gives the following HTML output.
```html
Enter your name:
Enter your city:
```
# Advanced
There are several more features that clooey supports:
- Each prompt can contain a `[placeholder]` value, delimited by square brackets,
that will be used as the placeholder text in the form.
- You can provide a title and description for the form. The title is the first
non-whitespace line of the docstring, and the description is the rest of the
docstring.Here's a sample Python script that has `input` calls.
```python
"""Tell me about youA simple hello world application to greet you!
"""name = input("Enter your name: ")
age = int(input("Enter your age: "))
city = input("Enter your city [Seattle]: ") or 'Seattle'print(f"Welcome to {city}, {name} ({age})!")
```clooey can convert this Python script into a form, based on its calls to
`input`.```python
import clooeyTEMPLATE_FORM = """
{% if cli.title %}{{ cli.title }}
{% endif %}
{% if cli.description %}{{ cli.description }}
{% endif %}{% for input in cli.inputs %}
{{ input.label }}
{% endfor %}
"""
cli = clooey.parse('sample.py')
html = clooey.generate(cli, TEMPLATE_FORM)
print(html)
```Notice that the HTML template is
[Jinja2](https://jinja.palletsprojects.com/en/3.1.x/) formatted. You can
customize this HTML as you see fit, as long as the there are input fields
with the names `1`, `2`, `3`, etc. The above script gives us this output.```html
Tell me about you
A simple hello world application to greet you!
Enter your name:
Enter your age:
Enter your city:
```
If you save this HTML in a file and open the file in a browser, you'll then see
the following:# Web Demo
See an actual web form by running the following command:
```bash
clooey web
```From left to right, you'll see the launched Flask app with the following form,
fill out the form as you would the CLI, then submit the form to see the output.
You can optionally provide your own script to parse. For example,
```bash
wget https://github.com/alvinwan/clooey/blob/main/clooey/examples/password.py
clooey web --program password.py
```## CLI Demo
Run the CLI.
```bash
clooey cli
```You'll then see the following output
```html
Enter your name:
Enter your age:
Enter your city:
```
Next, the script will prompt you for input, as though you were filling out the
web form.```
Enter your name: Alvin
Enter your age: 1000
Enter your city: Seattle
```Finally, the script will execute the Python script with the input values.
```
Welcome to Seattle, Alvin (1000)!
```You can optionally provide your own script to parse. For example,
```bash
wget https://github.com/alvinwan/clooey/blob/main/clooey/examples/piglatin.py
clooey cli --program piglatin.py
```