Skip to main content

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

Metadata

Release files for defgui 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defgui 0.2.1
File Size Uploaded
defgui-0.2.1.tar.gz 7.2 kB Details

Release files / defgui-0.2.1.tar.gz

Download URL defgui-0.2.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c67d85eb08a40d4e516217ad527f7743f3ce3f1df5a05d0d7446609d0f0bcf5d
BLAKE2b-256 checksum
How to use checksums
175bf07d81e507823b9d95b46b72ff7fd0115cc72a24700861db3e9b68e91cd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page