Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/all_types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## All Types Example

This is a nipanel example that demonstrates all supported data types

### Feature

- Demonstrates support for all data types

### Required Software

- Python 3.9 or later

### Usage

Run `poetry run examples/all_types/all_types.py`
23 changes: 23 additions & 0 deletions examples/all_types/all_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""example script for nipanel package demonstrating various data types."""

from pathlib import Path

from define_types import all_types_with_values

import nipanel

panel_script_path = Path(__file__).with_name("all_types_panel.py")
panel = nipanel.create_panel(panel_script_path)

print("Setting values")
for name, value in all_types_with_values.items():
print(f"{name:>15} {value}")
panel.set_value(name, value)

print()
print("Getting values")
for name in all_types_with_values.keys():
the_value = panel.get_value(name)
print(f"{name:>20} {the_value}")

print(f"Panel URL: {panel.panel_url}")
19 changes: 19 additions & 0 deletions examples/all_types/all_types_panel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""A Streamlit panel to demonstrate various types and their values."""

import streamlit as st
from define_types import all_types_with_values

import nipanel

panel = nipanel.initialize_panel()

st.title("All Types")

for name in all_types_with_values.keys():
col1, col2 = st.columns([0.4, 0.6])

with col1:
st.write(name)

with col2:
st.write(panel.get_value(name))
55 changes: 55 additions & 0 deletions examples/all_types/define_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Define types."""

import enum


class MyIntFlags(enum.IntFlag):
"""Example of an IntFlag enum."""

VALUE1 = 1
VALUE2 = 2
VALUE4 = 4


class MyIntEnum(enum.IntEnum):
"""Example of an IntEnum enum."""

VALUE10 = 10
VALUE20 = 20
VALUE30 = 30


class MyStrEnum(str, enum.Enum):
"""Example of a mixin string enum."""

VALUE1 = "value1"
VALUE2 = "value2"
VALUE3 = "value3"


all_types_with_values = {
# supported scalar types
"bool_scalar": True,
"bytes_scalar": b"robotext",
"float_scalar": 13.12,
"int_scalar": 42,
"str_scalar": "sample string",
# supported collection types
"bool_collection": [True, False, True],
"bytes_collection": [b"one", b"two", b"three"],
"float_collection": [1.1, 2.2, 3.3],
"int_collection": [1, 2, 3],
"str_collection": ["one", "two", "three"],
# supported enum and flag types
"intflags_scalar": MyIntFlags.VALUE1 | MyIntFlags.VALUE4,
"intenum_scalar": MyIntEnum.VALUE20,
"strenum_scalar": MyStrEnum.VALUE3,
"intflags_collection": [MyIntFlags.VALUE1, MyIntFlags.VALUE2, MyIntFlags.VALUE4],
"intenum_collection": [MyIntEnum.VALUE10, MyIntEnum.VALUE20, MyIntEnum.VALUE30],
"strenum_collection": [MyStrEnum.VALUE1, MyStrEnum.VALUE2, MyStrEnum.VALUE3],
# supported collections
"list": [1, 2, 3],
"tuple": (4, 5, 6),
"set": {7, 8, 9},
"frozenset": frozenset([10, 11, 12]),
}
58 changes: 0 additions & 58 deletions examples/placeholder.py

This file was deleted.

10 changes: 3 additions & 7 deletions examples/sample/sample.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""This example demonstrates how to open/update a Streamlit application using nipanel package."""

import pathlib
from pathlib import Path

import nipanel

script_path = pathlib.Path(__file__)
panel_script_path = str(script_path.with_name("sample_panel.py"))
panel_script_path = Path(__file__).with_name("sample_panel.py")
panel = nipanel.create_panel(panel_script_path)

panel = nipanel.StreamlitPanel(
panel_id="sample_panel",
streamlit_script_path=panel_script_path,
)
panel.set_value("sample_string", "Hello, World!")
panel.set_value("sample_int", 42)
panel.set_value("sample_float", 3.14)
Expand Down
10 changes: 8 additions & 2 deletions src/nipanel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

from nipanel._panel import Panel
from nipanel._streamlit_panel import StreamlitPanel
from nipanel._streamlit_panel_initializer import initialize_panel
from nipanel._streamlit_panel_initializer import create_panel, initialize_panel
from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor

__all__ = ["Panel", "StreamlitPanel", "StreamlitPanelValueAccessor", "initialize_panel"]
__all__ = [
"create_panel",
"initialize_panel",
"Panel",
"StreamlitPanel",
"StreamlitPanelValueAccessor",
]

# Hide that it was defined in a helper file
Panel.__module__ = __name__
Expand Down
25 changes: 25 additions & 0 deletions src/nipanel/_streamlit_panel_initializer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
from pathlib import Path
from typing import cast

import streamlit as st

from nipanel._streamlit_panel import StreamlitPanel
from nipanel._streamlit_panel_value_accessor import StreamlitPanelValueAccessor
from nipanel.streamlit_refresh import initialize_refresh_component

PANEL_ACCESSOR_KEY = "StreamlitPanelValueAccessor"


def create_panel(streamlit_script_path: Path) -> StreamlitPanel:
"""Create a Streamlit panel with the specified script path.
This function initializes a Streamlit panel using the provided script path.
The panel ID will be derived from the script path, which is expected to be a valid Streamlit script.
It is typically used to create a new panel instance for use in a Streamlit application.
Args:
streamlit_script_path: The file path of the Streamlit script to be used for the panel.
Returns:
A StreamlitPanel instance initialized with the given panel ID.
"""
path_str = str(streamlit_script_path)
if not path_str.endswith(".py"):
raise ValueError(
"The provided script path must be a valid Streamlit script ending with '.py'."
)

panel_id = path_str.replace("\\", "/").split("/")[-1].replace(".py", "")
return StreamlitPanel(panel_id, path_str)


def initialize_panel() -> StreamlitPanelValueAccessor:
"""Initialize and return the Streamlit panel value accessor.
Expand Down
Loading