streamlitfront


Namestreamlitfront JSON
Version 0.1.33 PyPI version JSON
download
home_pagehttps://github.com/i2mint/streamlitfront
SummaryGenerate streamlit frontends from python functions
upload_time2024-05-30 11:44:29
maintainerNone
docs_urlNone
authorOtoSense
requires_pythonNone
licenseapache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# streamlitfront
Generate streamlit frontends from python functions


To install:	```pip install streamlitfront```


# Example

Define functions in a file named ``render_functions.py``:

```python
def foo(a: int = 1, b: int = 2, c=3):
    """This is foo. It computes something"""
    return (a * b) + c


def bar(x, greeting="hello"):
    """bar greets its input"""
    return f"{greeting} {x}"


def confuser(a: int, x: float = 3.14):
    return (a ** 2) * x


funcs = [foo, bar, confuser]
```

Then add the following to the file (we will be modifying this part):

```python
from streamlitfront import mk_app

if __name__ == '__main__':
    app = mk_app(funcs)
    app()
```

Execute `streamlit run render_functions.py` in terminal and ...

![image](https://user-images.githubusercontent.com/63666082/172496315-86f65258-f59f-4e17-b9bc-c92c69884311.png)

![image](https://user-images.githubusercontent.com/63666082/172496343-a0a876eb-6e6b-4e6b-a890-352c4a21664a.png)

![image](https://user-images.githubusercontent.com/63666082/172496378-40efc696-05d6-4e4e-af9f-da94e0803927.png)

... you can play with your functions!

## Configuration

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:

    ```python
    from streamlitfront import mk_app
    

    if __name__ == '__main__':
        config = {
            'app': {
                'title': 'Another application name'
            }
        }
        app = mk_app(funcs, config=config)
        app()
    ```

    Execute `streamlit run render_functions.py` in terminal and ...

    ![image](https://user-images.githubusercontent.com/63666082/172715999-6611d981-6e7c-4ea1-8d02-2ec449912bf2.png)

    ... the application name changed!

- obj

    You can define a wrapper to transform the initial object into an output of your choice to be rendered:
    
    ```python
    from typing import Iterable
    from streamlitfront import mk_app
    

    def trans(objs: Iterable):
        return list(reversed(objs))
    

    if __name__ == '__main__':
        config = {
            'obj': {
                'trans': trans
            }
        }
        app = mk_app(funcs, config=config)
        app()
    ```

    Execute `streamlit run render_functions.py` in terminal and ...
    
    ![image](https://user-images.githubusercontent.com/63666082/172716258-3efcfa55-f25c-4ae2-a232-a788f62b541b.png)

    ... 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:
    
    ```python
    from front.elements import INT_INPUT_SLIDER_COMPONENT
    from streamlitfront import mk_app
    

    if __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.py` in terminal and ...

    ![image](https://user-images.githubusercontent.com/63666082/172725124-2a88c95b-8c1f-423e-9e68-0c1b90a5e031.png)

    ... the input ``a`` is a slider now !
    
Obviously, you can combine the three types of configuration:
    
```python
from typing import Iterable
from front.elements import INT_INPUT_SLIDER_COMPONENT
from streamlitfront import mk_app
    

def trans(objs: Iterable):
    return list(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 ...

![image](https://user-images.githubusercontent.com/63666082/172725591-b3a60cf6-b497-4f4d-87d7-ce02ec90dbe4.png)

... 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.
    
```python
from typing import Any, Callable, Iterable
from front.elements import VIEW_CONTAINER, FLOAT_INPUT_SLIDER_COMPONENT, TEXT_INPUT_COMPONENT
from streamlitfront import mk_app
    

def trans(objs: Iterable):
    return list(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 ...

![image](https://user-images.githubusercontent.com/63666082/172726101-a596ea02-bf1c-4c66-b6b4-6569d1176b5c.png)

... the convention is applied !

# Old Example (using deprecated ``dispatch_funcs`` function)

Write a module like this:

```python
# simple.py

def foo(a: int = 0, b: int = 0, c=0):
    """This is foo. It computes something"""
    return (a * b) + c

def bar(x, greeting='hello'):
    """bar greets its input"""
    return f'{greeting} {x}'

def confuser(a: int = 0, x: float = 3.14):
    return (a ** 2) * x

funcs = [foo, bar, confuser]

if __name__ == '__main__':
    from streamlitfront import dispatch_funcs
    app = dispatch_funcs(funcs)
    app()
    
    # ... and you get a browser based app that exposes foo, bar, and confuser

```

Execute `streamlit run simple.py` in terminal and ...

![image](https://user-images.githubusercontent.com/1906276/121604989-61874d80-ca00-11eb-9e1b-e3ac28e09418.png)

![image](https://user-images.githubusercontent.com/1906276/121605028-7f54b280-ca00-11eb-93f7-f4c936ae9d54.png)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/i2mint/streamlitfront",
    "name": "streamlitfront",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "OtoSense",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/f3/5d/a57ed9a691eb62a585a7d72ac15ff7750fd414edd95370667d16b7babf1d/streamlitfront-0.1.33.tar.gz",
    "platform": "any",
    "description": "\n# streamlitfront\nGenerate streamlit frontends from python functions\n\n\nTo install:\t```pip install streamlitfront```\n\n\n# Example\n\nDefine functions in a file named ``render_functions.py``:\n\n```python\ndef foo(a: int = 1, b: int = 2, c=3):\n    \"\"\"This is foo. It computes something\"\"\"\n    return (a * b) + c\n\n\ndef bar(x, greeting=\"hello\"):\n    \"\"\"bar greets its input\"\"\"\n    return f\"{greeting} {x}\"\n\n\ndef confuser(a: int, x: float = 3.14):\n    return (a ** 2) * x\n\n\nfuncs = [foo, bar, confuser]\n```\n\nThen add the following to the file (we will be modifying this part):\n\n```python\nfrom streamlitfront import mk_app\n\nif __name__ == '__main__':\n    app = mk_app(funcs)\n    app()\n```\n\nExecute `streamlit run render_functions.py` in terminal and ...\n\n![image](https://user-images.githubusercontent.com/63666082/172496315-86f65258-f59f-4e17-b9bc-c92c69884311.png)\n\n![image](https://user-images.githubusercontent.com/63666082/172496343-a0a876eb-6e6b-4e6b-a890-352c4a21664a.png)\n\n![image](https://user-images.githubusercontent.com/63666082/172496378-40efc696-05d6-4e4e-af9f-da94e0803927.png)\n\n... you can play with your functions!\n\n## Configuration\n\nThe 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.\n\n- app\n\n    By default, the application name is \"My Front Application\", but you can set the title of the application as follow:\n\n    ```python\n    from streamlitfront import mk_app\n    \n\n    if __name__ == '__main__':\n        config = {\n            'app': {\n                'title': 'Another application name'\n            }\n        }\n        app = mk_app(funcs, config=config)\n        app()\n    ```\n\n    Execute `streamlit run render_functions.py` in terminal and ...\n\n    ![image](https://user-images.githubusercontent.com/63666082/172715999-6611d981-6e7c-4ea1-8d02-2ec449912bf2.png)\n\n    ... the application name changed!\n\n- obj\n\n    You can define a wrapper to transform the initial object into an output of your choice to be rendered:\n    \n    ```python\n    from typing import Iterable\n    from streamlitfront import mk_app\n    \n\n    def trans(objs: Iterable):\n        return list(reversed(objs))\n    \n\n    if __name__ == '__main__':\n        config = {\n            'obj': {\n                'trans': trans\n            }\n        }\n        app = mk_app(funcs, config=config)\n        app()\n    ```\n\n    Execute `streamlit run render_functions.py` in terminal and ...\n    \n    ![image](https://user-images.githubusercontent.com/63666082/172716258-3efcfa55-f25c-4ae2-a232-a788f62b541b.png)\n\n    ... the view order has changed !\n\n    Note that the capital letter at the beginning of the view names are gone, because the default transforming is no longer applied.\n\n- rendering\n\n    You can define the way elements are rendered in the GUI.\n    For instance, you can choose to render a text input instead of a number input for a specific parameter of a specific function:\n    \n    ```python\n    from front.elements import INT_INPUT_SLIDER_COMPONENT\n    from streamlitfront import mk_app\n    \n\n    if __name__ == '__main__':\n        config = {\n            'rendering': {\n                'Foo': {\n                    'inputs': {\n                        'a': {\n                            'component': INT_INPUT_SLIDER_COMPONENT,\n                            'max_value': 10\n                        }\n                    }\n                }\n            }\n        }\n        app = mk_app(funcs, config=config)\n        app()\n    ```\n\n    Execute `streamlit run render_functions.py` in terminal and ...\n\n    ![image](https://user-images.githubusercontent.com/63666082/172725124-2a88c95b-8c1f-423e-9e68-0c1b90a5e031.png)\n\n    ... the input ``a`` is a slider now !\n    \nObviously, you can combine the three types of configuration:\n    \n```python\nfrom typing import Iterable\nfrom front.elements import INT_INPUT_SLIDER_COMPONENT\nfrom streamlitfront import mk_app\n    \n\ndef trans(objs: Iterable):\n    return list(reversed(objs))\n\n\nif __name__ == '__main__':\n    config = {\n        'app': {\n            'title': 'Another application name'\n        },\n        'obj': {\n            'trans': trans\n        },\n        'rendering': {\n            'foo': {\n                'inputs': {\n                    'a': {\n                        'component': INT_INPUT_SLIDER_COMPONENT,\n                        'max_value': 10\n                    }\n                }\n            }\n        }\n    }\n    app = mk_app(funcs, config=config)\n    app()\n```\n\nExecute `streamlit run render_functions.py` in terminal and ...\n\n![image](https://user-images.githubusercontent.com/63666082/172725591-b3a60cf6-b497-4f4d-87d7-ce02ec90dbe4.png)\n\n... all three configurations are applied !\n\nYou 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.\n    \n```python\nfrom typing import Any, Callable, Iterable\nfrom front.elements import VIEW_CONTAINER, FLOAT_INPUT_SLIDER_COMPONENT, TEXT_INPUT_COMPONENT\nfrom streamlitfront import mk_app\n    \n\ndef trans(objs: Iterable):\n    return list(reversed(objs))\n\n\nif __name__ == '__main__':\n    convention = {\n        'app': {\n            'title': 'Another application name'\n        },\n        'obj': {\n            'trans': trans\n        },\n        'rendering': {\n            Callable: {\n                'container': VIEW_CONTAINER,\n                'inputs': {\n                    float: {\n                        'component': FLOAT_INPUT_SLIDER_COMPONENT,\n                        'max_value': 10.0,\n                        'format': '%.2f',\n                        'step': 0.01,\n                    },\n                    Any: {\n                        'component': TEXT_INPUT_COMPONENT,\n                    },\n                },\n            },\n        },\n    }\n    app = mk_app(funcs, convention=convention)\n    app()\n```\n\nExecute `streamlit run render_functions.py` in terminal and ...\n\n![image](https://user-images.githubusercontent.com/63666082/172726101-a596ea02-bf1c-4c66-b6b4-6569d1176b5c.png)\n\n... the convention is applied !\n\n# Old Example (using deprecated ``dispatch_funcs`` function)\n\nWrite a module like this:\n\n```python\n# simple.py\n\ndef foo(a: int = 0, b: int = 0, c=0):\n    \"\"\"This is foo. It computes something\"\"\"\n    return (a * b) + c\n\ndef bar(x, greeting='hello'):\n    \"\"\"bar greets its input\"\"\"\n    return f'{greeting} {x}'\n\ndef confuser(a: int = 0, x: float = 3.14):\n    return (a ** 2) * x\n\nfuncs = [foo, bar, confuser]\n\nif __name__ == '__main__':\n    from streamlitfront import dispatch_funcs\n    app = dispatch_funcs(funcs)\n    app()\n    \n    # ... and you get a browser based app that exposes foo, bar, and confuser\n\n```\n\nExecute `streamlit run simple.py` in terminal and ...\n\n![image](https://user-images.githubusercontent.com/1906276/121604989-61874d80-ca00-11eb-9e1b-e3ac28e09418.png)\n\n![image](https://user-images.githubusercontent.com/1906276/121605028-7f54b280-ca00-11eb-93f7-f4c936ae9d54.png)\n",
    "bugtrack_url": null,
    "license": "apache-2.0",
    "summary": "Generate streamlit frontends from python functions",
    "version": "0.1.33",
    "project_urls": {
        "Homepage": "https://github.com/i2mint/streamlitfront"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f35da57ed9a691eb62a585a7d72ac15ff7750fd414edd95370667d16b7babf1d",
                "md5": "47607186268f90c66c00e977d693813f",
                "sha256": "65ffbacb651fc3210b4128dc7d9b8615cb29ce00cbb1fbcc95765b8cf209d881"
            },
            "downloads": -1,
            "filename": "streamlitfront-0.1.33.tar.gz",
            "has_sig": false,
            "md5_digest": "47607186268f90c66c00e977d693813f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 49310,
            "upload_time": "2024-05-30T11:44:29",
            "upload_time_iso_8601": "2024-05-30T11:44:29.665549Z",
            "url": "https://files.pythonhosted.org/packages/f3/5d/a57ed9a691eb62a585a7d72ac15ff7750fd414edd95370667d16b7babf1d/streamlitfront-0.1.33.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-30 11:44:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "i2mint",
    "github_project": "streamlitfront",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "streamlitfront"
}
        
Elapsed time: 0.25268s