File size: 973 Bytes
0efb4de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Literal

import streamlit as st

MARGINS = {
    "top": "0",
    "bottom": "0",
}

STICKY_CONTAINER_HTML = """

<style>

div[data-testid="stVerticalBlock"] div:has(div.fixed-header-{i}) {{

    position: sticky;

    {position}: {margin};

    background-color: white;

    z-index: {z};

}}

</style>

<div class='fixed-header-{i}'/>

""".strip()

# Not to apply the same style to multiple containers
count = 0


def sticky_container(

    *,

    height: int | None = None,

    border: bool | None = None,

    mode: Literal["top", "bottom"] = "top",

    margin: str | None = None,

    z:int |None=None

):
    if margin is None:
        margin = MARGINS[mode]

    global count
    html_code = STICKY_CONTAINER_HTML.format(position=mode, margin=margin, i=count,z=z)
    count += 1

    container = st.container(height=height, border=border)
    container.markdown(html_code, unsafe_allow_html=True)
    return container