|
1 | | -# Python Web I/O |
| 1 | +# <img src="./.github/logo.png" style="width:40px;padding-right:10px;margin-bottom:-8px;" alt="Sticker of a cute yellow Python snake, representing the use of the Python programming language in this project."> Python Web I/O |
2 | 2 | Generate a webpage as a GUI for a Python script, and serve from anywhere. |
3 | 3 |
|
4 | | -## Usage |
5 | | -``` |
6 | | -$ export SECRET_KEY="someSecureSecretKey" |
7 | | -$ python_web_io .\example.py |
8 | | -``` |
9 | | -* Create a `.envrc` file, setting `SECRET_KEY` as per [`python_web_io/.envrc.example`](https://github.com/Cutwell/python-web-io/blob/main/python_web_io/.envrc.example). |
10 | | -* Try running the [`example.py`](https://github.com/Cutwell/python-web-io/blob/main/python_web_io/example.py) script using `python_web_io example.py`. |
| 4 | +## Documentation |
| 5 | +Check out the [wiki](https://github.com/Cutwell/python-web-io/wiki). |
11 | 6 |
|
12 | | -|Argument||| |
13 | | -|:---:|:---:|:---:| |
14 | | -|`"example.py"`|Required|Specify the file path for the app Python script / entrypoint.| |
15 | | -|`--debug`|Optional|Run the Flask server with debug output enabled.| |
| 7 | +## Quickstart |
16 | 8 |
|
17 | | -## Config |
18 | | -### Magic |
19 | | -`input()` and `print()` both support the `magic` keyword argument. For `input()`, `magic` sets the `type` of the input html element. For `print()`, `magic` sets the element type. |
| 9 | +Install `python-web-io` locally using: |
| 10 | +```bash |
| 11 | +pip install python-web-io |
| 12 | +``` |
20 | 13 |
|
21 | | -||Magic|Default| |
22 | | -|:---:|:---:|:---:| |
23 | | -|`input()`|`button`, `checkbox`, `color`, `date`, `datetime-local`, `email`, `file`, `image`, `month`, `number`, `password`, `radio`, `range`, `search`, `tel`, `text`, `time`, `url`, `week`|`text`| |
24 | | -|`print()`|`style`, `img`, `address`, `footer`, `aside`, `header`, `h1..6`, `blockquote`, `p`, `b`, `abbr`, `code`, `em`, `i`, `mark`, `q`, `s`, `small`, `span`, `strong`, |`p`| |
| 14 | +Or via `poetry` using: |
| 15 | +```bash |
| 16 | +poetry add python_web_io |
| 17 | +``` |
25 | 18 |
|
26 | | -#### Arguments |
27 | | -`input()` and `print()` both support the `magic_args` keyword argument. `magic_args` accepts a dictionary, which can be used to set attributes for the html element. |
| 19 | +If evaluating / testing `python-web-io`, install dependencies for the example apps using: |
| 20 | +```bash |
| 21 | +poetry add python_web_io --with examples |
| 22 | +``` |
28 | 23 |
|
29 | | -### Cache |
30 | | -The user script is re-evaluated after each user interaction, to progress the script to the next `input()`, etc. This means expensive functions may be called more than once per session. To reduce latency, a cache decorator is made available through the `python_web_io` module. The `@cache_to_file()` decorator accepts a single argument: `file_path`, which indicates where the cache (a `.pkl` file) should be stored. |
| 24 | +After installing the project, some environment setup is required: |
31 | 25 |
|
32 | | -```python3 |
33 | | -import python_web_io as io |
| 26 | +### Required setup |
| 27 | +Create an `app.py` file containing your script, and an `.envrc` file to store project secrets. (Note: remember to add `.envrc` to your `.gitignore`) |
| 28 | +Look for example apps in [`./python_web_io/examples`](https://github.com/Cutwell/python-web-io/tree/main/python_web_io/examples). |
| 29 | +``` |
| 30 | +. |
| 31 | +├── .envrc |
| 32 | +├── config.toml |
| 33 | +└── app.py |
| 34 | +``` |
34 | 35 |
|
35 | | -@io.cache_to_file('cache.pickle') |
36 | | -def expensive_function(arg): |
37 | | - # Calculate the result here |
38 | | - return result |
| 36 | +Add the following environment variables to your `.envrc`. (Note: remember to activate the `.envrc` in your terminal using `direnv allow`) |
| 37 | +```bash |
| 38 | +# server env vars |
| 39 | +export PYTHON_WEB_IO_SECRET="" |
| 40 | +export PYTHON_WEB_IO_CONFIG=".pythonwebio/config.toml" # defaults to .pythonwebio/config.toml if not set |
39 | 41 | ``` |
40 | 42 |
|
41 | | -Cache is persistent across sessions, allowing multiple users to access it. Session specific data can be stored using `session` from `flask`. |
| 43 | +Generate a random key for `PYTHON_WEB_IO_SECRET` using this python command line snippet: |
| 44 | +```bash |
| 45 | +python -c 'import secrets; print(secrets.token_hex())' |
| 46 | +``` |
42 | 47 |
|
43 | | -```python3 |
44 | | -from flask import session |
45 | | -session['some_var] = 'some_val' |
| 48 | +If testing `wikipedia_assistant.py`, an OpenAI API key will also need to be set. |
| 49 | +```bash |
| 50 | +export OPENAI_API_KEY="" |
46 | 51 | ``` |
47 | 52 |
|
48 | | -Reserved keys for the `session` namespace are: `io` and `counter`. |
| 53 | +### Running the webapp |
| 54 | +We recommend running `python_web_io` using `uvicorn`: |
| 55 | +```bash |
| 56 | +poetry run uvicorn python_web_io.main:app |
| 57 | +``` |
49 | 58 |
|
50 | 59 | ## License |
51 | 60 | MIT |
0 commit comments