L iqEddlmZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z mZmZmZmZddlmZdd lmZmZdd lmZdd lmZdd lmZmZdd lmZddl m!Z!ddl"m#Z#ddl$m%Z%e rddl&m'Z'ededefZ(egefZ)GddeZ*Gdde*Z+ d!ddd d"dZ,edd d#dZ-e d!dd d$dZ-ed d!dd d%d Z-y)&) annotationsN)abstractmethod)Callable)deepcopy)wraps) TYPE_CHECKINGAnyProtocolTypeVaroverload)handle_uncaught_app_exception)FragmentHandledExceptionFragmentStorageKeyError) ForwardMsg)gather_metrics)RerunException StopException)get_script_run_ctx)time_to_seconds)get_object_name)calc_md5) timedeltaF.)boundcleZdZdZedd dZed dZed dZed dZed dZ y)FragmentStorageaA key-value store for Fragments. Used to implement the @st.fragment decorator. We intentionally define this as its own protocol despite how generic it appears to be at first glance. The reason why is that, in any case where fragments aren't just stored as Python closures in memory, storing and retrieving Fragments will generally involve serializing and deserializing function bytecode, which is a tricky aspect to implementing FragmentStorages that won't generally appear with our other *Storage protocols. Nct)zfRemove all fragments saved in this FragmentStorage unless listed in new_fragment_ids. NotImplementedError)selfnew_fragment_idss `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/runtime/fragment.pyclearzFragmentStorage.clear;s "!ct)z.Returns the stored fragment for the given key.rr keys r"getzFragmentStorage.getB "!r$ct)z%Saves a fragment under the given key.rr r'values r"setzFragmentStorage.setGr)r$ct)z3Delete the fragment corresponding to the given key.rr&s r"deletezFragmentStorage.deleteLr)r$ct)z@Return whether the given key is present in this FragmentStorage.rr&s r"containszFragmentStorage.containsQr)r$Nr!zset[str] | NonereturnNoner'strr4Fragmentr'r7r,r8r4r5r'r7r4r5r'r7r4bool) __name__ __module__ __qualname____doc__rr#r(r-r/r1r$r"rr.sk"" """"""""r$rcBeZdZdZd dZd d dZd dZd dZddZddZ y)MemoryFragmentStoragezA simple, memory-backed implementation of FragmentStorage. MemoryFragmentStorage is just a wrapper around a plain Python dict that complies with the FragmentStorage protocol. ci|_yr2 _fragments)r s r"__init__zMemoryFragmentStorage.__init__cs /1r$Nc| t}t|jj}|D]}||vs|j|=yr2)r-listrFkeys)r r! fragment_idsfids r"r#zMemoryFragmentStorage.clearhsK  #"u DOO0023  )C**OOC( )r$cl |j|S#t$r}tt|d}~wwxYwr2rFKeyErrorrr7r r'es r"r(zMemoryFragmentStorage.getrs5 2??3' ' 2)#a&1 1 2s 3.3c"||j|<yr2rEr+s r"r-zMemoryFragmentStorage.setxs$r$cj |j|=y#t$r}tt|d}~wwxYwr2rNrPs r"r/zMemoryFragmentStorage.delete{s2 2$ 2)#a&1 1 2s  2-2c||jvSr2rEr&s r"r1zMemoryFragmentStorage.containssdoo%%r$)r4r5r2r3r6r9r:r;) r=r>r?r@rGr#r(r-r/r1rAr$r"rCrC\s% 2 )2 %2 &r$rC) run_everyadditional_hash_infoc,|dfd }|S|tdfd }tjt5|jj jt j|_ddd|S#1swY|SxYw)aContains the actual fragment logic. This function should be used by our internal functions that use fragments under-the-hood, so that fragment metrics are not tracked for those elements (note that the @gather_metrics annotation is only on the publicly exposed function) Nct|S)NfuncrV)fragment)frVs r"wrapperz_fragment..wrappers# r$c   ddlmt}|yt|jtj t jdt dj |j d  fd }|jj| rFt}t |j_|j_|j%||S)Nr)context_dg_stack.cRddl}td}| td|jr*t |_j t |jj |j} |_ |jk7r|j ntj}d}|5|j5 jd}|j r|j j"ngdd|_i } ddd|cddd||_ g|_S#t&t(f$rt*$r}t-|t/|d}~wwxYw#1swYXxYw#1swYnxYw ||_ g|_y#||_ g|_wxYw)NrT)suppress_warningz&ctx is None. This should never happen.rb) streamlitr RuntimeErrorfragment_ids_this_runrcursorsr-r!addcurrent_fragment_idactive_script_hashrun_with_active_hash contextlib nullcontext containerr(_cursor delta_pathcurrent_fragment_delta_pathrr Exceptionr r)stctxprev_fragment_idactive_hash_contextresult active_dgrQargsr`cursors_snapshotdg_stack_snapshot fragment_idinitialized_active_script_hashkwargsnon_optional_funcs r"wrapped_fragmentz1_fragment..wrap..wrapped_fragments " %d;C{"#KLL((''78  $$X.?%@A  $ $[ 1 #66 &1C #0 569O9OO,,-KL#//1$ ("" >>)9(<(<(>r(BI$-#4#4!* 1 1 < <%'!r ?#C; &7%G%GF >B"E""""H+;'24/'+) ""(> :!<#;1"==>1 > >""""""H+;'24/+;'24/s[8F<E: E.AD8 E: F8E+E&&E++E..E7 3E::F?FF&)r4r )$streamlit.delta_generator_singletonsr`rrrhr(rr>r_get_delta_path_strrkfragment_storager-rr auto_rerunintervalr}enqueue) rzrrurmsgr`r{r|r}r~rWrrVs `` @@@@@r"wrapz_fragment..wrapsI " ;#CKK0$%5%9%9%;< ++,Ao>O.P-QRcdfRgR{R{R}Q~@TU V *-)?)?&O 5O 5b   .>? ,C&5i&@CNN #)4CNN & KK  !!r$)r]rr4r)rzr rr r4r ) rrmsuppressAttributeError__dict__updateinspect signature __signature__)r[rVrWr^rrs `` @r" _fragmentrs |   k"k"Z   ^ ,B .778$../@A B K B Ks AB  BrVcyr2rArZs r"r\r\s r$cyr2rArZs r"r\r\s r$r\ct||S)aDecorator to turn a function into a fragment which can rerun independently of the full app. When a user interacts with an input widget created inside a fragment, Streamlit only reruns the fragment instead of the full app. If ``run_every`` is set, Streamlit will also rerun the fragment at the specified interval while the session is active, even if the user is not interacting with your app. To trigger an app rerun from inside a fragment, call ``st.rerun()`` directly. To trigger a fragment rerun from within itself, call ``st.rerun(scope="fragment")``. Any values from the fragment that need to be accessed from the wider app should generally be stored in Session State. When Streamlit element commands are called directly in a fragment, the elements are cleared and redrawn on each fragment rerun, just like all elements are redrawn on each app rerun. The rest of the app is persisted during a fragment rerun. When a fragment renders elements into externally created containers, the elements will not be cleared with each fragment rerun. Instead, elements will accumulate in those containers with each fragment rerun, until the next app rerun. Calling ``st.sidebar`` in a fragment is not supported. To write elements to the sidebar with a fragment, call your fragment function inside a ``with st.sidebar`` context manager. Fragment code can interact with Session State, imported modules, and other Streamlit elements created outside the fragment. Note that these interactions are additive across multiple fragment reruns. You are responsible for handling any side effects of that behavior. .. warning:: - Fragments can only contain widgets in their main body. Fragments can't render widgets to externally created containers. Parameters ---------- func: callable The function to turn into a fragment. run_every: int, float, timedelta, str, or None The time interval between automatic fragment reruns. This can be one of the following: - ``None`` (default). - An ``int`` or ``float`` specifying the interval in seconds. - A string specifying the time in a format supported by `Pandas' Timedelta constructor `_, e.g. ``"1d"``, ``"1.5 days"``, or ``"1h23s"``. - A ``timedelta`` object from `Python's built-in datetime library `_, e.g. ``timedelta(days=1)``. If ``run_every`` is ``None``, the fragment will only rerun from user-triggered events. Examples -------- The following example demonstrates basic usage of ``@st.fragment``. As an analogy, "inflating balloons" is a slow process that happens outside of the fragment. "Releasing balloons" is a quick process that happens inside of the fragment. >>> import streamlit as st >>> import time >>> >>> @st.fragment >>> def release_the_balloons(): >>> st.button("Release the balloons", help="Fragment rerun") >>> st.balloons() >>> >>> with st.spinner("Inflating balloons..."): >>> time.sleep(5) >>> release_the_balloons() >>> st.button("Inflate more balloons", help="Full rerun") .. output:: https://doc-fragment-balloons.streamlit.app/ height: 220px This next example demonstrates how elements both inside and outside of a fragement update with each app or fragment rerun. In this app, clicking "Rerun full app" will increment both counters and update all values displayed in the app. In contrast, clicking "Rerun fragment" will only increment the counter within the fragment. In this case, the ``st.write`` command inside the fragment will update the app's frontend, but the two ``st.write`` commands outside the fragment will not update the frontend. >>> import streamlit as st >>> >>> if "app_runs" not in st.session_state: >>> st.session_state.app_runs = 0 >>> st.session_state.fragment_runs = 0 >>> >>> @st.fragment >>> def my_fragment(): >>> st.session_state.fragment_runs += 1 >>> st.button("Rerun fragment") >>> st.write(f"Fragment says it ran {st.session_state.fragment_runs} times.") >>> >>> st.session_state.app_runs += 1 >>> my_fragment() >>> st.button("Rerun full app") >>> st.write(f"Full app says it ran {st.session_state.app_runs} times.") >>> st.write(f"Full app sees that fragment ran {st.session_state.fragment_runs} times.") .. output:: https://doc-fragment.streamlit.app/ height: 400px You can also trigger an app rerun from inside a fragment by calling ``st.rerun``. >>> import streamlit as st >>> >>> if "clicks" not in st.session_state: >>> st.session_state.clicks = 0 >>> >>> @st.fragment >>> def count_to_five(): >>> if st.button("Plus one!"): >>> st.session_state.clicks += 1 >>> if st.session_state.clicks % 5 == 0: >>> st.rerun() >>> return >>> >>> count_to_five() >>> st.header(f"Multiples of five clicks: {st.session_state.clicks // 5}") >>> >>> if st.button("Check click count"): >>> st.toast(f"## Total clicks: {st.session_state.clicks}") .. output:: https://doc-fragment-rerun.streamlit.app/ height: 400px r)rrZs r"r\r\&s` TY //r$r2)r[F | NonerV$int | float | timedelta | str | NonerWr7r4Callable[[F], F] | F)r[rrVrr4r)r[r5rVrr4zCallable[[F], F])r[rrVrr4r). __future__rrmrabcrcollections.abcrcopyr functoolsrtypingrr r r r streamlit.error_utilr streamlit.errorsrrstreamlit.proto.ForwardMsg_pb2rstreamlit.runtime.metrics_utilr/streamlit.runtime.scriptrunner_utils.exceptionsrr7streamlit.runtime.scriptrunner_utils.script_run_contextrstreamlit.time_utilrstreamlit.type_utilrstreamlit.utilrdatetimerrr8rrCrr\rAr$r"rs#$BB>N59W//#" CxS)* BG &"h&"\&&O&&TL7; " L L4L L  L^ 7;  4      7; 4   O07;O0 O04O0 O0O0r$