Metadata-Version: 2.1
Name: tkgen
Version: 0.2.2
Summary: A Tkinter form dialog generator.
Author-email: Théo Cavignac <theo.cavignac+dev@gmail.com>
License: BSD 3-Clause License
        
        Copyright (c) 2021, Théo Cavignac
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: repository, https://git.sr.ht/~lattay/python-tkgen
Project-URL: documentation, https://tkgen.readthedocs.io
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE

# TkGen 

## About

`tkgen` is a python package that automatically generate a Tkinter form window from a [Pyskema](https://pypi.org/project/pyskema/) schema.
It provide a simple interface to initialize the window and collect the data inputed by user.
The data is provided as python native objects that would be valid regarding the schema.
It depends on tkinter and Pyskema.

## Installation

Recommended:
Use `pip install tkgen`.

Manual:
Clone this project.
Run `pip install .` in this folder.

## Usage

The main entrypoint of this package is the function `tkgen.make_form`.
Here is a minimal exemple of its usage:
```python
from tkgen import make_form
from pyskema import Node, AtomType

schema = Node.of_record({
    "a": Node.of_atom(AtomType.INT),
})

win = make_form(schema, print)
win.mainloop()
```

The first parameter of `make_toplevel` (the model) is the schema that define the form.
The second parameter (the callback) is a function to be called when the form is submitted.
The callback is passed a single argument which is either None (if the user pressed cancel) or the data inputed.
This dictionary mirror the structure of the model.

### Extracting data

Once the form is filled, you want to access its data.
This is done through the callback parameter of `make_form`.
This callback is an arbitrary function you should provide that will receive the data in the form of a dictionary.

Here is a simple example of saving the data in an arbitrary json file:

```python
import json
from tkgen import make_form
from pyskema import Node, AtomType

model = Node.of_record({
    "filename": Node.of_atom(AtomType.STR),
    "Plumbus": Node.of_record({
        "number of schleem": Node.of_atom(AtomType.STR),
        "length of dinglepop": Node.of_atom(AtomType.FLOAT),
        "color of fleeb": Node.of_atom(AtomType.OPTION, [
            "pink",
            "red",
            "octarine",
        ]),
    }),
})

def save_data(result):
    filename = result["filename"]
    data = result["Plumbus"]
    with open(filename, "w") as f:
        json.dump(f, data)

win = make_form(model, save_data)
win.mainloop()
```

### Loading data

You may want to be able to load back some data from a previous instance of the form.
This is possible thanks to the `init_data` optional parameter.
For simplicity it is also possible through the optional `data` parameter of `make_form`.

If you were to use the previous example and save a file named *plumbus.json*, the following example would load data from the json file and produce a filled form identical to what it looked when you saved the file.

```python
import json
from tkgen import make_form
from pyskema import Node, AtomType

model = Node.of_record({
    "filename": Node.of_atom(AtomType.STR),
    "Plumbus": Node.of_record({
        "number of schleem": Node.of_atom(AtomType.STR),
        "length of dinglepop": Node.of_atom(AtomType.FLOAT),
        "color of fleeb": Node.of_atom(AtomType.OPTION, [
            "pink",
            "red",
            "octarine",
        ]),
    }),
})

with open(filename, "r") as f:
    data = json.load(f)

saved_data = {
    "filename": filename,
    "Plumbus": data,
}

def save_data(result):
    filename = result["filename"]
    data = result["Plumbus"]
    with open(filename, "w") as f:
        json.dump(f, data)

win = make_form(model, save_data, init_data=saved_data)
win.mainloop()
```
