Skip to main content

Decorator for Fast Generation of Function Input Output Components with Tkinter or Streamlit

Project description

logo

中文

This module is a function decorator that automatically generates corresponding GUI components based on the function's parameters and return values.

Table of Contents

Overview

This module is inspired by magicgui, thank you to the author. This module has two branches: one based on tkinter and one based on streamlit. Main differences from magicgui:

  1. Magicgui uses pyqt and pyside as tools, while this module uses tkinter and streamlit.
  2. This module only provides a decorator to generate components, not separate GUI components.
  3. This module can return the input values of input components, which is convenient for separate use.

Features

Basic function: Use a decorator to automatically generate corresponding GUI components based on the function's parameters and return values.

tkinter branch

  • Based mainly on tkinter and the typing and ctypes libraries from the Python standard library. No third-party dependencies like pyside are required.
  • Based on the principle of easy use, this branch has only one decorator function and no other parameter settings.
  • The output components generated by this branch are consistent with the number of return values.
  • DPI adaptation is automatically performed, and the interface will not be blurred.
  • Currently supported parameter types: int, float, str, List[str]

streamlit branch

  • Based mainly on streamlit.
  • This branch allows you to set whether input components are placed horizontally or vertically.
  • When placed horizontally, you can set the column width ratio for each component.
  • You can set whether to display the execute button and whether to perform function calculations.
  • Returns the input values of each component and the function execution result values for subsequent separate use.

Usage

General Installation

pip install defgui
pip install streamlit

If Streamlit is not required, install version v0.1.

pip install defgui==0.1

When importing modules:

from defgui import defgui

You can use it directly by adding a decorator to the function.

Note: When defining the function, the parameters need to have type annotations, otherwise an error will be reported.

1. Using the tkinter branch

  • Currently supported type annotations for this branch: int, float, str, List[str]
  • Click the automatically generated run button to display the output results.
from defgui import defgui_tkinter
from typing import List
# Define the function
@defgui_tkinter
def example_function(a: int, b: float, c: str, d: List[str]) -> tuple:
    """Example function that returns a tuple of four values."""
    return a + 1, b + 1, "str:%s" % (c), d
# Run the function
example_function()

Running results

png

2. Using the streamlit branch

  • Currently supported type annotations for this branch: int, float, str, bool, list, datetime.date
  • For list parameters, if not assigned a value, the generated component will not have any options and will appear grey and unavailable. It is recommended to assign a value to the list parameter before running it.
  • For datetime.date parameters, if not assigned a value, the generated date component will default to the current date.
  • When defining the function, the parameters can be positional or keyword parameters, but they must have type annotations, otherwise an error will be reported.
  • This branch of the decorator has 3 default parameters: horizontal=True, col_size=None, execute=True
  • horizontal=True: Input components are placed horizontally by default.
  • col_size=None: Column size is not set by default, and the column width is divided evenly according to the number of input components.
  • execute=True: The execute button is displayed by default, and function calculations are performed. False will not display it.
  • When the decorated function is executed, it defaults to returning the input values of the input components and the result values. The input values are of list type, and the order of list items is consistent with the order of function parameters.
  • The decorated function can be executed with or without values assigned.

(1) Executing with parameter assignment, components placed horizontally, and the execute button displayed

from defgui import defgui_streamlit
import streamlit as st
import datetime
st.set_page_config(layout="wide")
st.title("Streamlit Decorator App")
@defgui_streamlit(horizontal=True, col_size=[1, 1, 1, 2, 2], execute=True)
def greet(date: datetime.date, name: str, age: int = 20, food: list = []) -> str:
    return f"{date}, Hello, {name}! You are {age} years old, you like {food}"
input_values, results = greet(name="david", food=["apple", "banana"])
st.write(input_values[1])

png

(2) Executing without parameter assignment, components placed vertically, and the execute button not displayed

from defgui import defgui_streamlit
import streamlit as st
import datetime
st.set_page_config(layout="wide")
st.title("Streamlit Decorator App")
@defgui_streamlit(horizontal=False, col_size=None, execute=False)
def greet(date: datetime.date, name: str, age: int, food: list) -> str:
    return f"{date}, Hello, {name}! You are {age} years old, you like {food}"
cols = st.columns(3)
with cols[0]:
    greet()

png

Version Notes

v 0.2 Added a decorator based on streamlit

v 0.1 Decorator based on tkinter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

defgui-0.2.1.tar.gz (7.2 kB view details)

Uploaded Source

File details

Details for the file defgui-0.2.1.tar.gz.

File metadata

  • Download URL: defgui-0.2.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.8

File hashes

Hashes for defgui-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c67d85eb08a40d4e516217ad527f7743f3ce3f1df5a05d0d7446609d0f0bcf5d
MD5 d263efb2af84c5df991e5631cd62dea6
BLAKE2b-256 175bf07d81e507823b9d95b46b72ff7fd0115cc72a24700861db3e9b68e91cd2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page