Package: python-uime
Python UI-MeA simple python decorator, to build UI forms out of your everyday python functionsUI-MePython 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;DRThe promise:With-in 3 lines of code, you will be able to get a working UI form out of your python functionsAnother promise:You won't regret the time spent in reading theMotivationsection, it is not too
longBut for the ones seeking instant gratification: watch this fancy gif (which I totally struggled to create):MotivationI'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,argparsebeing 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 usefulThink of this likeSwagger UI, but for python functions.FeaturesEasy Function ExposureDecorate Python functions with@ui_enabledto expose them as web forms.Automatic UI GenerationGenerates web UIs for decorated functions, with form fields corresponding to function parameters.Grouping of FunctionsOrganize functions intogroups (nav-tabs) in the UI for better organization.Customizable Function MetadataSpecify titles, descriptions, and other metadata for functions.Built-in Web ServerComes with an integrated Flask web server to host the UI.Clipboard SupportEasy copy-to-clipboard feature for function outputs.Quick navigation SidebarIf you have too many functions, the sidebar can be used to quickly navigate to your functionGlobal VariablesSet global variables for your script, from within the UI.Type Inferring (This is Cool !!)Functions that contain arguments with types, are inferred and rendered accordingly in the INPUT formInstallationInstall Python UI-me using pip:python3-mpipinstallpython-uimeUsageBasically 3 lines of codefromuimeimportstart_server,ui_enabled## <--- This is line 1##  Below is line 2@ui_enabled(group="Greeting",description="This function will greet you (with positivity!)")defhello_world(name):returnf"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)")defmake_api_call(url,data):returnjson.dumps({"url":url,"data":hello_world(data)})@ui_enabled(group="Maths",description="This will return a + b")defsum_math_function(a,b):returna+b@ui_enabled(group="Maths")defdifference_math_function(a,b):returna-bif__name__=='__main__':start_server()## <--- This is line 3 (As promised, within 3 lines of code)Advanced Usage1. Setting Global VariablesYou 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_globalHere is an example:fromuimeimportstart_server,ui_enabled,ui_global## <--- ui_global is the new importDEFAULT="There are no accidents."DEFAULT_2="Only coincidences."@ui_global(name="DEFAULT",description="Global DEFAULT value",default_value=DEFAULT)defset_default(value):globalDEFAULTDEFAULT=value@ui_global(name="DEFAULT_2",description="Global DEFAULT_2 value",default_value=DEFAULT_2)defset_default2(value):globalDEFAULT_2DEFAULT_2=value@ui_enabled(group="group1")defhello_world(name):returnf"Hello{name}.{DEFAULT}{DEFAULT_2}"# <-- using the global variablesThe left navigation bar contains a section for Global Variables, which will allow you to set them.2. Function Parameter Type Inferringtypeform inputtype inferrednothingtext inputstrstrtext inputstrboolswitchboolintnumber inputintfloatnumber inputfloatcomplexnumber inputcomplexlisttext arealist of stringsettext areaset of stringtypetext arealist of stringList[str]text arealist of stringList[int]text arealist of intList[float]text arealist of floatList[complex]text arealist of complexList[list]text arealist of list of stringdicttext areajson to dictSample code below shows a list of parameters that have different types:fromtypingimportListimportjsonfromuimeimportstart_server,ui_enabled@ui_enabled(group="group1")deftest_list_string(regular_list:list,strings:List[str],ints:List[int],dicts:dict,list_of_list:List[list]):returnf"""list ={regular_list}strings={strings}ints={ints}dicts:{json.dumps(dicts)}list_of_list:{list_of_list}"""The UI would look something like this[!NOTE]
As you observe the above table, a text-area is used to collect inputs for complex datatypes. This is being done on
purpose to keep things simple, but that simplicity comes at a cost.
When inferring a list of anything from the text-area - extraction of the list from the large string, is done by using
delimiters.
By default, the priority of delimiters are NEWLINE, COMMA, SPACE
ie: NEWLINE is checked first, if available, the string will be split by newline and returned as a list. Then there is a
check for COMMA, if available, the string will be split by comma and returned as a list. Finally SPACE.
In case of , it is always a check on NEWLINE for the outer list, and COMMA/SPACE for the inner listsDependenciesThe 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 serverJinja2: for templatingTailwind CSS: it is a pretty neat, utility-first CSS framework for rapid UI developmentFeatures PendingHandle overloading of functions (identify functions with ids rather than names)Add support for settingglobalvariables 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 formCapture parameter data types and change the form field type accordinglyContributionsPlease raise Issues, Bugs or any feature requests
atGithub Issues.If you plan on contributing to the code, fork the repository and raise a Pull Request back here.