U cf7@sTUddlZddlmZmZmZddlmZddlmZm Z m Z m Z m Z m Z mZmZddlmZddlmZmZmZmZddlmZdd lmZdd lmZdd lmZmZdd l m!Z!dd l"m#Z#GdddeZ$GdddeeZ%e&dkrPddl'Z'ddl(Z(ddl)m*Z*ddlm+Z+m Z m,Z,ddl-m.Z.ddlmZddl/m%Z%ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7eZe5ddddZ4e7d d!d"Z6e68d#d$d%d&e1d'd(e6d)d*d+d,d-e4e3d.g Z9e*e9Z:d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAgZ;e%edBHZe>fe?fe@dC<eAdDD]ZBe;eBeCe;ZDe;D]ZEeEeDkrTqBe(FdEe'GddFdkr~eHeIe:dGe''dHde=eDeEf<eCe=eCe;dkre=JeKe=Lde7dIdJZ6e6MdKe6MdLe6MdMe=ND]6\\ZOZPZQe68eOePe#eQdNeQdOkr dPndQdRqer?r@rArBrc Cs|dkstd||_|dk r"|nt|_||_d|_||_||_d|_d|_ t |_ d|_ ||_ d|_|rpdn||_d|_||_| |_| |_t|| d|_dS)Nrzrefresh_per_second must be > 0FT)rA)AssertionError _renderablerr;_screen _alt_screen_redirect_stdout_redirect_stderr_restore_stdout_restore_stderrrr/ ipy_widgetr=_startedr>_refresh_threadrrA_get_renderablerrB _live_render) r$rCr;r<r=rr>r?r@rArBr'r'r(r#3s,z Live.__init__r)cCs|jS)z'Check if live display has been started.)rMr,r'r'r( is_started[szLive.is_startedcCs |jdk r|n|j}|pdS)N)rOrEr$rCr'r'r(rB`s  zLive.get_renderable)r1rc Cs|j|jrW5QRdS|j|d|_|jrB|jd|_|jd||j ||rz | Wnt k r| YnX|j rt||j|_|jW5QRXdS)z|Start live rendering display. Args: refresh (bool, optional): Also refresh. Defaults to False. NTF)r/rMr;set_liverFset_alt_screenrG show_cursor_enable_redirect_iopush_render_hookr1 Exceptionr-r=rrrNstart)r$r1r'r'r(rZhs&    z Live.startc Cs|j|jsW5QRdS|jd|_|jrN|jdk rN|jd|_d|_|jz|j st|jjst|W5||j |j s|jj r|j |j d|j r|jd|jr|j s|j|j|jdk r|jr|jXW5QRXW5QRXdS)zStop live rendering display.NFvisibleT)r/rMr; clear_liver=rNr-rA_disable_redirect_iopop_render_hookrG is_terminallinerVrUr>controlrPrestore_cursorrLclose is_jupyterr1r,r'r'r(r-s0         z Live.stopcCs|j|jdk d|S)Nr1)rZrEr,r'r'r( __enter__szLive.__enter__)exc_typeexc_valexc_tbrcCs |dSr*)r-)r$rgrhrir'r'r(__exit__sz Live.__exit__cCst|jjs|jjrp|jr@ttjts@tj|_t dt|jtjt_|j rpttj tsptj |_ t dt|jtj t_ dS)z&Enable redirecting of stdout / stderr.r N) r;r_rdrH isinstancesysstdoutrrJr rIstderrrKr,r'r'r(rWszLive._enable_redirect_iocCs8|jrtd|jt_d|_|jr4td|jt_d|_dS)z'Disable redirecting of stdout / stderr.r N)rJr rlrmrKrnr,r'r'r(r]s zLive._disable_redirect_iocCs|}|jrt|S|S)zwGet the renderable that is being displayed Returns: RenderableType: Displayed renderable. )rBrGrrSr'r'r(rCszLive.renderablere)rCr1rc Cs>t|tr|j|}|j||_|r0|W5QRXdS)zUpdate the renderable that is being displayed Args: renderable (RenderableType): New renderable to use. refresh (bool, optional): Refresh the display. Defaults to False. N)rkstrr; render_strr/rEr1)r$rCr1r'r'r(updates   z Live.updatec Cs"|j|j|j|jjrzddlm}ddlm }Wn&t k rbddl }| dYnNX|j dkr||_ ||j |j $|j jdd|j|jjW5QRXnb|jjr|jjs|j|jtW5QRXn0|js|js|j|jtW5QRXW5QRXdS)z&Update the display of the Live Render.r)display)OutputNz(install "ipywidgets" for Jupyter supportT)r.)r/rPset_renderablerCr;rdIPython.displayrr ipywidgetsrs ImportErrorwarningswarnrL clear_outputprintr_is_dumb_terminalrrMr>)r$rrrsrxr'r'r(r1s0    z Live.refresh) renderablesrc Csj|j|j_|jjrN|j.|jr(tn|j}|f||jf}W5QRXn|j sf|j sf||jf}|S)z;Process renderables to restore cursor and display progress.) rArPr;is_interactiver/rGrhomeposition_cursorrMr>)r$r}resetr'r'r(process_renderabless   zLive.process_renderables)N)F)r3r4r5r6r rrboolr7rrr#propertyrQrBrZr-rfr BaseExceptionrrjrWr]rCrqr1r rrr'r'r'r(r#sZ (     r__main__)cycle)Dictr Tuple)Align)r)r)Panel)Rule)Syntax)Tablea~def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]: """Iterate and generate a tuple with a flag for last value.""" iter_values = iter(values) try: previous_value = next(iter_values) except StopIteration: return for value in iter_values: yield False, previous_value previous_value = value yield True, previous_valuepythonT) line_numbersZfoobarZbaz123zYou can make the terminal shorter and taller to see the live table hideText may be printed while the progress bars are rendering.z(In fact, [i]any[/i] renderable will workzSuch as [magenta]tables[/]...zPretty printed structures...ZexamplezPretty printed)typetextz Syntax...zGive it a try!ZSGDZMYRZEURUSDZAUDZJPYZCNHZHKDZCADZINRZDKKZGBPZRUBZNZDZMXNZIDRZTWDZTHBZVND)r;exchange_rate_dictdg? i@zExchange Rates)titlezSource CurrencyzDestination Currencyz Exchange Ratez.4fg?redgreen)style)Trl threadingrrrtypesrtypingrrrr r r r r rRrr;rrrrrar file_proxyrjupyterr live_renderrrr<rrrrrr3randomtime itertoolsrrralignrrpanelrrulersyntaxrtableradd_rowZprogress_renderablesZexamplesZ exchangesZ live_tablerror7__annotations__rangeindexlenZselect_exchangeZexchangesleeprandintlognextpoplistkeys add_columnitemssourcedestZ exchange_raterqcenterr'r'r'r(s (      m