Spaces:
Running
Running
File size: 5,796 Bytes
87c3140 |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
from typing import Union
import streamlit as st
import inspect
from importlib import import_module
from pathlib import Path
from typing import Any, Callable, Optional, TypeVar, Union, overload
try:
from streamlit.runtime.metrics_util import gather_metrics as _gather_metrics
except ImportError:
def _gather_metrics(name, func): # type: ignore
return func
F = TypeVar("F", bound=Callable[..., Any])
# Typing overloads here are actually required so that you can correctly (= with correct typing) use the decorator in different ways:
# 1) as a decorator without parameters @extra
# 2) as a decorator with parameters (@extra(foo="bar") but this also refers to empty parameters @extra()
# 3) as a function: extra(my_function)
@overload
def extra(
func: F,
) -> F:
...
@overload
def extra(
func: None = None,
) -> Callable[[F], F]:
...
def extra(
func: Optional[F] = None,
) -> Union[Callable[[F], F], F]:
if func:
filename = inspect.stack()[1].filename
submodule = Path(filename).parent.name
extra_name = "streamlit_extras." + submodule
module = import_module(extra_name)
if hasattr(module, "__funcs__"):
module.__funcs__ += [func] # type: ignore
else:
module.__funcs__ = [func] # type: ignore
profiling_name = f"{submodule}.{func.__name__}"
try:
return _gather_metrics(name=profiling_name, func=func)
except TypeError:
# Don't fail on streamlit==1.13.0, which only expects a callable
pass
def wrapper(f: F) -> F:
return f
return wrapper
@extra
def proportional_rain(
emoji1: str,
count1: int,
emoji2: str,
count2: int,
font_size: int = 64,
falling_speed: int = 5,
animation_length: Union[int, str] = "infinite"
):
"""
Creates a CSS animation where input emojis fall from top to bottom of the screen.
The proportion of emojis is based on the provided counts.
"""
if isinstance(animation_length, int):
animation_length = f"{animation_length}"
# CSS Code ...
st.write(
f"""
<style>
body {{
background: gray;
}}
.emoji {{
color: #777;
font-size: {font_size}px;
font-family: Arial;
// text-shadow: 0 0 5px #000;
}}
///*delete for no hover-effect*/
//.emoji:hover {{
// font-size: 60px;
// text-shadow: 5px 5px 5px white;
//}}
@-webkit-keyframes emojis-fall {{
0% {{
top: -10%;
}}
100% {{
top: 100%;
}}
}}
@-webkit-keyframes emojis-shake {{
0% {{
-webkit-transform: translateX(0px);
transform: translateX(0px);
}}
50% {{
-webkit-transform: translateX(20px);
transform: translateX(20px);
}}
100% {{
-webkit-transform: translateX(0px);
transform: translateX(0px);
}}
}}
@keyframes emojis-fall {{
0% {{
top: -10%;
}}
100% {{
top: 100%;
}}
}}
@keyframes emojis-shake {{
0% {{
transform: translateX(0px);
}}
25% {{
transform: translateX(15px);
}}
50% {{
transform: translateX(-15px);
}}
100% {{
transform: translateX(0px);
}}
}}
.emoji {{
position: fixed;
top: -10%;
z-index: 99999;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
-webkit-animation-name: emojis-fall, emojis-shake;
-webkit-animation-duration: 5s, 3s;
-webkit-animation-timing-function: linear, ease-in-out;
-webkit-animation-iteration-count: {animation_length}, {animation_length}; // overall length
-webkit-animation-play-state: running, running;
animation-name: emojis-fall, emojis-shake;
animation-duration: {falling_speed}s, 3s; // fall speed
animation-timing-function: linear, ease-in-out;
animation-iteration-count: {animation_length}, {animation_length}; // overall length
animation-play-state: running, running;
}}
.emoji:nth-of-type(0) {{
left: 1%;
-webkit-animation-delay: 0s, 0s;
animation-delay: 0s, 0s;
}}
.emoji:nth-of-type(1) {{
left: 10%;
-webkit-animation-delay: 1s, 1s;
animation-delay: 1s, 1s;
}}
.emoji:nth-of-type(2) {{
left: 20%;
-webkit-animation-delay: 6s, 0.5s;
animation-delay: 6s, 0.5s;
}}
.emoji:nth-of-type(3) {{
left: 30%;
-webkit-animation-delay: 4s, 2s;
animation-delay: 4s, 2s;
}}
.emoji:nth-of-type(4) {{
left: 40%;
-webkit-animation-delay: 2s, 2s;
animation-delay: 2s, 2s;
}}
.emoji:nth-of-type(5) {{
left: 50%;
-webkit-animation-delay: 8s, 3s;
animation-delay: 8s, 3s;
}}
.emoji:nth-of-type(6) {{
left: 60%;
-webkit-animation-delay: 6s, 2s;
animation-delay: 6s, 2s;
}}
.emoji:nth-of-type(7) {{
left: 70%;
-webkit-animation-delay: 2.5s, 1s;
animation-delay: 2.5s, 1s;
}}
.emoji:nth-of-type(8) {{
left: 80%;
-webkit-animation-delay: 1s, 0s;
animation-delay: 1s, 0s;
}}
.emoji:nth-of-type(9) {{
left: 90%;
-webkit-animation-delay: 3s, 1.5s;
animation-delay: 3s, 1.5s;
}}
</style>
""",
unsafe_allow_html=True,
)
# Create emoji strings based on counts
emoji_str1 = "".join([f'<div class="emoji">{emoji1}</div>' for _ in range(count1)])
emoji_str2 = "".join([f'<div class="emoji">{emoji2}</div>' for _ in range(count2)])
st.write(
f"""
<!--get emojis from https://getemoji.com-->
<div class="emojis">
{emoji_str1}
{emoji_str2}
</div>
""",
unsafe_allow_html=True,
) |