Generate streamlit frontends from python functions
To install: pip install streamlitfront
Define functions in a file named render_functions.py:
deffoo(a: int=1, b: int=2, c=3):
"""This is foo. It computes something"""return (a*b) +cdefbar(x, greeting="hello"):
"""bar greets its input"""returnf"{greeting}{x}"defconfuser(a: int, x: float=3.14):
return (a**2) *xfuncs= [foo, bar, confuser]Then add the following to the file (we will be modifying this part):
fromstreamlitfrontimportmk_appif__name__=='__main__':
app=mk_app(funcs)
app()Execute streamlit run render_functions.py in terminal and ...
... you can play with your functions!
The default configuration for the application is define by the convention object: dflt_convention. But you can overwrite parts or the entire configuration by setting the config parameter. The configuration is composed of three parts: app, obj and rendering.
app
By default, the application name is "My Front Application", but you can set the title of the application as follow:
fromstreamlitfrontimportmk_appif__name__=='__main__': config= { 'app': { 'title': 'Another application name' } } app=mk_app(funcs, config=config) app()
Execute
streamlit run render_functions.pyin terminal and ...... the application name changed!
obj
You can define a wrapper to transform the initial object into an output of your choice to be rendered:
fromtypingimportIterablefromstreamlitfrontimportmk_appdeftrans(objs: Iterable): returnlist(reversed(objs)) if__name__=='__main__': config= { 'obj': { 'trans': trans } } app=mk_app(funcs, config=config) app()
Execute
streamlit run render_functions.pyin terminal and ...... the view order has changed !
Note that the capital letter at the beginning of the view names are gone, because the default transforming is no longer applied.
rendering
You can define the way elements are rendered in the GUI. For instance, you can choose to render a text input instead of a number input for a specific parameter of a specific function:
fromfront.elementsimportINT_INPUT_SLIDER_COMPONENTfromstreamlitfrontimportmk_appif__name__=='__main__': config= { 'rendering': { 'Foo': { 'inputs': { 'a': { 'component': INT_INPUT_SLIDER_COMPONENT, 'max_value': 10 } } } } } app=mk_app(funcs, config=config) app()
Execute
streamlit run render_functions.pyin terminal and ...... the input
ais a slider now !
Obviously, you can combine the three types of configuration:
fromtypingimportIterablefromfront.elementsimportINT_INPUT_SLIDER_COMPONENTfromstreamlitfrontimportmk_appdeftrans(objs: Iterable):
returnlist(reversed(objs))
if__name__=='__main__':
config= {
'app': {
'title': 'Another application name'
},
'obj': {
'trans': trans
},
'rendering': {
'foo': {
'inputs': {
'a': {
'component': INT_INPUT_SLIDER_COMPONENT,
'max_value': 10
}
}
}
}
}
app=mk_app(funcs, config=config)
app()Execute streamlit run render_functions.py in terminal and ...
... all three configurations are applied !
You can also overwrite the whole configuration by setting the convention parameter. Be careful though, by overwritting the default convention, you have to make sure that all configuations are defined. Otherwise, the application would crash or behave unexpectedly.
fromtypingimportAny, Callable, Iterablefromfront.elementsimportVIEW_CONTAINER, FLOAT_INPUT_SLIDER_COMPONENT, TEXT_INPUT_COMPONENTfromstreamlitfrontimportmk_appdeftrans(objs: Iterable):
returnlist(reversed(objs))
if__name__=='__main__':
convention= {
'app': {
'title': 'Another application name'
},
'obj': {
'trans': trans
},
'rendering': {
Callable: {
'container': VIEW_CONTAINER,
'inputs': {
float: {
'component': FLOAT_INPUT_SLIDER_COMPONENT,
'max_value': 10.0,
'format': '%.2f',
'step': 0.01,
},
Any: {
'component': TEXT_INPUT_COMPONENT,
},
},
},
},
}
app=mk_app(funcs, convention=convention)
app()Execute streamlit run render_functions.py in terminal and ...
... the convention is applied !
Write a module like this:
# simple.pydeffoo(a: int=0, b: int=0, c=0):
"""This is foo. It computes something"""return (a*b) +cdefbar(x, greeting='hello'):
"""bar greets its input"""returnf'{greeting}{x}'defconfuser(a: int=0, x: float=3.14):
return (a**2) *xfuncs= [foo, bar, confuser]
if__name__=='__main__':
fromstreamlitfrontimportdispatch_funcsapp=dispatch_funcs(funcs)
app()
# ... and you get a browser based app that exposes foo, bar, and confuserExecute streamlit run simple.py in terminal and ...









