Skip to main content

RHDZMOTA Extension App: streamlit_webapps

Project description

RHDZMOTA EXT: streamlit_webapps

Package extensions have 2 main purposes:

  • Split the dependency sets into several smaller python projects
  • Serve as domain-specific packages (i.e., split functionality).

About the streamlit_webapps extension

The streamlit extension contains some common useful abstractions to be used when working on creating streamlit applications.

The starting abstraction is the PageView class, which provides an "easy to reason about" pattern to follow when working on creating webapps. To get a sneak-peak of the functionality, consider creating a webapp.py script with the following content:

from rhdzmota.ext.streamlit_webapps.page_view import PageView


if __name__ == "__main__":
    with PageView() as page:
        page.view()
  • Run with: `streamlit run webapp.py

With a single import you can have a fully functional streamlit page with content (in this case, a "non implemented" message.

The PageView abstractions allow you to specify the page configuration via class initialization such as:

  • PageView(page_title="Demo", page_layout="wide", favicon_path="path/to/favicon.png")

You can provide a custom streamlit implementation to a page-view via:

  • Inline (discouraged) via the PageView.infline class constructor.
  • Inheritance (recommended) by providing an implementation for the view instance method.

Code Example: Using inheritance to provide a custom streamlit implementation.

import random
import datetime as dt

import streamlit as st

from rhdzmota.ext.streamlit_webapps.page_view import PageView

class WelcomeView(PageView):
    def view(self, **kwargs):
        st.markdown("# My Custom Webapp")
        with st.form(f"form-{self.refname}"):
            num_input = int(st.number_input("Num. of Options (input)", min_value=2, step=1))
            num_output = int(st.number_input("Num. of Winners", min_value=1, step=1))
            exclusive_select = st.checkbox("Exclusive Select")
            submitted = st.form_submit_button("Submit")

        if not submitted:
            return

        values = [f"Option-{i}" for i in range(num_input)]
        timestamp = dt.datetime.now().isoformat()
        sep = "\n* "
        result_prefix = f"Result (TS: {dt.datetime.now().isoformat()}) "
        if not exclusive_select:
            output = result_prefix + sep + sep.join(random.choices(values, k=num_output))
            return st.markdown(output)
        random.shuffle(values)
        output = result_prefix + sep + sep.join(values[:num_output] if num_output < num_input else values)
        return st.markdown(output)


if __name__ == "__main__":
    with WelcomeView(page_title="Demo", page_layout="wide") as page:
        page.view()

Linked Installation

Most extensions can be installed indirectly via the main package by providing an extra-dependency tag. Example:

$ pip install 'rhdzmota[ext.hello_world]'
  • General pattern: 'rhdzmota[ext.<extension-slug>]'

Standalone Installations

Package extensions can also be installed independently. For example:

$ pip install rhdzmota_extension_hello_world
  • General pattern: rhdzmota_extension_<extension-slug>

You can also install them locally:

$ EXT_BUILD_LOCAL=1 pip install -e extensions/hello_world
  • General pattern: EXT_BUILD_LOCAL=1 pip install -e path/to/extension

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page