Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 clooey

TEMPLATE_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 you

A 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 clooey

TEMPLATE_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:

Screenshot 2024-06-19 at 12 42 36 AM

# 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.

Screenshot 2024-06-10 at 9 40 48 PM
Screenshot 2024-06-10 at 9 41 58 PM
Screenshot 2024-06-10 at 9 41 09 PM

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
```