Deploy with Streamlit and NGROK

deploy_app[source]

deploy_app(app:Union[Path, str]='app.py')

Deploy a streamlit app using ngrok

Utility functions to load a module

load_module[source]

load_module(module_path:Union[Path, str], module_name='module')

Load and return a module

import_from_module[source]

import_from_module(module_path:Union[Path, str], name:str, module_name='module')

Import a list of names from a module from its path

Get template of App

blackify[source]

blackify(code:str)

Run "black" on a code passed as string

Base Template

Templates for CSV

Templates for Tabular

Templates for NLP

Class to Create Templates

StreamlitApp.append[source]

StreamlitApp.append(content:str)

Add additional content to the app

StreamlitApp.deploy[source]

StreamlitApp.deploy(dest:Union[Path, str]='app.py')

Deploy the app

StreamlitApp.render_fastai[source]

StreamlitApp.render_fastai(title:str, author:str, model:Union[Path, str], post_process_code:str='', custom_import:str='')

Render an app based on template

Args: title: title of the App author: author of the App model: path of .pkl model to load (exported with learn.export(...)) post_process_mode: optional code to post-process the model after loading custom_import: optional code for custom import

StreamlitApp.render_pycaret[source]

StreamlitApp.render_pycaret(title:str, author:str, model:Union[Path, str], module:str, post_process_code:str='', custom_import:str='')

Render an app based on template

Args: title: title of the App author: author of the App model: path of .pkl model to load (exported with learn.export(...)) module: regression, classification, etc. post_process_mode: optional code to post-process the model after loading custom_import: custom import (optional)

StreamlitApp.save[source]

StreamlitApp.save(dest:Union[Path, str]='app.py', show=False)

Write the app to a file

Custom App

class StreamlitAppCustom[source]

StreamlitAppCustom()

Application with custom implementation

Test

# For Test Cases (might have duplicate import because it will be in a dedicated file)
import re
from pathlib import Path
from typing import List

import pytest
from test_common.utils_4_tests import DATA_DIR, diff_files

DEPLOY_DATA_DIR = DATA_DIR / "deploy"
def test_streamlit_cv_fastai(tmp_path):
    """Test Streamlit app for CV with fastai"""
    dest = tmp_path / "app_cv_fastai.py"
    StreamlitApp("CV").render_fastai(
        title="Is it a cat?",
        author="Jeff",
        model="model.pkl",
    ).append(
        """
        st.sidebar.write("---")
        st.sidebar.button("Show Balloons", on_click=st.balloons)
        """
    ).save(
        dest=dest
    )

    assert diff_files(DEPLOY_DATA_DIR / "app_CV.py", dest, show=True) == ""
def test_streamlit_tabular_pycaret(tmp_path):
    """Test Streamlit app for Tabular with pycaret"""
    dest = tmp_path / "app_tabular_pycaret.py"
    StreamlitApp("Tabular").render_pycaret(
        title="My Mini Tabular App",
        author="Jeff",
        model="model.pkl",
        module="regression",
    ).save(dest=dest)

    assert diff_files(DEPLOY_DATA_DIR / "app_Tabular.py", dest, show=True) == ""
def test_custom_app_code(tmp_path):
    """Test custom app with code provided via string"""
    dest = tmp_path / "app_custom_code.py"
    code = """
        st.write("hello")
        st.balloons()
    """
    StreamlitAppCustom().render(
        "Custom App", "Jeff", code
    ).save(dest=dest)

    assert diff_files(DEPLOY_DATA_DIR / "app_custom.py", dest, show=True) == ""
def test_custom_app_external(tmp_path):
    """Test custom app with code provided via string"""
    dest = tmp_path / "app_custom_external.py"

    StreamlitAppCustom().render(
        "Custom App", "Jeff", DEPLOY_DATA_DIR / "correct_code_for_custom_app.py"
    ).save(dest=dest)

    # Because we have a absolute path of "test_data/deploy", we will replace by relative path
    # otherwise our tests will always be failed
    content = dest.read_text(encoding="utf-8").replace(str(DEPLOY_DATA_DIR), "<deploy_dir>")
    content = content.replace(">\\", ">/").replace('r"<deploy_dir>/', 'Path(__file__).parent / "')
    dest.write_text(content, encoding="utf-8")

    assert diff_files(DEPLOY_DATA_DIR / "app_custom_external.py", dest, show=True) == ""
ipytest.run()
....                                                                                         [100%]##vso[results.publish type=JUnit;runTitle='Pytest results';]e:\AnsysDev\_perso_repo\unpackai\nbs\test-output.xml
##vso[task.logissue type=warning;]Coverage XML was not created, skipping upload.

------------ generated xml file: e:\AnsysDev\_perso_repo\unpackai\nbs\test-output.xml -------------
4 passed in 0.18s