appyter


Nameappyter JSON
Version 0.19.24 PyPI version JSON
download
home_pagehttps://github.com/maayanLab/appyter/
SummaryNone
upload_time2024-03-21 14:34:19
maintainerNone
docs_urlNone
authorDaniel J. B. Clarke
requires_python>=3.7.0
licenseCC-BY-NC-SA-4.0
keywords
VCS
bugtrack_url
requirements aiohttp_remotes aiohttp-wsgi aiohttp bs4 click click-default-group flask_cors flask fsspec fusepy ipython jinja2 jupyter_client lxml nbclient nbconvert nbformat notebook python-dotenv python-engineio python-socketio pyyaml watchgod werkzeug yarl
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Appyter
This module enables you to turn your jupyter notebook into a jinja2 template-driven web application. Or just parse for other purposes.

## Installation
NOTE: This application uses modern python features including async await (3.5) and fstrings (3.6), to ensure you have the best experience please use python >= 3.8.

```bash
# Install package from pip
pip3 install --upgrade appyter

# OR Install package from github repository main
pip3 install --upgrade git+https://github.com/Maayanlab/appyter.git
```

## Usage
Appyter enables you to serve that notebook on an executable webapp.

`appyter jupyter_notebook.ipynb`

If for some reason, `appyter` doesn't end up discoverable in your PATH, you can use `python3 -m appyter` instead.

- A dotenv file (`.env`) or environment variables can be use to configure HOST (APPYTER_HOST), PORT (APPYTER_PORT), and PREFIX (APPYTER_PREFIX) of the webserver.
- Some pre-configured profiles can be used for styling the form (`--profile=profile_name`) see `appyter/profiles`
- In debug mode (`--debug`), changes to the notebook will automatically update the webapp.
- Custom fields can be used by putting them in the directory of execution with the following format:
  - `./fields/YourField.py`: Python Field Implementation
- The templates used natively by the application can be modified to provide your own look and feel:
  - `./templates/head.jinja2`: Custom head (title, CSS, scripts)
  - `./templates/form.jinja2`: Custom form handling
  - `./templates/fields/StringField.jinja2`: Override field style
- Custom jinja2 filters can be added if necessary
  - `./filters/your_filter.py`: Python jinja2 filter (function)
- Custom externally-referenced resources (i.e. images) can be put under the static directory
  - `./static/img/your_image.png`: Reference in templates with `{% static 'img/your_image.png' %}`
- Custom blueprints to additionally mount with flask
  - `./blueprints/your_app/__init__.py`: `your_app = Blueprint('your_app', __name__)`


## Creating an Appyter

Create a standard python jupyter notebook, make the first cell:
```
#%%appyter init
from appyter import magic
magic.init(lambda _=globals: _())
```

Normal cells are allowed, you also have access to jinja2-rendered cells:
```
%%appyter {cell_type}
```

Supported cell_types:
- `markdown`: Substitute jinja2 template variables, render as a markdown cell
- `hide`: Substitute jinja2 template variables, show it rendered in your notebook with the default values, but when executing publicly, don't show/execute the cell.
- `code_exec`: Substitute jinja2 template variables, render as python, show it rendered in your notebook with the default values and execute it
- `code_eval`: Substitute jinja2 template variables, render as python, show it rendered in your notebook with the default values and execute it, "eval" the last line of the cell and display the result.

## Discussion
Consider the following notebook:

```
%%appyter markdown
# {{ StringField(name='title', label='Title', default='My Title').render_value }}

{{ TextField(name='description', label='Description', default='My description').render_value }}
```

```
%%appyter code_eval
{% set number_1 = IntField(name='number_1', label='First Number', min=0, max=10, default=5) %}
{% set number_2 = IntField(name='number_2', label='Second Number', min=0, max=10, default=5) %}
{% set op = ChoiceField(
  name='operator',
  label='Operator',
  choices={
    'add': '+',
    'subtract': '-',
    'multiply': '*',
    'divide': '/',
    'power': '**',
  },
  default='add',
) %}
answer = {{ number_1 }} {{ op }} {{ number_2 }}
```

This can be parsed in various ways:

