Skip to main content

A python library to help you build simple UI forms for your python functions

Project description

Python UI-Me

A simple python decorator, to build UI forms out of your everyday python functions

license

UI-Me

Python UI-Me (as in: Python methods saying "make elegant UI forms out of me") is a Python package that enables developers to quickly create web-based user interfaces for Python functions. It uses decorators to mark functions for UI exposure and a built-in Flask server to render the UI.

TL;DR

The promise: With-in 3 lines of code, you will be able to get a working UI form out of your python functions
Another promise: You won't regret the time spent in reading the Motivation section, it is not too long
But for the ones seeking instant gratification: watch this fancy gif (which I totally struggled to create):
demo.gif

Motivation

I'm pretty sure I'm not the only lazy developer that over-engineers every small daily task as scripts.
Honestly, this is yet another attempt of the aforementioned over-engineering, towards making it as easy as possible to whip up a quick UI to run those scripts.
Yes, there are good alternatives, argparse being a popular one for running your scripts through cli. But I've always struggled with it the moment the script has multiple functionalities or modules. The day you start forgetting your engineering principles and start overloading your one script to do many things (because duh! that was the whole point of writing it as a script), these cli tools start to fall apart.
Not the mention the amount of code you'd have to write, like stitching parsers and subparsers and subsubparsers and..

Having said that, for proper production scripts, UI-me is not the way to go. But for those quick and dirty daily / personal scripts, you would find UI-me useful

Features

  • Easy Function Exposure: Decorate Python functions with @ui_enabled to expose them as web forms.
  • Automatic UI Generation: Generates web UIs for decorated functions, with form fields corresponding to function parameters.
  • Grouping of Functions: Organize functions into groups (nav-tabs) in the UI for better organization.
  • Customizable Function Metadata: Specify titles, descriptions, and other metadata for functions.
  • Built-in Web Server: Comes with an integrated Flask web server to host the UI.
  • Clipboard Support: Easy copy-to-clipboard feature for function outputs.
  • Global Variables: Set global variables for your script, from within the UI.

Installation

Install Python UI-me using pip:

python3 -m pip install python-uime

Usage

Basically 3 lines of code

from uime import start_server, ui_enabled  ## <--- This is line 1

##  Below is line 2
@ui_enabled(group="Greeting", description="This function will greet you (with positivity!)")
def hello_world(name):
    return f"Hello {name}"

@ui_enabled(group="Greeting", title="My Test Function with Nice Title",
            description="This function will return a json (So that you can see it is nicely printed)")
def make_api_call(url, data):
    return json.dumps({"url": url, "data": hello_world(data)})

@ui_enabled(group="Maths", description="This will return a + b")
def sum_math_function(a, b):
    return a + b

@ui_enabled(group="Maths")
def difference_math_function(a, b):
    return a - b

if __name__ == '__main__':
    start_server()  ## <--- This is line 3 (As promised, within 3 lines of code)

img.png

Advanced Usage

1. Setting Global Variables

You might run into situations where you want to set global variables of your script.
This is going to be a little more involved - you need to expose a setter to the global variable, while using a new decorator @ui_global
Here is an example:

from uime import start_server, ui_enabled, ui_global  ## <--- ui_global is the new import

DEFAULT = "There are no accidents."
DEFAULT_2 = "Only coincidences."

@ui_global(name="DEFAULT", description="Global DEFAULT value", default_value=DEFAULT)
def set_default(value):
    global DEFAULT
    DEFAULT = value

@ui_global(name="DEFAULT_2", description="Global DEFAULT_2 value", default_value=DEFAULT_2)
def set_default2(value):
    global DEFAULT_2
    DEFAULT_2 = value


@ui_enabled(group="group1")
def hello_world(name):
    return f"Hello {name}. {DEFAULT} {DEFAULT_2}" # <-- using the global variables

Use should see a button on the top right corner of the UI, which will allow you to set the global variables.
global.png

Dependencies

The following is not the exhaustive list of dependencies, but UI-me was made possible because of these:

  • Flask: quickest way to spin up a web server
  • Jinja2: for templating
  • Tailwind CSS: it is a pretty neat, utility-first CSS framework for rapid UI development

Features Pending

  • Handle overloading of functions (identify functions with ids rather than names)
  • Add support for setting global variables in the UI (Setting Global Variables)
  • Add support for complex data-structures as inputs (like list of strings, or json strings)
  • Make default values for parameters as non-mandatory in the form
  • Capture parameter data types and change the form field type accordingly

Contributions

Please raise Issues, Bugs or any feature requests at Github Issues.
If you plan on contributing to the code, fork the repository and raise a Pull Request back here.

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

python-uime-1.0.3.tar.gz (14.5 kB view hashes)

Uploaded Source

Supported by

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