```python
# Parse variables alone
assert parse_variables(appyter) == {
  'number_1': IntField(name='number_1', label='First Number', min=0, max=10, default=5),
  'number_2': IntField(name='number_2', label='Second Number', min=0, max=10, default=5),
  'operator': ChoiceField(
    name='operator',
    label='Operator',
    choices={
      'add': '+',
      'subtract': '-',
      'multiply': '*',
      'divide': '/',
      'power': '**'
    },
    default='add'
  ),
}

# Render jupyter notebook with variables substituted
assert render_jupyter(appyter, {
  'title': 'Test',
  'description': '',
  'number_1': 0,
  'number_2': 5,
  'operator': 'subtract',
}) == '''
---
# Test

---
answer = 0 - 5
---
''' # in valid ipynb syntax

# Render web UI
assert render_html(appyter) == '''
<form id="notebook">
  Title: <input type="string" name="title"></input>
  Description: <textarea name="description"></textarea>
  First Number: <input type="number" name="number_1" min=0 max=10></input>
  Second Number: <input type="number" name="number_2" min=0 max=10></input>
  Operator: <select name="operator">
    <option value="add">add</option>
    <option value="subtract">subtract</option>
    <option value="divide">divide</option>
    <option value="power">power</option>
  </select>
  <input type="submit" text="Submit"></input>
</form>
<script src="./appyter.js"></script>
<script>
  document.getElementById("notebook").on('submit', appyter.submit)
</script>
'''

# Web UI Renderer
assert render_html_renderer(appyter, {
  'title': 'Test',
  'description': '',
  'number_1': 0,
  'number_2': 5,
  'operator': 'subtract',
}) == '''
<div id="notebook"></div>
<script src="./appyter.js"></script>
<script>
  document.on('load', function () {
    appyter.render(
      document.getElementById("notebook"),
      {
        "title": "Test",
        "description": "",
        "number_1": 0,
        "number_2": 5,
        "operator": "subtract",
      }
    )
  })
</script>
'''
```

`appyter` can be used to get these conversions, it can also be used to host standalone webapps using these mechanisms.

## Development

### Testing

`pytest` is used for python testing. It can be invoked as follows:

```bash
# it is also prudent to test other protocols such as s3 (production) & sbfs (CAVATICA)
export FSSPEC_URI='memory://'
pytest --log-cli-level=DEBUG appyter
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/maayanLab/appyter/",
    "name": "appyter",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7.0",
    "maintainer_email": null,
    "keywords": null,
    "author": "Daniel J. B. Clarke",
    "author_email": "u8sand@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e6/ed/a07aac175f678ceed3c011675ace8af739cd92bb8fad04ff2fc538ca7318/appyter-0.19.24.tar.gz",
    "platform": null,
    "description": "# Appyter\nThis module enables you to turn your jupyter notebook into a jinja2 template-driven web application. Or just parse for other purposes.\n\n## Installation\nNOTE: This application uses modern python features including async await (3.5) and fstrings (3.6), to ensure you have the best experience please use python >= 3.8.\n\n```bash\n# Install package from pip\npip3 install --upgrade appyter\n\n# OR Install package from github repository main\npip3 install --upgrade git+https://github.com/Maayanlab/appyter.git\n```\n\n## Usage\nAppyter enables you to serve that notebook on an executable webapp.\n\n`appyter jupyter_notebook.ipynb`\n\nIf for some reason, `appyter` doesn't end up discoverable in your PATH, you can use `python3 -m appyter` instead.\n\n- A dotenv file (`.env`) or environment variables can be use to configure HOST (APPYTER_HOST), PORT (APPYTER_PORT), and PREFIX (APPYTER_PREFIX) of the webserver.\n- Some pre-configured profiles can be used for styling the form (`--profile=profile_name`) see `appyter/profiles`\n- In debug mode (`--debug`), changes to the notebook will automatically update the webapp.\n- Custom fields can be used by putting them in the directory of execution with the following format:\n  - `./fields/YourField.py`: Python Field Implementation\n- The templates used natively by the application can be modified to provide your own look and feel:\n  - `./templates/head.jinja2`: Custom head (title, CSS, scripts)\n  - `./templates/form.jinja2`: Custom form handling\n  - `./templates/fields/StringField.jinja2`: Override field style\n- Custom jinja2 filters can be added if necessary\n  - `./filters/your_filter.py`: Python jinja2 filter (function)\n- Custom externally-referenced resources (i.e. images) can be put under the static directory\n  - `./static/img/your_image.png`: Reference in templates with `{% static 'img/your_image.png' %}`\n- Custom blueprints to additionally mount with flask\n  - `./blueprints/your_app/__init__.py`: `your_app = Blueprint('your_app', __name__)`\n\n\n## Creating an Appyter\n\nCreate a standard python jupyter notebook, make the first cell:\n```\n#%%appyter init\nfrom appyter import magic\nmagic.init(lambda _=globals: _())\n```\n\nNormal cells are allowed, you also have access to jinja2-rendered cells:\n```\n%%appyter {cell_type}\n```\n\nSupported cell_types:\n- `markdown`: Substitute jinja2 template variables, render as a markdown cell\n- `hide`: Substitute jinja2 template variables, show it rendered in your notebook with the default values, but when executing publicly, don't show/execute the cell.\n- `code_exec`: Substitute jinja2 template variables, render as python, show it rendered in your notebook with the default values and execute it\n- `code_eval`: Substitute jinja2 template variables, render as python, show it rendered in your notebook with the default values and execute it, \"eval\" the last line of the cell and display the result.\n\n## Discussion\nConsider the following notebook:\n\n```\n%%appyter markdown\n# {{ StringField(name='title', label='Title', default='My Title').render_value }}\n\n{{ TextField(name='description', label='Description', default='My description').render_value }}\n```\n\n```\n%%appyter code_eval\n{% set number_1 = IntField(name='number_1', label='First Number', min=0, max=10, default=5) %}\n{% set number_2 = IntField(name='number_2', label='Second Number', min=0, max=10, default=5) %}\n{% set op = ChoiceField(\n  name='operator',\n  label='Operator',\n  choices={\n    'add': '+',\n    'subtract': '-',\n    'multiply': '*',\n    'divide': '/',\n    'power': '**',\n  },\n  default='add',\n) %}\nanswer = {{ number_1 }} {{ op }} {{ number_2 }}\n```\n\nThis can be parsed in various ways:\n\n```python\n# Parse variables alone\nassert parse_variables(appyter) == {\n  'number_1': IntField(name='number_1', label='First Number', min=0, max=10, default=5),\n  'number_2': IntField(name='number_2', label='Second Number', min=0, max=10, default=5),\n  'operator': ChoiceField(\n    name='operator',\n    label='Operator',\n    choices={\n      'add': '+',\n      'subtract': '-',\n      'multiply': '*',\n      'divide': '/',\n      'power': '**'\n    },\n    default='add'\n  ),\n}\n\n# Render jupyter notebook with variables substituted\nassert render_jupyter(appyter, {\n  'title': 'Test',\n  'description': '',\n  'number_1': 0,\n  'number_2': 5,\n  'operator': 'subtract',\n}) == '''\n---\n# Test\n\n---\nanswer = 0 - 5\n---\n''' # in valid ipynb syntax\n\n# Render web UI\nassert render_html(appyter) == '''\n<form id=\"notebook\">\n  Title: <input type=\"string\" name=\"title\"></input>\n  Description: <textarea name=\"description\"></textarea>\n  First Number: <input type=\"number\" name=\"number_1\" min=0 max=10></input>\n  Second Number: <input type=\"number\" name=\"number_2\" min=0 max=10></input>\n  Operator: <select name=\"operator\">\n    <option value=\"add\">add</option>\n    <option value=\"subtract\">subtract</option>\n    <option value=\"divide\">divide</option>\n    <option value=\"power\">power</option>\n  </select>\n  <input type=\"submit\" text=\"Submit\"></input>\n</form>\n<script src=\"./appyter.js\"></script>\n<script>\n  document.getElementById(\"notebook\").on('submit', appyter.submit)\n</script>\n'''\n\n# Web UI Renderer\nassert render_html_renderer(appyter, {\n  'title': 'Test',\n  'description': '',\n  'number_1': 0,\n  'number_2': 5,\n  'operator': 'subtract',\n}) == '''\n<div id=\"notebook\"></div>\n<script src=\"./appyter.js\"></script>\n<script>\n  document.on('load', function () {\n    appyter.render(\n      document.getElementById(\"notebook\"),\n      {\n        \"title\": \"Test\",\n        \"description\": \"\",\n        \"number_1\": 0,\n        \"number_2\": 5,\n        \"operator\": \"subtract\",\n      }\n    )\n  })\n</script>\n'''\n```\n\n`appyter` can be used to get these conversions, it can also be used to host standalone webapps using these mechanisms.\n\n## Development\n\n### Testing\n\n`pytest` is used for python testing. It can be invoked as follows:\n\n```bash\n# it is also prudent to test other protocols such as s3 (production) & sbfs (CAVATICA)\nexport FSSPEC_URI='memory://'\npytest --log-cli-level=DEBUG appyter\n```\n",
    "bugtrack_url": null,
    "license": "CC-BY-NC-SA-4.0",
    "summary": null,
    "version": "0.19.24",
    "project_urls": {
        "Homepage": "https://github.com/maayanLab/appyter/"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "489bc174064e1ae35435a9f56d5ca8b6fd4854d2c25b2f1305586d1094d0ba9b",
                "md5": "d3147fc65218bd0018c76a484eeab8ef",
                "sha256": "4e4bcc195ccde8b9910d6e5d9a91f2b0d8745a3ad2b2829860876e250aa94aa1"
            },
            "downloads": -1,
            "filename": "appyter-0.19.24-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d3147fc65218bd0018c76a484eeab8ef",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.0",
            "size": 1950186,
            "upload_time": "2024-03-21T14:34:15",
            "upload_time_iso_8601": "2024-03-21T14:34:15.935170Z",
            "url": "https://files.pythonhosted.org/packages/48/9b/c174064e1ae35435a9f56d5ca8b6fd4854d2c25b2f1305586d1094d0ba9b/appyter-0.19.24-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e6eda07aac175f678ceed3c011675ace8af739cd92bb8fad04ff2fc538ca7318",
                "md5": "2269074cefcb19d7549fd0d1022182a8",
                "sha256": "3a2d0654b9f9a5f9b58104651c2a6112f0d789cee86a87674befd445c8aac004"
            },
            "downloads": -1,
            "filename": "appyter-0.19.24.tar.gz",
            "has_sig": false,
            "md5_digest": "2269074cefcb19d7549fd0d1022182a8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.0",
            "size": 1851030,
            "upload_time": "2024-03-21T14:34:19",
            "upload_time_iso_8601": "2024-03-21T14:34:19.575420Z",
            "url": "https://files.pythonhosted.org/packages/e6/ed/a07aac175f678ceed3c011675ace8af739cd92bb8fad04ff2fc538ca7318/appyter-0.19.24.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-21 14:34:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "maayanLab",
    "github_project": "appyter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "aiohttp_remotes",
            "specs": [
                [
                    ">=",
                    "1.2"
                ],
                [
                    "<",
                    "1.3"
                ]
            ]
        },
        {
            "name": "aiohttp-wsgi",
            "specs": [
                [
                    "==",
                    "0.10.0"
                ]
            ]
        },
        {
            "name": "aiohttp",
            "specs": [
                [
                    ">=",
                    "3.8.3"
                ],
                [
                    "<",
                    "3.9"
                ]
            ]
        },
        {
            "name": "bs4",
            "specs": []
        },
        {
            "name": "click",
            "specs": []
        },
        {
            "name": "click-default-group",
            "specs": []
        },
        {
            "name": "flask_cors",
            "specs": []
        },
        {
            "name": "flask",
            "specs": [
                [
                    "<",
                    "2.1"
                ],
                [
                    ">=",
                    "2.0"
                ]
            ]
        },
        {
            "name": "fsspec",
            "specs": [
                [
                    "==",
                    "2022.1.0"
                ]
            ]
        },
        {
            "name": "fusepy",
            "specs": []
        },
        {
            "name": "ipython",
            "specs": []
        },
        {
            "name": "jinja2",
            "specs": []
        },
        {
            "name": "jupyter_client",
            "specs": []
        },
        {
            "name": "lxml",
            "specs": []
        },
        {
            "name": "nbclient",
            "specs": [
                [
                    "==",
                    "0.5.13"
                ]
            ]
        },
        {
            "name": "nbconvert",
            "specs": []
        },
        {
            "name": "nbformat",
            "specs": [
                [
                    "<",
                    "6.0"
                ],
                [
                    ">=",
                    "5.0"
                ]
            ]
        },
        {
            "name": "notebook",
            "specs": []
        },
        {
            "name": "python-dotenv",
            "specs": []
        },
        {
            "name": "python-engineio",
            "specs": [
                [
                    "==",
                    "4.0.0"
                ]
            ]
        },
        {
            "name": "python-socketio",
            "specs": [
                [
                    "==",
                    "5.0.4"
                ]
            ]
        },
        {
            "name": "pyyaml",
            "specs": []
        },
        {
            "name": "watchgod",
            "specs": []
        },
        {
            "name": "werkzeug",
            "specs": [
                [
                    "==",
                    "2.0.3"
                ]
            ]
        },
        {
            "name": "yarl",
            "specs": [
                [
                    "==",
                    "1.8.2"
                ]
            ]
        }
    ],
    "lcname": "appyter"
}
        
Elapsed time: 0.32351s