gui.py 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from __future__ import annotations
  12. import contextlib
  13. import importlib
  14. import json
  15. import math
  16. import os
  17. import re
  18. import sys
  19. import tempfile
  20. import time
  21. import typing as t
  22. import uuid
  23. import warnings
  24. from importlib import metadata, util
  25. from importlib.util import find_spec
  26. from inspect import currentframe, getabsfile, iscoroutinefunction, ismethod, ismodule
  27. from pathlib import Path
  28. from threading import Thread, Timer
  29. from types import FrameType, FunctionType, LambdaType, ModuleType, SimpleNamespace
  30. from urllib.parse import unquote, urlencode, urlparse
  31. import markdown as md_lib
  32. import tzlocal
  33. from flask import (
  34. Blueprint,
  35. Flask,
  36. g,
  37. has_app_context,
  38. jsonify,
  39. request,
  40. send_file,
  41. send_from_directory,
  42. )
  43. from werkzeug.utils import secure_filename
  44. import __main__ # noqa: F401
  45. from taipy.common.logger._taipy_logger import _TaipyLogger
  46. if util.find_spec("pyngrok"):
  47. from pyngrok import ngrok # type: ignore[reportMissingImports]
  48. from ._default_config import _default_stylekit, default_config
  49. from ._event_context_manager import _EventManager
  50. from ._hook import _Hooks
  51. from ._page import _Page
  52. from ._renderers import _EmptyPage
  53. from ._renderers._markdown import _TaipyMarkdownExtension
  54. from ._renderers.factory import _Factory
  55. from ._renderers.json import _TaipyJsonEncoder
  56. from ._renderers.utils import _get_columns_dict
  57. from ._warnings import TaipyGuiWarning, _warn
  58. from .builder import _ElementApiGenerator
  59. from .config import Config, ConfigParameter, _Config
  60. from .custom import Page as CustomPage
  61. from .custom.utils import get_current_resource_handler, is_in_custom_page_context
  62. from .data.content_accessor import _ContentAccessor
  63. from .data.data_accessor import _DataAccessors
  64. from .data.data_format import _DataFormat
  65. from .data.data_scope import _DataScopes
  66. from .extension.library import Element, ElementLibrary
  67. from .page import Page
  68. from .partial import Partial
  69. from .server import _Server
  70. from .state import State, _AsyncState, _GuiState
  71. from .types import _WsType
  72. from .utils import (
  73. _delscopeattr,
  74. _DoNotUpdate,
  75. _filter_locals,
  76. _function_name,
  77. _get_broadcast_var_name,
  78. _get_client_var_name,
  79. _get_css_var_value,
  80. _get_expr_var_name,
  81. _get_lambda_id,
  82. _get_module_name_from_frame,
  83. _get_non_existent_file_path,
  84. _get_page_from_module,
  85. _getscopeattr,
  86. _getscopeattr_drill,
  87. _hasscopeattr,
  88. _is_function,
  89. _is_in_notebook,
  90. _is_unnamed_function,
  91. _LocalsContext,
  92. _MapDict,
  93. _setscopeattr,
  94. _setscopeattr_drill,
  95. _TaipyBase,
  96. _TaipyContent,
  97. _TaipyContentHtml,
  98. _TaipyContentImage,
  99. _TaipyData,
  100. _TaipyLov,
  101. _TaipyLovValue,
  102. _to_camel_case,
  103. _variable_decode,
  104. is_debugging,
  105. )
  106. from .utils._adapter import _Adapter
  107. from .utils._bindings import _Bindings
  108. from .utils._evaluator import _Evaluator
  109. from .utils._variable_directory import _is_moduled_variable, _VariableDirectory
  110. from .utils.chart_config_builder import _build_chart_config
  111. from .utils.table_col_builder import _enhance_columns
  112. from .utils.threads import _invoke_async_callback
  113. class Gui:
  114. """Entry point for the Graphical User Interface generation.
  115. !!! note
  116. This class belongs to and is documented in the `taipy.gui` package, but it is
  117. accessible from the top `taipy` package to simplify its access, allowing to
  118. use:
  119. ```py
  120. from taipy import Gui
  121. ```
  122. """
  123. __root_page_name = "TaiPy_root_page"
  124. __env_filename = "taipy.gui.env"
  125. __UI_BLOCK_NAME = "TaipyUiBlockVar"
  126. __MESSAGE_GROUPING_NAME = "TaipyMessageGrouping"
  127. __ON_INIT_NAME = "TaipyOnInit"
  128. __ARG_CLIENT_ID = "client_id"
  129. __INIT_URL = "taipy-init"
  130. __JSX_URL = "taipy-jsx"
  131. __CONTENT_ROOT = "taipy-content"
  132. __UPLOAD_URL = "taipy-uploads"
  133. _EXTENSION_ROOT = "taipy-extension"
  134. __USER_CONTENT_URL = "taipy-user-content"
  135. __BROADCAST_G_ID = "taipy_broadcasting"
  136. __BRDCST_CALLBACK_G_ID = "taipy_brdcst_callback"
  137. __SELF_VAR = "__gui"
  138. __DO_NOT_UPDATE_VALUE = _DoNotUpdate()
  139. _HTML_CONTENT_KEY = "__taipy_html_content"
  140. __USER_CONTENT_CB = "custom_user_content_cb"
  141. __ROBOTO_FONT = "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
  142. __DOWNLOAD_ACTION = "__Taipy__download_csv"
  143. __DOWNLOAD_DELETE_ACTION = "__Taipy__download_delete_csv"
  144. __DEFAULT_FAVICON_URL = "https://raw.githubusercontent.com/Avaiga/taipy-assets/develop/favicon.png"
  145. __RE_HTML = re.compile(r"(.*?)\.html$")
  146. __RE_MD = re.compile(r"(.*?)\.md$")
  147. __RE_PY = re.compile(r"(.*?)\.py$")
  148. __RE_PAGE_NAME = re.compile(r"^[\w\-\/]+$")
  149. __reserved_routes: t.List[str] = [
  150. __INIT_URL,
  151. __JSX_URL,
  152. __CONTENT_ROOT,
  153. __UPLOAD_URL,
  154. _EXTENSION_ROOT,
  155. __USER_CONTENT_URL,
  156. ]
  157. __LOCAL_TZ = str(tzlocal.get_localzone())
  158. __extensions: t.Dict[str, t.List[ElementLibrary]] = {}
  159. __shared_variables: t.List[str] = []
  160. __content_providers: t.Dict[type, t.Callable[..., str]] = {}
  161. # See set_unsupported_data_converter()
  162. __unsupported_data_converter: t.Optional[t.Callable] = None
  163. def __init__(
  164. self,
  165. page: t.Optional[t.Union[str, Page]] = None,
  166. pages: t.Optional[dict] = None,
  167. css_file: t.Optional[str] = None,
  168. path_mapping: t.Optional[dict] = None,
  169. env_filename: t.Optional[str] = None,
  170. libraries: t.Optional[t.List[ElementLibrary]] = None,
  171. flask: t.Optional[Flask] = None,
  172. script_paths: t.Optional[t.Union[str, Path, t.List[t.Union[str, Path]]]] = None,
  173. ):
  174. """Initialize a new Gui instance.
  175. Arguments:
  176. page (Optional[Union[str, Page^]]): An optional `Page^` instance that is used
  177. when there is a single page in this interface, referenced as the *root*
  178. page (located at `/`).<br/>
  179. If *page* is a raw string and if it holds a path to a readable file then
  180. a `Markdown^` page is built from the content of that file.<br/>
  181. If *page* is a string that does not indicate a path to readable file then
  182. a `Markdown^` page is built from that string.<br/>
  183. Note that if *pages* is provided, those pages are added as well.
  184. pages (Optional[dict]): Used if you want to initialize this instance with a set
  185. of pages.<br/>
  186. The method `(Gui.)add_pages(pages)^` is called if *pages* is not None.
  187. You can find details on the possible values of this argument in the
  188. documentation for this method.
  189. css_file (Optional[str]): A pathname to a CSS file that gets used as a style sheet in
  190. all the pages.<br/>
  191. The default value is a file that has the same base name as the Python
  192. file defining the `main` function, sitting next to this Python file,
  193. with the `.css` extension.
  194. path_mapping (Optional[dict]): A dictionary that associates a URL prefix to
  195. a path in the server file system.<br/>
  196. If the assets of your application are located in */home/me/app_assets* and
  197. you want to access them using only '*assets*' in your application, you can
  198. set *path_mapping={"assets": "/home/me/app_assets"}*. If your application
  199. then requests the file *"/assets/images/logo.png"*, the server searches
  200. for the file *"/home/me/app_assets/images/logo.png"*.<br/>
  201. If empty or not defined, access through the browser to all resources under the directory
  202. of the main Python file is allowed.
  203. env_filename (Optional[str]): An optional file from which to load application
  204. configuration variables (see the
  205. [Configuration](../../../../../userman/advanced_features/configuration/gui-config.md#configuring-the-gui-instance)
  206. section of the User Manual for details.)<br/>
  207. The default value is "taipy.gui.env"
  208. libraries (Optional[List[ElementLibrary]]): An optional list of extension library
  209. instances that pages can reference.<br/>
  210. Using this argument is equivalent to calling `(Gui.)add_library()^` for each
  211. list's elements.
  212. flask (Optional[Flask]): An optional instance of a Flask application object.<br/>
  213. If this argument is set, this `Gui` instance will use the value of this argument
  214. as the underlying server. If omitted or set to None, this `Gui` will create its
  215. own Flask application instance and use it to serve the pages.
  216. script_paths (Optional[Union[str, Path, List[Union[str, Path]]]]):
  217. Specifies the path(s) to the JavaScript files or external resources used by the application.
  218. It can be a single URL or path, or a list containing multiple URLs and/or paths.
  219. """
  220. # store suspected local containing frame
  221. self.__frame = t.cast(FrameType, t.cast(FrameType, currentframe()).f_back)
  222. self.__default_module_name = _get_module_name_from_frame(self.__frame)
  223. self._set_css_file(css_file)
  224. # Set the assets URL path
  225. self.__script_files: list[str] = []
  226. self.__load_scripts(script_paths)
  227. # Preserve server config for server initialization
  228. if path_mapping is None:
  229. path_mapping = {}
  230. self._path_mapping = path_mapping
  231. self._flask = flask
  232. self._config = _Config()
  233. self.__content_accessor = None
  234. self.__accessors: t.Optional[_DataAccessors] = None
  235. self.__state: t.Optional[State] = None
  236. self.__bindings = _Bindings(self)
  237. self.__locals_context = _LocalsContext()
  238. self.__var_dir = _VariableDirectory(self.__locals_context)
  239. self.__evaluator: _Evaluator = None # type: ignore[assignment]
  240. self.__adapter = _Adapter()
  241. self.__directory_name_of_pages: t.List[str] = []
  242. self.__favicon: t.Optional[t.Union[str, Path]] = None
  243. # default actions
  244. self.on_action: t.Optional[t.Callable] = None
  245. """The function called when a control triggers an action, as the result of an end-user's interaction.
  246. It defaults to the `on_action()` global function defined in the Python
  247. application. If there is no such function, actions will not trigger anything.<br/>
  248. The signature of the *on_action* callback function must be:
  249. - *state*: the `State^` instance of the caller.
  250. - *id* (optional): a string representing the identifier of the caller.
  251. - *payload* (optional): an optional payload from the caller.
  252. """
  253. self.on_change: t.Optional[t.Callable] = None
  254. """The function called when a control modifies bound variables, as the result of an end-user's interaction.
  255. It defaults to the `on_change()` global function defined in the Python
  256. application. If there is no such function, user interactions will not trigger
  257. anything.<br/>
  258. The signature of the *on_change* callback function must be:
  259. - *state*: the `State^` instance of the caller.
  260. - *var_name* (str): The name of the variable that triggered this callback.
  261. - *var_value* (any): The new value for this variable.
  262. """
  263. self.on_init: t.Optional[t.Callable] = None
  264. """The function that is called on the first connection of a new client.
  265. It defaults to the `on_init()` global function defined in the Python
  266. application. If there is no such function, the first connection will not trigger
  267. anything.<br/>
  268. The signature of the *on_init* callback function must be:
  269. - *state*: the `State^` instance of the caller.
  270. """
  271. self.on_page_load: t.Optional[t.Callable] = None
  272. """This callback is invoked just before the page content is sent to the front-end.
  273. It defaults to the `on_page_load()` global function defined in the Python
  274. application. If there is no such function, page loads will not trigger
  275. anything.<br/>
  276. The signature of the *on_page_load* callback function must be:
  277. - *state*: the `State^` instance of the caller.
  278. - *page_name*: the name of the page that is being loaded.
  279. """
  280. self.on_navigate: t.Optional[t.Callable] = None
  281. """The function that is called when a page is requested.
  282. It defaults to the `on_navigate()` global function defined in the Python
  283. application. If there is no such function, page requests will not trigger
  284. anything.<br/>
  285. The signature of the *on_navigate* callback function must be:
  286. - *state*: the `State^` instance of the caller.
  287. - *page_name*: the name of the page the user is navigating to.
  288. - *params* (Optional): the query parameters provided in the URL.
  289. The *on_navigate* callback function must return the name of the page the user should be
  290. directed to.
  291. """
  292. self.on_exception: t.Optional[t.Callable] = None
  293. """The function that is called an exception occurs on user code.
  294. It defaults to the `on_exception()` global function defined in the Python
  295. application. If there is no such function, exceptions will not trigger
  296. anything.<br/>
  297. The signature of the *on_exception* callback function must be:
  298. - *state*: the `State^` instance of the caller.
  299. - *function_name*: the name of the function that raised the exception.
  300. - *exception*: the exception object that was raised.
  301. """
  302. self.on_status: t.Optional[t.Callable] = None
  303. """The function that is called when the status page is shown.
  304. It defaults to the `on_status()` global function defined in the Python
  305. application. If there is no such function, status page content shows only the status of the
  306. server.<br/>
  307. The signature of the *on_status* callback function must be:
  308. - *state*: the `State^` instance of the caller.
  309. It must return raw and valid HTML content as a string.
  310. """
  311. self.on_user_content: t.Optional[t.Callable] = None
  312. """The function that is called when a specific URL (generated by `get_user_content_url()^`) is requested.
  313. This callback function must return the raw HTML content of the page to be displayed on
  314. the browser.
  315. This attribute defaults to the `on_user_content()` global function defined in the Python
  316. application. If there is no such function, those specific URLs will not trigger
  317. anything.<br/>
  318. The signature of the *on_user_content* callback function must be:
  319. - *state*: the `State^` instance of the caller.
  320. - *path*: the path provided to the `get_user_content_url()^` to build the URL.
  321. - *parameters*: An optional dictionary as defined in the `get_user_content_url()^` call.
  322. The returned HTML content can therefore use both the variables stored in the *state*
  323. and the parameters provided in the call to `get_user_content_url()^`.
  324. """
  325. # sid from client_id
  326. self.__client_id_2_sid: t.Dict[str, t.Set[str]] = {}
  327. # Load default config
  328. self._flask_blueprint: t.List[Blueprint] = []
  329. self._config._load(default_config)
  330. # get taipy version
  331. try:
  332. gui_file = Path(__file__ or ".").resolve()
  333. with open(gui_file.parent / "version.json") as version_file:
  334. self.__version = json.load(version_file)
  335. except Exception as e: # pragma: no cover
  336. _warn("Cannot retrieve version.json file", e)
  337. self.__version = {}
  338. # Load Markdown extension
  339. # NOTE: Make sure, if you change this extension list, that the User Manual gets updated.
  340. # There's a section that explicitly lists these extensions in
  341. # docs/gui/pages.md#markdown-specifics
  342. self._markdown = md_lib.Markdown(
  343. extensions=[
  344. "fenced_code",
  345. "meta",
  346. "admonition",
  347. "sane_lists",
  348. "tables",
  349. "attr_list",
  350. "md_in_html",
  351. _TaipyMarkdownExtension(gui=self),
  352. ]
  353. )
  354. self.__event_manager = _EventManager()
  355. # Init Gui Hooks
  356. _Hooks()._init(self)
  357. if page:
  358. self.add_page(name=Gui.__root_page_name, page=page)
  359. if pages is not None:
  360. self.add_pages(pages)
  361. if env_filename is not None:
  362. self.__env_filename = env_filename
  363. if libraries is not None:
  364. for library in libraries:
  365. Gui.add_library(library)
  366. def __load_scripts(self, script_paths: t.Optional[t.Union[str, Path, t.List[t.Union[str, Path]]]]):
  367. if script_paths is None:
  368. return
  369. else:
  370. if isinstance(script_paths, (str, Path)):
  371. script_paths = [script_paths]
  372. for script_path in script_paths:
  373. if isinstance(script_path, str):
  374. parsed_url = urlparse(script_path)
  375. if parsed_url.netloc:
  376. self.__script_files.append(script_path)
  377. continue
  378. script_path = Path(script_path)
  379. if (
  380. isinstance(script_path, Path)
  381. and script_path.exists()
  382. and script_path.is_file()
  383. and script_path.suffix == ".js"
  384. ):
  385. self.__script_files.append(str(script_path))
  386. else:
  387. _warn(f"Script path '{script_path}' does not exist, is not a file, or is not a JavaScript file.")
  388. if not self.__script_files:
  389. _warn("No JavaScript files found in the specified script paths.")
  390. @staticmethod
  391. def add_library(library: ElementLibrary) -> None:
  392. """Add a custom visual element library.
  393. This application will be able to use custom visual elements defined in this library.
  394. Arguments:
  395. library: The custom visual element library to add to this application.
  396. Multiple libraries with the same name can be added. This allows to split multiple custom visual
  397. elements in several `ElementLibrary^` instances, but still refer to these elements with the same
  398. prefix in the page definitions.
  399. """
  400. if isinstance(library, ElementLibrary):
  401. _Factory.set_library(library)
  402. library_name = library.get_name()
  403. if library_name.isidentifier():
  404. libs = Gui.__extensions.get(library_name)
  405. if libs is None:
  406. Gui.__extensions[library_name] = [library]
  407. else:
  408. libs.append(library)
  409. _ElementApiGenerator().add_library(library)
  410. else:
  411. raise NameError(f"ElementLibrary passed to add_library() has an invalid name: '{library_name}'")
  412. else: # pragma: no cover
  413. raise RuntimeError(
  414. f"add_library() argument should be a subclass of ElementLibrary instead of '{type(library)}'"
  415. )
  416. @staticmethod
  417. def register_content_provider(content_type: type, content_provider: t.Callable[..., str]) -> None:
  418. """Add a custom content provider.
  419. The application can use custom content for the `part` block when its *content* property
  420. is set to an object with type *type*.
  421. Arguments:
  422. content_type: The type of the content that triggers the content provider.
  423. content_provider: The function that converts content of type *type* into an HTML string.
  424. """ # noqa: E501
  425. if Gui.__content_providers.get(content_type):
  426. _warn(f"The type {content_type} is already associated with a provider.")
  427. return
  428. if not _is_function(content_provider):
  429. _warn(f"The provider for {content_type} must be a function.")
  430. return
  431. Gui.__content_providers[content_type] = content_provider
  432. def __process_content_provider(self, state: State, path: str, query: t.Dict[str, str]):
  433. variable_name = query.get("variable_name")
  434. content = None
  435. if variable_name:
  436. content = _getscopeattr(self, variable_name)
  437. if isinstance(content, _TaipyContentHtml):
  438. content = content.get()
  439. provider_fn = Gui.__content_providers.get(type(content))
  440. if provider_fn is None:
  441. # try plotly
  442. if find_spec("plotly") and find_spec("plotly.graph_objs"):
  443. from plotly.graph_objs import Figure as PlotlyFigure # type: ignore[reportMissingImports]
  444. if isinstance(content, PlotlyFigure):
  445. def get_plotly_content(figure: PlotlyFigure):
  446. return figure.to_html()
  447. Gui.register_content_provider(PlotlyFigure, get_plotly_content)
  448. provider_fn = get_plotly_content
  449. if provider_fn is None:
  450. # try matplotlib
  451. if find_spec("matplotlib") and find_spec("matplotlib.figure"):
  452. from matplotlib.figure import Figure as MatplotlibFigure
  453. if isinstance(content, MatplotlibFigure):
  454. def get_matplotlib_content(figure: MatplotlibFigure):
  455. import base64
  456. from io import BytesIO
  457. buf = BytesIO()
  458. figure.savefig(buf, format="png")
  459. data = base64.b64encode(buf.getbuffer()).decode("ascii")
  460. return f'<img src="data:image/png;base64,{data}"/>'
  461. Gui.register_content_provider(MatplotlibFigure, get_matplotlib_content)
  462. provider_fn = get_matplotlib_content
  463. if _is_function(provider_fn):
  464. try:
  465. return t.cast(t.Callable, provider_fn)(t.cast(t.Any, content))
  466. except Exception as e:
  467. _warn(f"Error in content provider for type {str(type(content))}", e)
  468. return (
  469. '<div style="background:white;color:red;">'
  470. + (f"No valid provider for type {type(content).__name__}" if content else "Wrong context.")
  471. + "</div>"
  472. )
  473. @staticmethod
  474. def add_shared_variable(*names: str) -> None:
  475. """Add shared variables.
  476. The variables will be synchronized between all clients when updated.
  477. Note that only variables from the main module will be registered.
  478. This is a synonym for `(Gui.)add_shared_variables()^`.
  479. Arguments:
  480. names: The names of the variables that become shared, as a list argument.
  481. """
  482. for name in names:
  483. if name not in Gui.__shared_variables:
  484. Gui.__shared_variables.append(name)
  485. @staticmethod
  486. def add_shared_variables(*names: str) -> None:
  487. """Add shared variables.
  488. The variables will be synchronized between all clients when updated.
  489. Note that only variables from the main module will be registered.
  490. This is a synonym for `(Gui.)add_shared_variable()^`.
  491. Arguments:
  492. names: The names of the variables that become shared, as a list argument.
  493. """
  494. Gui.add_shared_variable(*names)
  495. def _get_shared_variables(self) -> t.List[str]:
  496. return self.__evaluator.get_shared_variables()
  497. @staticmethod
  498. def _clear_shared_variable() -> None:
  499. Gui.__shared_variables.clear()
  500. def __get_content_accessor(self):
  501. if self.__content_accessor is None:
  502. self.__content_accessor = _ContentAccessor(self._get_config("data_url_max_size", 50 * 1024))
  503. return self.__content_accessor
  504. def _bindings(self):
  505. return self.__bindings
  506. def _get_data_scope(self) -> SimpleNamespace:
  507. return self.__bindings._get_data_scope()
  508. def _get_data_scope_metadata(self) -> t.Dict[str, t.Any]:
  509. return self.__bindings._get_data_scope_metadata()
  510. def _get_all_data_scopes(self) -> t.Dict[str, SimpleNamespace]:
  511. return self.__bindings._get_all_scopes()
  512. def _get_config(self, name: ConfigParameter, default_value: t.Any) -> t.Any:
  513. return self._config._get_config(name, default_value)
  514. def _get_themes(self) -> t.Optional[t.Dict[str, t.Any]]:
  515. theme = self._get_config("theme", None)
  516. dark_theme = self._get_config("dark_theme", None)
  517. light_theme = self._get_config("light_theme", None)
  518. res = {}
  519. if theme:
  520. res["base"] = theme
  521. if dark_theme:
  522. res["dark"] = dark_theme
  523. if light_theme:
  524. res["light"] = light_theme
  525. return res if theme or dark_theme or light_theme else None
  526. def _bind(self, name: str, value: t.Any) -> None:
  527. self._bindings()._bind(name, value)
  528. def __get_state(self):
  529. return self.__state
  530. def _get_client_id(self) -> str:
  531. return (
  532. _DataScopes._GLOBAL_ID
  533. if self._bindings()._is_single_client()
  534. else getattr(g, Gui.__ARG_CLIENT_ID, "unknown id")
  535. )
  536. def __set_client_id_in_context(self, client_id: t.Optional[str] = None, force=False):
  537. if not client_id and request:
  538. client_id = request.args.get(Gui.__ARG_CLIENT_ID, "")
  539. if not client_id and (ws_client_id := getattr(g, "ws_client_id", None)):
  540. client_id = ws_client_id
  541. if not client_id and force:
  542. res = self._bindings()._get_or_create_scope("")
  543. client_id = res[0] if res[1] else None
  544. if client_id and request:
  545. if sid := getattr(request, "sid", None):
  546. sids = self.__client_id_2_sid.get(client_id, None)
  547. if sids is None:
  548. sids = set()
  549. self.__client_id_2_sid[client_id] = sids
  550. sids.add(sid)
  551. g.client_id = client_id
  552. def __is_var_modified_in_context(self, var_name: str, derived_vars: t.Set[str]) -> bool:
  553. modified_vars: t.Optional[t.Set[str]] = getattr(g, "modified_vars", None)
  554. der_vars: t.Optional[t.Set[str]] = getattr(g, "derived_vars", None)
  555. setattr(g, "update_count", getattr(g, "update_count", 0) + 1) # noqa: B010
  556. if modified_vars is None:
  557. modified_vars = set()
  558. g.modified_vars = modified_vars
  559. if der_vars is None:
  560. g.derived_vars = derived_vars
  561. else:
  562. der_vars.update(derived_vars)
  563. if var_name in modified_vars:
  564. return True
  565. modified_vars.add(var_name)
  566. return False
  567. def __clean_vars_on_exit(self) -> t.Optional[t.Set[str]]:
  568. update_count = getattr(g, "update_count", 0) - 1
  569. if update_count < 1:
  570. derived_vars: t.Set[str] = getattr(g, "derived_vars", set())
  571. delattr(g, "update_count")
  572. delattr(g, "modified_vars")
  573. delattr(g, "derived_vars")
  574. return derived_vars
  575. else:
  576. setattr(g, "update_count", update_count) # noqa: B010
  577. return None
  578. def _handle_connect(self):
  579. _Hooks().handle_connect(self)
  580. def _handle_disconnect(self):
  581. _Hooks()._handle_disconnect(self)
  582. if (sid := getattr(request, "sid", None)) and (st_to := self._get_config("state_retention_period", 0)) > 0:
  583. for cl_id, sids in self.__client_id_2_sid.items():
  584. if sid in sids:
  585. if len(sids) == 1:
  586. Timer(st_to, self._remove_state, [cl_id]).start()
  587. else:
  588. sids.remove(sid)
  589. return
  590. def _remove_state(self, client_id: str):
  591. if (sids := self.__client_id_2_sid.get(client_id, None)) and len(sids) == 1:
  592. try:
  593. del self.__client_id_2_sid[client_id]
  594. self._bindings()._delete_scope(client_id)
  595. except Exception as e:
  596. _warn(f"Unexpected error removing state {client_id}", e)
  597. def _manage_message(self, msg_type: _WsType, message: dict) -> None:
  598. try:
  599. client_id = None
  600. if msg_type == _WsType.CLIENT_ID.value:
  601. payload = message.get("payload", {})
  602. res = self._bindings()._get_or_create_scope(
  603. payload.get("id", "") if isinstance(payload, dict) else str(payload)
  604. )
  605. client_id = res[0] if res[1] else None
  606. if self._config.config.get("app_id", False):
  607. front_app_id = payload.get("app_id", None) if isinstance(payload, dict) else None
  608. if front_app_id is not None:
  609. self.__handle_ws_app_id({"name": message.get("name"), "payload": front_app_id})
  610. expected_client_id = client_id or message.get(Gui.__ARG_CLIENT_ID)
  611. self.__set_client_id_in_context(expected_client_id)
  612. g.ws_client_id = expected_client_id
  613. with self._set_locals_context(message.get("module_context") or None):
  614. with self._get_authorization():
  615. payload = message.get("payload", {})
  616. if msg_type == _WsType.UPDATE.value:
  617. self.__front_end_update(
  618. str(message.get("name")),
  619. payload.get("value"),
  620. message.get("propagate", True),
  621. payload.get("relvar"),
  622. payload.get("on_change"),
  623. )
  624. elif msg_type == _WsType.ACTION.value:
  625. self.__on_action(message.get("name"), message.get("payload"))
  626. elif msg_type == _WsType.DATA_UPDATE.value:
  627. self.__request_data_update(str(message.get("name")), message.get("payload"))
  628. elif msg_type == _WsType.REQUEST_UPDATE.value:
  629. self.__request_var_update(message.get("payload"))
  630. elif msg_type == _WsType.GET_MODULE_CONTEXT.value:
  631. self.__handle_ws_get_module_context(payload)
  632. elif msg_type == _WsType.GET_DATA_TREE.value:
  633. self.__handle_ws_get_data_tree()
  634. elif msg_type == _WsType.APP_ID.value:
  635. self.__handle_ws_app_id(message)
  636. elif msg_type == _WsType.GET_ROUTES.value:
  637. self.__handle_ws_get_routes()
  638. elif msg_type == _WsType.LOCAL_STORAGE.value:
  639. self.__handle_ws_local_storage(message)
  640. else:
  641. self._manage_external_message(msg_type, message)
  642. self.__send_ack(message.get("ack_id"))
  643. except Exception as e: # pragma: no cover
  644. if isinstance(e, AttributeError) and (name := message.get("name")):
  645. try:
  646. names = self._get_real_var_name(name)
  647. var_name = names[0] if isinstance(names, tuple) else names
  648. var_context = names[1] if isinstance(names, tuple) else None
  649. if var_name.startswith("tpec_"):
  650. var_name = var_name[5:]
  651. if var_name.startswith("TpExPr_"):
  652. var_name = var_name[7:]
  653. _warn(
  654. f"A problem occurred while resolving variable '{var_name}'"
  655. + (f" in module '{var_context}'." if var_context else ".")
  656. )
  657. except Exception as e1:
  658. _warn(f"Resolving name '{name}' failed", e1)
  659. else:
  660. _warn(f"Decoding Message has failed: {message}", e)
  661. # To be expanded by inheriting classes
  662. # this will be used to handle ws messages that is not handled by the base Gui class
  663. def _manage_external_message(self, msg_type: _WsType, message: dict) -> None:
  664. _Hooks()._manage_external_message(self, msg_type, message)
  665. def __front_end_update(
  666. self,
  667. var_name: str,
  668. value: t.Any,
  669. propagate=True,
  670. rel_var: t.Optional[str] = None,
  671. on_change: t.Optional[str] = None,
  672. ) -> None:
  673. if not var_name:
  674. return
  675. # Check if Variable is a managed type
  676. current_value = _getscopeattr_drill(self, self.__evaluator.get_hash_from_expr(var_name))
  677. if isinstance(current_value, _TaipyData):
  678. return
  679. elif rel_var and isinstance(current_value, _TaipyLovValue): # pragma: no cover
  680. lov_holder = _getscopeattr_drill(self, self.__evaluator.get_hash_from_expr(rel_var))
  681. if isinstance(lov_holder, _TaipyLov):
  682. if isinstance(value, (str, list)):
  683. val = value if isinstance(value, list) else [value]
  684. elt_4_ids = self.__adapter._get_elt_per_ids(lov_holder.get_name(), lov_holder.get())
  685. ret_val = [elt_4_ids.get(x, x) for x in val]
  686. if isinstance(value, list):
  687. value = ret_val
  688. elif ret_val:
  689. value = ret_val[0]
  690. elif isinstance(current_value, _TaipyBase):
  691. value = current_value.cast_value(value)
  692. self._update_var(
  693. var_name, value, propagate, current_value if isinstance(current_value, _TaipyBase) else None, on_change
  694. )
  695. def _update_var(
  696. self,
  697. var_name: str,
  698. value: t.Any,
  699. propagate=True,
  700. holder: t.Optional[_TaipyBase] = None,
  701. on_change: t.Optional[str] = None,
  702. forward: t.Optional[bool] = True,
  703. ) -> None:
  704. if holder:
  705. var_name = holder.get_name()
  706. hash_expr = self.__evaluator.get_hash_from_expr(var_name)
  707. derived_vars = {hash_expr}
  708. # set to broadcast mode if hash_expr is in shared_variable
  709. if hash_expr in self._get_shared_variables():
  710. self._set_broadcast()
  711. # Use custom attrsetter function to allow value binding for _MapDict
  712. if propagate:
  713. _setscopeattr_drill(self, hash_expr, value)
  714. # In case expression == hash (which is when there is only a single variable in expression)
  715. if var_name == hash_expr or hash_expr.startswith("tpec_"):
  716. derived_vars.update(self._re_evaluate_expr(var_name))
  717. elif holder:
  718. derived_vars.update(self._evaluate_holders(hash_expr))
  719. if forward:
  720. # if the variable has been evaluated then skip updating to prevent infinite loop
  721. var_modified = self.__is_var_modified_in_context(hash_expr, derived_vars)
  722. if not var_modified:
  723. self._call_on_change(
  724. var_name,
  725. value.get()
  726. if isinstance(value, _TaipyBase)
  727. else value._dict
  728. if isinstance(value, _MapDict)
  729. else value,
  730. on_change,
  731. )
  732. derived_modified = self.__clean_vars_on_exit()
  733. if derived_modified is not None:
  734. self.__send_var_list_update(list(derived_modified), var_name)
  735. def _get_real_var_name(self, var_name: str) -> t.Tuple[str, str]:
  736. if not var_name:
  737. return (var_name, var_name)
  738. # Handle holder prefix if needed
  739. if var_name.startswith(_TaipyBase._HOLDER_PREFIX):
  740. for hp in _TaipyBase._get_holder_prefixes():
  741. if var_name.startswith(hp):
  742. var_name = var_name[len(hp) :]
  743. break
  744. suffix_var_name = ""
  745. if "." in var_name:
  746. first_dot_index = var_name.index(".")
  747. suffix_var_name = var_name[first_dot_index + 1 :]
  748. var_name = var_name[:first_dot_index]
  749. var_name_decode, module_name = _variable_decode(self._get_expr_from_hash(var_name))
  750. current_context = self._get_locals_context()
  751. # #583: allow module resolution for var_name in current_context root_page context
  752. if (
  753. module_name
  754. and self._config.root_page
  755. and self._config.root_page._renderer
  756. and self._config.root_page._renderer._get_module_name() == module_name
  757. ):
  758. return f"{var_name_decode}.{suffix_var_name}" if suffix_var_name else var_name_decode, module_name
  759. if module_name == current_context:
  760. var_name = var_name_decode
  761. # only strict checking for cross-context linked variable when the context has been properly set
  762. elif self._has_set_context():
  763. if var_name not in self.__var_dir._var_head:
  764. raise NameError(f"Can't find matching variable for {var_name} on context: {current_context}")
  765. _found = False
  766. for k, v in self.__var_dir._var_head[var_name]:
  767. if v == current_context:
  768. var_name = k
  769. _found = True
  770. break
  771. if not _found: # pragma: no cover
  772. raise NameError(f"Can't find matching variable for {var_name} on context: {current_context}")
  773. return f"{var_name}.{suffix_var_name}" if suffix_var_name else var_name, current_context
  774. def _call_on_change(self, var_name: str, value: t.Any, on_change: t.Optional[str] = None):
  775. try:
  776. var_name, current_context = self._get_real_var_name(var_name)
  777. except Exception as e: # pragma: no cover
  778. _warn("", e)
  779. return
  780. on_change_fn = self._get_user_function(on_change) if on_change else None
  781. if not _is_function(on_change_fn):
  782. on_change_fn = self._get_user_function("on_change")
  783. if _is_function(on_change_fn):
  784. try:
  785. self._call_function_with_state(t.cast(t.Callable, on_change_fn), [var_name, value, current_context])
  786. except Exception as e: # pragma: no cover
  787. if not self._call_on_exception(on_change or "on_change", e):
  788. _warn(f"{on_change or 'on_change'}(): callback function raised an exception", e)
  789. def _get_content(self, var_name: str, value: t.Any, image: bool) -> t.Any:
  790. ret_value = self.__get_content_accessor().get_info(var_name, value, image)
  791. return f"/{Gui.__CONTENT_ROOT}/{ret_value[0]}" if isinstance(ret_value, tuple) else ret_value
  792. def __serve_content(self, path: str) -> t.Any:
  793. self.__set_client_id_in_context()
  794. parts = path.split("/")
  795. if len(parts) > 1:
  796. file_name = parts[-1]
  797. (dir_path, as_attachment) = self.__get_content_accessor().get_content_path(
  798. path[: -len(file_name) - 1], file_name, request.args.get("bypass")
  799. )
  800. if dir_path:
  801. return send_from_directory(str(dir_path), file_name, as_attachment=as_attachment)
  802. return ("", 404)
  803. def _get_user_content_url(
  804. self, path: t.Optional[str] = None, query_args: t.Optional[t.Dict[str, str]] = None
  805. ) -> t.Optional[str]:
  806. q_args = query_args or {}
  807. q_args.update({Gui.__ARG_CLIENT_ID: self._get_client_id()})
  808. return f"/{Gui.__USER_CONTENT_URL}/{path or 'TaIpY'}?{urlencode(q_args)}"
  809. def __serve_user_content(self, path: str) -> t.Any:
  810. self.__set_client_id_in_context()
  811. q_args: t.Dict[str, str] = {}
  812. q_args.update(request.args)
  813. q_args.pop(Gui.__ARG_CLIENT_ID, None)
  814. cb_function: t.Optional[t.Union[t.Callable, str]] = None
  815. cb_function_name = None
  816. if q_args.get(Gui._HTML_CONTENT_KEY):
  817. cb_function = self.__process_content_provider
  818. cb_function_name = cb_function.__name__
  819. else:
  820. cb_function_name = q_args.get(Gui.__USER_CONTENT_CB)
  821. if cb_function_name:
  822. cb_function = self._get_user_function(cb_function_name)
  823. if not _is_function(cb_function):
  824. parts = cb_function_name.split(".", 1)
  825. if len(parts) > 1:
  826. base = _getscopeattr(self, parts[0], None)
  827. if base and (meth := getattr(base, parts[1], None)):
  828. cb_function = meth
  829. else:
  830. base = self.__evaluator._get_instance_in_context(parts[0])
  831. if base and (meth := getattr(base, parts[1], None)):
  832. cb_function = meth
  833. if not _is_function(cb_function):
  834. _warn(f"{cb_function_name}() callback function has not been defined.")
  835. cb_function = None
  836. if cb_function is None:
  837. cb_function_name = "on_user_content"
  838. if hasattr(self, cb_function_name) and _is_function(self.on_user_content):
  839. cb_function = self.on_user_content
  840. else:
  841. _warn("on_user_content() callback function has not been defined.")
  842. if _is_function(cb_function):
  843. try:
  844. args: t.List[t.Any] = []
  845. if path:
  846. args.append(path)
  847. if len(q_args):
  848. args.append(q_args)
  849. ret = self._call_function_with_state(t.cast(t.Callable, cb_function), args)
  850. if ret is None:
  851. _warn(f"{cb_function_name}() callback function must return a value.")
  852. else:
  853. return (ret, 200)
  854. except Exception as e: # pragma: no cover
  855. if not self._call_on_exception(cb_function_name, e):
  856. _warn(f"{cb_function_name}() callback function raised an exception", e)
  857. return ("", 404)
  858. def __serve_extension(self, path: str) -> t.Any:
  859. parts = path.split("/")
  860. last_error = ""
  861. resource_name = None
  862. if len(parts) > 1:
  863. libs = Gui.__extensions.get(parts[0], [])
  864. for library in libs:
  865. try:
  866. resource_name = library.get_resource("/".join(parts[1:]))
  867. if resource_name:
  868. return send_file(resource_name)
  869. except Exception as e:
  870. last_error = f"\n{e}" # Check if the resource is served by another library with the same name
  871. _warn(f"Resource '{resource_name or path}' not accessible for library '{parts[0]}'{last_error}")
  872. return ("", 404)
  873. def __get_version(self) -> str:
  874. return f"{self.__version.get('major', 0)}.{self.__version.get('minor', 0)}.{self.__version.get('patch', 0)}"
  875. def __append_libraries_to_status(self, status: t.Dict[str, t.Any]):
  876. libraries: t.Dict[str, t.Any] = {}
  877. for libs_list in self.__extensions.values():
  878. for lib in libs_list:
  879. if not isinstance(lib, ElementLibrary):
  880. continue
  881. libs = libraries.get(lib.get_name())
  882. if libs is None:
  883. libs = []
  884. libraries[lib.get_name()] = libs
  885. elements: t.List[t.Dict[str, str]] = []
  886. libs.append({"js module": lib.get_js_module_name(), "elements": elements})
  887. for element_name, elt in lib.get_elements().items():
  888. if not isinstance(elt, Element):
  889. continue
  890. elt_dict = {"name": element_name}
  891. if hasattr(elt, "_render_xhtml"):
  892. elt_dict["render function"] = elt._render_xhtml.__code__.co_name
  893. else:
  894. elt_dict["react name"] = elt._get_js_name(element_name)
  895. elements.append(elt_dict)
  896. status.update({"libraries": libraries})
  897. def _serve_status(self, template: Path) -> t.Dict[str, t.Dict[str, str]]:
  898. base_json: t.Dict[str, t.Any] = {"user_status": str(self.__call_on_status() or "")}
  899. if self._get_config("extended_status", False):
  900. base_json.update(
  901. {
  902. "flask_version": str(metadata.version("flask") or ""),
  903. "backend_version": self.__get_version(),
  904. "host": f"{self._get_config('host', 'localhost')}:{self._get_config('port', 'default')}",
  905. "python_version": sys.version,
  906. }
  907. )
  908. self.__append_libraries_to_status(base_json)
  909. try:
  910. base_json.update(json.loads(template.read_text()))
  911. except Exception as e: # pragma: no cover
  912. _warn(f"Exception raised reading JSON in '{template}'", e)
  913. return {"gui": base_json}
  914. def __upload_files(self):
  915. self.__set_client_id_in_context()
  916. on_upload_action = request.form.get("on_action", None)
  917. var_name = t.cast(str, request.form.get("var_name", None))
  918. if not var_name and not on_upload_action:
  919. _warn("upload files: No var name")
  920. return ("upload files: No var name", 400)
  921. context = request.form.get("context", None)
  922. upload_data = request.form.get("upload_data", None)
  923. multiple = "multiple" in request.form and request.form["multiple"] == "True"
  924. # File parsing and checks
  925. file = request.files.get("blob", None)
  926. if not file:
  927. _warn("upload files: No file part")
  928. return ("upload files: No file part", 400)
  929. # If the user does not select a file, the browser submits an
  930. # empty file without a filename.
  931. if file.filename == "":
  932. _warn("upload files: No selected file")
  933. return ("upload files: No selected file", 400)
  934. # Path parsing and checks
  935. path = request.form.get("path", "")
  936. suffix = ""
  937. complete = True
  938. part = 0
  939. if "total" in request.form:
  940. total = int(request.form["total"])
  941. if total > 1 and "part" in request.form:
  942. part = int(request.form["part"])
  943. suffix = f".part.{part}"
  944. complete = part == total - 1
  945. # Extract upload path (when single file is selected, path="" does not change the path)
  946. upload_root = os.path.abspath(self._get_config("upload_folder", tempfile.gettempdir()))
  947. upload_path = os.path.abspath(os.path.join(upload_root, os.path.dirname(path)))
  948. if upload_path.startswith(upload_root):
  949. upload_path = Path(upload_path).resolve()
  950. os.makedirs(upload_path, exist_ok=True)
  951. # Save file into upload_path directory
  952. file_path = _get_non_existent_file_path(upload_path, secure_filename(file.filename))
  953. file.save(os.path.join(upload_path, (file_path.name + suffix)))
  954. else:
  955. _warn(f"upload files: Path {path} points outside of upload root.")
  956. return ("upload files: Path part points outside of upload root.", 400)
  957. if complete:
  958. if part > 0:
  959. try:
  960. with open(file_path, "wb") as grouped_file:
  961. for nb in range(part + 1):
  962. part_file_path = upload_path / f"{file_path.name}.part.{nb}"
  963. with open(part_file_path, "rb") as part_file:
  964. grouped_file.write(part_file.read())
  965. # remove file_path after it is merged
  966. part_file_path.unlink()
  967. except EnvironmentError as ee: # pragma: no cover
  968. _warn(f"Cannot group file after chunk upload for {file.filename}", ee)
  969. return (f"Cannot group file after chunk upload for {file.filename}", 500)
  970. # notify the file is uploaded
  971. newvalue = str(file_path)
  972. if multiple and var_name:
  973. value = _getscopeattr(self, var_name)
  974. if not isinstance(value, t.List):
  975. value = [] if value is None else [value]
  976. value.append(newvalue)
  977. newvalue = value
  978. with self._set_locals_context(context):
  979. if on_upload_action:
  980. data = {}
  981. if upload_data:
  982. try:
  983. data = json.loads(upload_data)
  984. except Exception:
  985. pass
  986. data["path"] = file_path
  987. file_fn = self._get_user_function(on_upload_action)
  988. if not _is_function(file_fn):
  989. file_fn = _getscopeattr(self, on_upload_action)
  990. if _is_function(file_fn):
  991. self._call_function_with_state(t.cast(t.Callable, file_fn), ["file_upload", {"args": [data]}])
  992. else:
  993. setattr(self._bindings(), var_name, newvalue)
  994. return ("", 200)
  995. def __send_var_list_update( # noqa C901
  996. self,
  997. modified_vars: t.List[str],
  998. front_var: t.Optional[str] = None,
  999. ):
  1000. ws_dict = {}
  1001. is_custom_page = is_in_custom_page_context()
  1002. values = {v: _getscopeattr_drill(self, v) for v in modified_vars if is_custom_page or _is_moduled_variable(v)}
  1003. if not values:
  1004. return
  1005. for k, v in values.items():
  1006. if isinstance(v, (_TaipyData, _TaipyContentHtml)) and v.get_name() in modified_vars:
  1007. modified_vars.remove(v.get_name())
  1008. elif isinstance(v, _DoNotUpdate):
  1009. modified_vars.remove(k)
  1010. for _var in modified_vars:
  1011. newvalue = values.get(_var)
  1012. resource_handler = get_current_resource_handler()
  1013. custom_page_filtered_types = resource_handler.data_layer_supported_types if resource_handler else ()
  1014. if isinstance(newvalue, (_TaipyData)) or isinstance(newvalue, custom_page_filtered_types):
  1015. newvalue = {"__taipy_refresh": True}
  1016. else:
  1017. if isinstance(newvalue, (_TaipyContent, _TaipyContentImage)):
  1018. ret_value = self.__get_content_accessor().get_info(
  1019. t.cast(str, front_var), newvalue.get(), isinstance(newvalue, _TaipyContentImage)
  1020. )
  1021. if isinstance(ret_value, tuple):
  1022. newvalue = f"/{Gui.__CONTENT_ROOT}/{ret_value[0]}"
  1023. else:
  1024. newvalue = ret_value
  1025. elif isinstance(newvalue, _TaipyContentHtml):
  1026. newvalue = self._get_user_content_url(
  1027. None, {"variable_name": str(_var), Gui._HTML_CONTENT_KEY: str(time.time())}
  1028. )
  1029. elif isinstance(newvalue, (_TaipyLov, _TaipyLovValue)):
  1030. newvalue = self.__adapter.run(
  1031. newvalue.get_name(), newvalue.get(), id_only=isinstance(newvalue, _TaipyLovValue)
  1032. )
  1033. elif isinstance(newvalue, _TaipyBase):
  1034. newvalue = newvalue.get()
  1035. # Skip in taipy-gui, available in custom frontend
  1036. if isinstance(newvalue, (dict, _MapDict)) and not is_in_custom_page_context():
  1037. continue
  1038. if isinstance(newvalue, float) and math.isnan(newvalue):
  1039. # do not let NaN go through json, it is not handle well (dies silently through websocket)
  1040. newvalue = None
  1041. if newvalue is not None and not isinstance(newvalue, str):
  1042. debug_warnings: t.List[warnings.WarningMessage] = []
  1043. with warnings.catch_warnings(record=True) as warns:
  1044. warnings.resetwarnings()
  1045. json.dumps(newvalue, cls=_TaipyJsonEncoder)
  1046. if len(warns):
  1047. keep_value = True
  1048. for w in warns:
  1049. if is_debugging():
  1050. debug_warnings.append(w)
  1051. if w.category is not DeprecationWarning and w.category is not PendingDeprecationWarning:
  1052. keep_value = False
  1053. break
  1054. if not keep_value:
  1055. # do not send data that is not serializable
  1056. continue
  1057. for w in debug_warnings:
  1058. warnings.warn(w.message, w.category) # noqa: B028
  1059. ws_dict[_var] = newvalue
  1060. # TODO: What if value == newvalue?
  1061. self.__send_ws_update_with_dict(ws_dict)
  1062. def __update_state_context(self, payload: dict):
  1063. # apply state context if any
  1064. state_context = payload.get("state_context")
  1065. if isinstance(state_context, dict):
  1066. for var, val in state_context.items():
  1067. self._update_var(var, val, True, forward=False)
  1068. @staticmethod
  1069. def set_unsupported_data_converter(converter: t.Optional[t.Callable[[t.Any], t.Any]]) -> None:
  1070. """Set a custom converter for unsupported data types.
  1071. This function allows specifying a custom conversion function for data types that cannot
  1072. be serialized automatically. When Taipy GUI encounters an unsupported type in the data
  1073. being serialized, it will invoke the provided *converter* function. The returned value
  1074. from this function will then be serialized if possible.
  1075. Arguments:
  1076. converter: A function that converts a value with an unsupported data type (the only
  1077. parameter to the function) into data with a supported data type (the returned value
  1078. from the function).</br>
  1079. If set to `None`, it removes any existing converter.
  1080. """
  1081. Gui.__unsupported_data_converter = converter
  1082. @staticmethod
  1083. def _convert_unsupported_data(value: t.Any) -> t.Optional[t.Any]:
  1084. """
  1085. Handles unsupported data by invoking the converter if there is one.
  1086. Arguments:
  1087. value: The unsupported data encountered.
  1088. Returns:
  1089. The transformed data or None if no transformation is possible.
  1090. """
  1091. try:
  1092. return Gui.__unsupported_data_converter(value) if _is_function(Gui.__unsupported_data_converter) else None # type: ignore
  1093. except Exception as e:
  1094. _warn(f"Error transforming data: {str(e)}")
  1095. return None
  1096. def __request_data_update(self, var_name: str, payload: t.Any) -> None:
  1097. # Use custom attrgetter function to allow value binding for _MapDict
  1098. newvalue = _getscopeattr_drill(self, var_name)
  1099. resource_handler = get_current_resource_handler()
  1100. custom_page_filtered_types = resource_handler.data_layer_supported_types if resource_handler else ()
  1101. if not isinstance(newvalue, _TaipyData) and isinstance(newvalue, custom_page_filtered_types):
  1102. newvalue = _TaipyData(newvalue, "")
  1103. if isinstance(newvalue, _TaipyData):
  1104. ret_payload = None
  1105. if isinstance(payload, dict):
  1106. self.__update_state_context(payload)
  1107. lib_name = payload.get("library")
  1108. if isinstance(lib_name, str):
  1109. libs = self.__extensions.get(lib_name, [])
  1110. for lib in libs:
  1111. user_var_name = var_name
  1112. try:
  1113. with contextlib.suppress(NameError):
  1114. # ignore name error and keep var_name
  1115. user_var_name = self._get_real_var_name(var_name)[0]
  1116. ret_payload = lib.get_data(lib_name, payload, user_var_name, newvalue)
  1117. if ret_payload:
  1118. break
  1119. except Exception as e: # pragma: no cover
  1120. _warn(
  1121. f"Exception raised in '{lib_name}.get_data({lib_name}, payload, {user_var_name}, value)'", # noqa: E501
  1122. e,
  1123. )
  1124. if not isinstance(ret_payload, dict):
  1125. ret_payload = self._get_accessor().get_data(var_name, newvalue, payload)
  1126. self.__send_ws_update_with_dict({var_name: ret_payload})
  1127. def __request_var_update(self, payload: t.Any):
  1128. if isinstance(payload, dict) and isinstance(payload.get("names"), list):
  1129. self.__update_state_context(payload)
  1130. if payload.get("refresh", False):
  1131. # refresh vars
  1132. for _var in t.cast(list, payload.get("names")):
  1133. val = _getscopeattr_drill(self, _var)
  1134. self._refresh_expr(
  1135. val.get_name() if isinstance(val, _TaipyBase) else _var,
  1136. val if isinstance(val, _TaipyBase) else None,
  1137. )
  1138. self.__send_var_list_update(payload["names"])
  1139. def __handle_ws_get_module_context(self, payload: t.Any):
  1140. if isinstance(payload, dict):
  1141. page_path = str(payload.get("path"))
  1142. if page_path in {"/", ""}:
  1143. page_path = Gui.__root_page_name
  1144. # Get Module Context
  1145. if mc := self._get_page_context(page_path):
  1146. page_renderer = t.cast(_Page, self._get_page(page_path))._renderer
  1147. self._bind_custom_page_variables(t.cast(t.Any, page_renderer), self._get_client_id())
  1148. # get metadata if there is one
  1149. metadata: t.Dict[str, t.Any] = {}
  1150. if hasattr(page_renderer, "_metadata"):
  1151. metadata = getattr(page_renderer, "_metadata", {})
  1152. meta_return = json.dumps(metadata, cls=_TaipyJsonEncoder) if metadata else None
  1153. self.__send_ws(
  1154. {
  1155. "type": _WsType.GET_MODULE_CONTEXT.value,
  1156. "payload": {"context": mc, "metadata": meta_return},
  1157. },
  1158. send_back_only=True,
  1159. )
  1160. def __get_variable_tree(self, data: t.Dict[str, t.Any]):
  1161. # Module Context -> Variable -> Variable data (name, type, initial_value)
  1162. variable_tree: t.Dict[str, t.Dict[str, t.Dict[str, t.Any]]] = {}
  1163. # Types of data to be handled by the data layer and filtered out here
  1164. resource_handler = get_current_resource_handler()
  1165. filtered_value_types = resource_handler.data_layer_supported_types if resource_handler else ()
  1166. for k, v in data.items():
  1167. if isinstance(v, _TaipyBase):
  1168. data[k] = v.get()
  1169. var_name, var_module_name = _variable_decode(k)
  1170. if var_module_name == "" or var_module_name is None:
  1171. var_module_name = "__main__"
  1172. if var_module_name not in variable_tree:
  1173. variable_tree[var_module_name] = {}
  1174. data_update = isinstance(v, filtered_value_types)
  1175. value = None if data_update else data[k]
  1176. # if _is_moduled_variable(k):
  1177. variable_tree[var_module_name][var_name] = {
  1178. "type": type(v).__name__,
  1179. "value": value,
  1180. "encoded_name": k,
  1181. "data_update": data_update,
  1182. }
  1183. return variable_tree
  1184. def __handle_ws_get_data_tree(self):
  1185. # Get Variables
  1186. self.__pre_render_pages()
  1187. data = {
  1188. k: v
  1189. for k, v in vars(self._get_data_scope()).items()
  1190. if not k.startswith("_")
  1191. and not callable(v)
  1192. and "TpExPr" not in k
  1193. and not isinstance(v, (ModuleType, FunctionType, LambdaType, type, Page))
  1194. }
  1195. function_data = {
  1196. k: v
  1197. for k, v in vars(self._get_data_scope()).items()
  1198. if not k.startswith("_") and "TpExPr" not in k and isinstance(v, (FunctionType, LambdaType))
  1199. }
  1200. self.__send_ws(
  1201. {
  1202. "type": _WsType.GET_DATA_TREE.value,
  1203. "payload": {
  1204. "variable": self.__get_variable_tree(data),
  1205. "function": self.__get_variable_tree(function_data),
  1206. },
  1207. },
  1208. send_back_only=True,
  1209. )
  1210. def __handle_ws_app_id(self, message: t.Any):
  1211. if not isinstance(message, dict):
  1212. return
  1213. name = message.get("name", "")
  1214. payload = message.get("payload", "")
  1215. app_id = id(self)
  1216. if payload == app_id:
  1217. return
  1218. self.__send_ws(
  1219. {
  1220. "type": _WsType.APP_ID.value,
  1221. "payload": {"name": name, "id": app_id},
  1222. },
  1223. send_back_only=True,
  1224. )
  1225. def __handle_ws_get_routes(self):
  1226. routes = (
  1227. [[self._config.root_page._route, t.cast(t.Any, self._config.root_page._renderer).page_type]]
  1228. if self._config.root_page
  1229. else []
  1230. )
  1231. routes += [
  1232. [page._route, t.cast(t.Any, page._renderer).page_type]
  1233. for page in self._config.pages
  1234. if page._route != Gui.__root_page_name
  1235. ]
  1236. self.__send_ws(
  1237. {
  1238. "type": _WsType.GET_ROUTES.value,
  1239. "payload": routes,
  1240. },
  1241. send_back_only=True,
  1242. )
  1243. def __handle_ws_local_storage(self, message: t.Any):
  1244. if not isinstance(message, dict):
  1245. return
  1246. payload = message.get("payload", None)
  1247. scope_meta_ls = self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE]
  1248. if payload is None:
  1249. return
  1250. for key, value in payload.items():
  1251. if value is not None and scope_meta_ls.get(key) != value:
  1252. scope_meta_ls[key] = value
  1253. def _query_local_storage(self, *keys: str) -> t.Optional[t.Union[str, t.Dict[str, str]]]:
  1254. if not keys:
  1255. return None
  1256. if len(keys) == 1:
  1257. if keys[0] in self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE]:
  1258. return self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE][keys[0]]
  1259. return None
  1260. # case of multiple keys
  1261. ls_items = {}
  1262. for key in keys:
  1263. if key in self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE]:
  1264. ls_items[key] = self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE][key]
  1265. return ls_items
  1266. def __send_ws(self, payload: dict, allow_grouping=True, send_back_only=False) -> None:
  1267. grouping_message = self.__get_message_grouping() if allow_grouping else None
  1268. if grouping_message is None:
  1269. try:
  1270. self._server._ws.emit(
  1271. "message",
  1272. payload,
  1273. to=t.cast(str, self.__get_ws_receiver(send_back_only)),
  1274. )
  1275. time.sleep(0.001)
  1276. except Exception as e: # pragma: no cover
  1277. _warn(f"Exception raised in WebSocket communication in '{self.__frame.f_code.co_name}'", e)
  1278. else:
  1279. grouping_message.append(payload)
  1280. def __broadcast_ws(self, payload: dict, client_id: t.Optional[str] = None):
  1281. try:
  1282. to = list(self.__get_sids(client_id)) if client_id else []
  1283. self._server._ws.emit("message", payload, to=t.cast(str, to) if to else None, include_self=True)
  1284. time.sleep(0.001)
  1285. except Exception as e: # pragma: no cover
  1286. _warn(f"Exception raised in WebSocket communication in '{self.__frame.f_code.co_name}'", e)
  1287. def __send_ack(self, ack_id: t.Optional[str]) -> None:
  1288. if ack_id:
  1289. try:
  1290. self._server._ws.emit(
  1291. "message",
  1292. {"type": _WsType.ACKNOWLEDGEMENT.value, "id": ack_id},
  1293. to=t.cast(str, self.__get_ws_receiver(True)),
  1294. )
  1295. time.sleep(0.001)
  1296. except Exception as e: # pragma: no cover
  1297. _warn(f"Exception raised in WebSocket communication (send ack) in '{self.__frame.f_code.co_name}'", e)
  1298. def _send_ws_id(self, id: str) -> None:
  1299. self.__send_ws(
  1300. {
  1301. "type": _WsType.CLIENT_ID.value,
  1302. "id": id,
  1303. },
  1304. allow_grouping=False,
  1305. )
  1306. def __send_ws_download(self, content: str, name: str, on_action: str) -> None:
  1307. self.__send_ws(
  1308. {"type": _WsType.DOWNLOAD_FILE.value, "content": content, "name": name, "onAction": on_action},
  1309. send_back_only=True,
  1310. )
  1311. def __send_ws_alert(
  1312. self, type: str, message: str, system_notification: bool, duration: int, notification_id: t.Optional[str] = None
  1313. ) -> None:
  1314. payload = {
  1315. "type": _WsType.ALERT.value,
  1316. "atype": type,
  1317. "message": message,
  1318. "system": system_notification,
  1319. "duration": duration,
  1320. }
  1321. if notification_id:
  1322. payload["notificationId"] = notification_id
  1323. self.__send_ws(
  1324. payload,
  1325. )
  1326. def __send_ws_partial(self, partial: str):
  1327. self.__send_ws(
  1328. {
  1329. "type": _WsType.PARTIAL.value,
  1330. "name": partial,
  1331. }
  1332. )
  1333. def __send_ws_block(
  1334. self,
  1335. action: t.Optional[str] = None,
  1336. message: t.Optional[str] = None,
  1337. close: t.Optional[bool] = False,
  1338. cancel: t.Optional[bool] = False,
  1339. ):
  1340. self.__send_ws(
  1341. {
  1342. "type": _WsType.BLOCK.value,
  1343. "action": action,
  1344. "close": close,
  1345. "message": message,
  1346. "noCancel": not cancel,
  1347. }
  1348. )
  1349. def __send_ws_navigate(
  1350. self,
  1351. to: str,
  1352. params: t.Optional[t.Dict[str, str]],
  1353. tab: t.Optional[str],
  1354. force: bool,
  1355. ):
  1356. self.__send_ws({"type": _WsType.NAVIGATE.value, "to": to, "params": params, "tab": tab, "force": force})
  1357. def __send_ws_update_with_dict(self, modified_values: dict) -> None:
  1358. payload = [
  1359. {"name": _get_client_var_name(k), "payload": v if isinstance(v, dict) and "value" in v else {"value": v}}
  1360. for k, v in modified_values.items()
  1361. ]
  1362. if self._is_broadcasting():
  1363. self.__broadcast_ws({"type": _WsType.MULTIPLE_UPDATE.value, "payload": payload})
  1364. self._set_broadcast(False)
  1365. else:
  1366. self.__send_ws({"type": _WsType.MULTIPLE_UPDATE.value, "payload": payload})
  1367. def __send_ws_broadcast(
  1368. self,
  1369. var_name: str,
  1370. var_value: t.Any,
  1371. client_id: t.Optional[str] = None,
  1372. message_type: t.Optional[_WsType] = None,
  1373. ):
  1374. self.__broadcast_ws(
  1375. {
  1376. "type": _WsType.BROADCAST.value if message_type is None else message_type.value,
  1377. "name": _get_broadcast_var_name(var_name),
  1378. "payload": {"value": var_value},
  1379. },
  1380. client_id,
  1381. )
  1382. def __get_ws_receiver(self, send_back_only=False) -> t.Union[t.List[str], t.Any, None]:
  1383. if self._bindings()._is_single_client():
  1384. return None
  1385. sid = getattr(request, "sid", None) if request else None
  1386. sids = self.__get_sids(self._get_client_id())
  1387. if sid:
  1388. sids.add(sid)
  1389. if send_back_only:
  1390. return sid
  1391. return list(sids)
  1392. def __get_sids(self, client_id: str) -> t.Set[str]:
  1393. return self.__client_id_2_sid.get(client_id, set())
  1394. def __get_message_grouping(self):
  1395. return (
  1396. _getscopeattr(self, Gui.__MESSAGE_GROUPING_NAME)
  1397. if _hasscopeattr(self, Gui.__MESSAGE_GROUPING_NAME)
  1398. else None
  1399. )
  1400. def __enter__(self):
  1401. self.__hold_messages()
  1402. return self
  1403. def __exit__(self, exc_type, exc_value, traceback):
  1404. try:
  1405. self.__send_messages()
  1406. except Exception as e: # pragma: no cover
  1407. _warn("Exception raised while sending messages", e)
  1408. if exc_value: # pragma: no cover
  1409. _warn(f"An {exc_type or 'Exception'} was raised", exc_value)
  1410. return True
  1411. def __hold_messages(self):
  1412. grouping_message = self.__get_message_grouping()
  1413. if grouping_message is None:
  1414. self._bind_var_val(Gui.__MESSAGE_GROUPING_NAME, [])
  1415. def __send_messages(self):
  1416. grouping_message = self.__get_message_grouping()
  1417. if grouping_message is not None:
  1418. _delscopeattr(self, Gui.__MESSAGE_GROUPING_NAME)
  1419. if len(grouping_message):
  1420. self.__send_ws({"type": _WsType.MULTIPLE_MESSAGE.value, "payload": grouping_message})
  1421. def _get_user_function(self, func_name: str) -> t.Union[t.Callable, str]:
  1422. func = (
  1423. getattr(self, func_name.split(".", 2)[1], func_name) if func_name.startswith(f"{Gui.__SELF_VAR}.") else None
  1424. )
  1425. if not _is_function(func):
  1426. func = _getscopeattr(self, func_name, None)
  1427. if not _is_function(func):
  1428. func = self._get_locals_bind().get(func_name)
  1429. if not _is_function(func):
  1430. func = self.__locals_context.get_default().get(func_name)
  1431. return t.cast(t.Callable, func) if _is_function(func) else func_name
  1432. def _get_user_instance(self, class_name: str, class_type: type) -> t.Union[object, str]:
  1433. cls = _getscopeattr(self, class_name, None)
  1434. if not isinstance(cls, class_type):
  1435. cls = self._get_locals_bind().get(class_name)
  1436. if not isinstance(cls, class_type):
  1437. cls = self.__locals_context.get_default().get(class_name)
  1438. return cls if isinstance(cls, class_type) else class_name
  1439. def __download_csv(self, state: State, var_name: str, payload: dict):
  1440. holder_name = t.cast(str, payload.get("var_name"))
  1441. try:
  1442. csv_path = self._get_accessor().to_csv(
  1443. holder_name,
  1444. _getscopeattr(self, holder_name, None),
  1445. )
  1446. if csv_path:
  1447. self._download(csv_path, "data.csv", Gui.__DOWNLOAD_DELETE_ACTION)
  1448. except Exception as e: # pragma: no cover
  1449. if not self._call_on_exception("download_csv", e):
  1450. _warn("download_csv(): Exception raised", e)
  1451. def __delete_csv(self, state: State, var_name: str, payload: dict):
  1452. try:
  1453. (Path(tempfile.gettempdir()) / t.cast(str, payload.get("args", [])[-1]).split("/")[-1]).unlink(True)
  1454. except Exception:
  1455. pass
  1456. def __on_action(self, id: t.Optional[str], payload: t.Any) -> None:
  1457. if isinstance(payload, dict):
  1458. action = payload.get("action")
  1459. else:
  1460. action = str(payload)
  1461. payload = {"action": action}
  1462. if action:
  1463. action_fn: t.Union[t.Callable, str]
  1464. if Gui.__DOWNLOAD_ACTION == action:
  1465. action_fn = self.__download_csv
  1466. payload["var_name"] = id
  1467. elif Gui.__DOWNLOAD_DELETE_ACTION == action:
  1468. action_fn = self.__delete_csv
  1469. else:
  1470. action_fn = self._get_user_function(action)
  1471. if self.__call_function_with_args(action_function=action_fn, id=id, payload=payload):
  1472. return
  1473. else: # pragma: no cover
  1474. _warn(f"on_action(): '{action}' is not a valid function.")
  1475. if getattr(self, "on_action", None) is not None:
  1476. self.__call_function_with_args(action_function=self.on_action, id=id, payload=payload)
  1477. def __call_function_with_args(self, **kwargs):
  1478. action_function = kwargs.get("action_function")
  1479. id = t.cast(str, kwargs.get("id"))
  1480. payload = kwargs.get("payload")
  1481. if _is_function(action_function):
  1482. try:
  1483. try:
  1484. args = [self._get_real_var_name(id)[0], payload]
  1485. except Exception:
  1486. args = [id, payload]
  1487. self._call_function_with_state(t.cast(t.Callable, action_function), args)
  1488. return True
  1489. except Exception as e: # pragma: no cover
  1490. if not self._call_on_exception(action_function, e):
  1491. _warn(f"on_action(): Exception raised in '{_function_name(action_function)}()'", e)
  1492. return False
  1493. def _call_function_with_state(self, user_function: t.Callable, args: t.Optional[t.List[t.Any]] = None) -> t.Any:
  1494. cp_args = [] if args is None else args.copy()
  1495. cp_args.insert(
  1496. 0,
  1497. _AsyncState(t.cast(_GuiState, self.__get_state()))
  1498. if iscoroutinefunction(user_function)
  1499. else self.__get_state(),
  1500. )
  1501. argcount = user_function.__code__.co_argcount
  1502. if argcount > 0 and ismethod(user_function):
  1503. argcount -= 1
  1504. if argcount > len(cp_args):
  1505. cp_args += (argcount - len(cp_args)) * [None]
  1506. else:
  1507. cp_args = cp_args[:argcount]
  1508. with self.__event_manager:
  1509. if iscoroutinefunction(user_function):
  1510. return _invoke_async_callback(user_function, cp_args)
  1511. else:
  1512. return user_function(*cp_args)
  1513. def _set_module_context(self, module_context: t.Optional[str]) -> t.ContextManager[None]:
  1514. return self._set_locals_context(module_context) if module_context is not None else contextlib.nullcontext()
  1515. def invoke_callback(
  1516. self,
  1517. state_id: str,
  1518. callback: t.Union[str, t.Callable],
  1519. args: t.Optional[t.Sequence[t.Any]] = None,
  1520. module_context: t.Optional[str] = None,
  1521. ) -> t.Any:
  1522. """Invoke a user callback for a given state.
  1523. See the [section on Long Running Callbacks in a Thread](../../../../../userman/gui/callbacks.md#long-running-callbacks-in-a-thread)
  1524. in the User Manual for details on when and how this function can be used.
  1525. Arguments:
  1526. state_id: The identifier of the state to use, as returned by `get_state_id()^`.
  1527. callback (Union[str, Callable[[State^, ...], None]]): The user-defined function that is invoked.<br/>
  1528. The first parameter of this function **must** be a `State^`.
  1529. args (Optional[Sequence]): The remaining arguments, as a List or a Tuple.
  1530. module_context (Optional[str]): The name of the module that will be used.
  1531. """ # noqa: E501
  1532. this_sid = None
  1533. if request:
  1534. # avoid messing with the client_id => Set(ws id)
  1535. this_sid = getattr(request, "sid", None)
  1536. request.sid = None # type: ignore[attr-defined]
  1537. try:
  1538. with self.get_flask_app().app_context():
  1539. setattr(g, Gui.__ARG_CLIENT_ID, state_id)
  1540. with self._set_module_context(module_context):
  1541. if not _is_function(callback):
  1542. callback = self._get_user_function(t.cast(str, callback))
  1543. if not _is_function(callback):
  1544. _warn(f"invoke_callback(): {callback} is not callable.")
  1545. return None
  1546. return self._call_function_with_state(t.cast(t.Callable, callback), list(args) if args else None)
  1547. except Exception as e: # pragma: no cover
  1548. if not self._call_on_exception(callback, e):
  1549. _warn(
  1550. f"Gui.invoke_callback(): Exception raised in {_function_name(callback)}",
  1551. e,
  1552. )
  1553. finally:
  1554. if this_sid:
  1555. request.sid = this_sid # type: ignore[attr-defined]
  1556. return None
  1557. def broadcast_callback(
  1558. self,
  1559. callback: t.Callable,
  1560. args: t.Optional[t.Sequence[t.Any]] = None,
  1561. module_context: t.Optional[str] = None,
  1562. ) -> t.Dict[str, t.Any]:
  1563. """Invoke a callback for every client.
  1564. This callback gets invoked for every client connected to the application with the appropriate
  1565. `State^` instance. You can then perform client-specific tasks, such as updating the state
  1566. variable reflected in the user interface.
  1567. Arguments:
  1568. callback: The user-defined function to be invoked.<br/>
  1569. The first parameter of this function must be a `State^` object representing the
  1570. client for which it is invoked.<br/>
  1571. The other parameters should reflect the ones provided in the *args* collection.
  1572. args: The parameters to send to *callback*, if any.
  1573. """
  1574. # Iterate over all the scopes
  1575. res = {}
  1576. for id in [id for id in self.__bindings._get_all_scopes() if id != _DataScopes._GLOBAL_ID]:
  1577. ret = self.invoke_callback(id, callback, args, module_context)
  1578. res[id] = ret
  1579. return res
  1580. def broadcast_change(self, var_name: str, value: t.Any):
  1581. """Propagates a new value for a given variable to all states.
  1582. This callback gets invoked for every client connected to the application to update the value
  1583. of the variable called *var_name* to the new value *value*, in their specific `State^`
  1584. instance. All user interfaces reflect the change.
  1585. Arguments:
  1586. var_name: The name of the variable to change.
  1587. value: The new value for the variable.
  1588. """
  1589. self.broadcast_callback(lambda s, n, v: s.assign(n, v), [var_name, value])
  1590. @staticmethod
  1591. def __broadcast_changes_fn(state: State, values: dict[str, t.Any]) -> None:
  1592. with state:
  1593. for n, v in values.items():
  1594. state.assign(n, v)
  1595. def broadcast_changes(self, values: t.Optional[dict[str, t.Any]] = None, **kwargs):
  1596. """Propagates new values for several variables to all states.
  1597. This callback gets invoked for every client connected to the application to update the value
  1598. of all the variables that are keys in *values*, in their specific `State^` instance. All
  1599. user interfaces reflect the change.
  1600. Arguments:
  1601. values: An optional dictionary where each key is the name of a variable to change, and
  1602. where the associated value is the new value to set for that variable, in each state
  1603. for the application.
  1604. **kwargs (dict[str, any]): A collection of variable name-value pairs that are updated
  1605. for each state of the application. Name-value pairs overload the ones in *values*
  1606. if the name exists as a key in the dictionary.
  1607. """
  1608. if kwargs:
  1609. values = values.copy() if values else {}
  1610. for n, v in kwargs.items():
  1611. values[n] = v
  1612. self.broadcast_callback(Gui.__broadcast_changes_fn, [values])
  1613. def _is_in_brdcst_callback(self):
  1614. try:
  1615. return getattr(g, Gui.__BRDCST_CALLBACK_G_ID, False)
  1616. except RuntimeError:
  1617. return False
  1618. # Proxy methods for Evaluator
  1619. def _evaluate_expr(
  1620. self, expr: str, lazy_declare: t.Optional[bool] = False, lambda_expr: t.Optional[bool] = False
  1621. ) -> t.Any:
  1622. return self.__evaluator.evaluate_expr(self, expr, lazy_declare, lambda_expr)
  1623. def _re_evaluate_expr(self, var_name: str) -> t.Set[str]:
  1624. return self.__evaluator.re_evaluate_expr(self, var_name)
  1625. def _refresh_expr(self, var_name: str, holder: t.Optional[_TaipyBase]):
  1626. return self.__evaluator.refresh_expr(self, var_name, holder)
  1627. def _get_expr_from_hash(self, hash_val: str) -> str:
  1628. return self.__evaluator.get_expr_from_hash(hash_val)
  1629. def _evaluate_bind_holder(self, holder: t.Type[_TaipyBase], expr: str) -> str:
  1630. return self.__evaluator.evaluate_bind_holder(self, holder, expr)
  1631. def _evaluate_holders(self, expr: str) -> t.List[str]:
  1632. return self.__evaluator.evaluate_holders(self, expr)
  1633. def _is_expression(self, expr: str) -> bool:
  1634. if self.__evaluator is None:
  1635. return False
  1636. return self.__evaluator._is_expression(expr)
  1637. # make components resettable
  1638. def _set_building(self, building: bool):
  1639. self._building = building
  1640. def __is_building(self):
  1641. return hasattr(self, "_building") and self._building
  1642. def _get_call_method_name(self, name: str):
  1643. return f"{Gui.__SELF_VAR}.{name}"
  1644. def __get_attributes(self, attr_json: str, hash_json: str, args_dict: t.Dict[str, t.Any]):
  1645. attributes: t.Dict[str, t.Any] = json.loads(unquote(attr_json))
  1646. hashes: t.Dict[str, str] = json.loads(unquote(hash_json))
  1647. attributes.update({k: args_dict.get(v) for k, v in hashes.items()})
  1648. return attributes, hashes
  1649. def _compare_data(self, *data):
  1650. return data[0]
  1651. def _get_adapted_lov(self, lov: list, var_type: str):
  1652. return self.__adapter._get_adapted_lov(lov, var_type)
  1653. def table_on_edit(self, state: State, var_name: str, payload: t.Dict[str, t.Any]):
  1654. """Default implementation of the `on_edit` callback for tables.
  1655. This function sets the value of a specific cell in the tabular dataset stored in
  1656. *var_name*, typically bound to the *data* property of a table control.
  1657. Arguments:
  1658. state: the state instance received in the callback.
  1659. var_name: the name of the variable bound to the table's *data* property.
  1660. payload: the payload dictionary received from the `on_edit` callback.<br/>
  1661. This dictionary has the following keys:
  1662. - *"index"*: The row index of the cell to be modified.
  1663. - *"col"*: Specifies the name of the column of the cell to be modified.
  1664. - *"value"*: Specifies the new value to be assigned to the cell.
  1665. - *"user_value"*: Contains the text entered by the user.
  1666. - *"tz"*: Specifies the timezone to be used, if applicable.
  1667. """
  1668. try:
  1669. setattr(state, var_name, self._get_accessor().on_edit(getattr(state, var_name), payload))
  1670. except Exception as e:
  1671. _warn("Gui.table_on_edit() failed potentially from a table's on_edit callback.", e)
  1672. def table_on_add(
  1673. self, state: State, var_name: str, payload: t.Dict[str, t.Any], new_row: t.Optional[t.List[t.Any]] = None
  1674. ):
  1675. """Default implementation of the `on_add` callback for tables.
  1676. This function creates a new row in the tabular dataset stored in *var_name*.<br/>
  1677. The row is added at the index specified in *payload["index"]*.
  1678. Arguments:
  1679. state: The state instance received from the callback.
  1680. var_name: The name of the variable bound to the table's *data* property.
  1681. payload: The payload dictionary received from the `on_add` callback.
  1682. new_row: The initial values for the new row.<br/>
  1683. If this parameter is not specified, the new row is initialized with all values set
  1684. to 0, with the exact meaning depending on the column data type.
  1685. """
  1686. try:
  1687. setattr(state, var_name, self._get_accessor().on_add(getattr(state, var_name), payload, new_row))
  1688. except Exception as e:
  1689. _warn("Gui.table_on_add() failed potentially from a table's on_add callback.", e)
  1690. def table_on_delete(self, state: State, var_name: str, payload: t.Dict[str, t.Any]):
  1691. """Default implementation of the `on_delete` callback for tables.
  1692. This function removes a row from the tabular dataset stored in *var_name*.<br/>
  1693. The row to be removed is located at the index specified in *payload["index"]*.
  1694. Arguments:
  1695. state: The state instance received in the callback.
  1696. var_name: The name of the variable bound to the table's *data* property.
  1697. payload: The payload dictionary received from the `on_delete` callback.
  1698. """
  1699. try:
  1700. setattr(state, var_name, self._get_accessor().on_delete(getattr(state, var_name), payload))
  1701. except Exception as e:
  1702. _warn("Gui.table_on_delete() failed potentially from a table's on_delete callback.", e)
  1703. def _tbl_cols(
  1704. self, rebuild: bool, rebuild_val: t.Optional[bool], attr_json: str, hash_json: str, **kwargs
  1705. ) -> t.Union[str, _DoNotUpdate]:
  1706. if not self.__is_building():
  1707. try:
  1708. rebuild = rebuild_val if rebuild_val is not None else rebuild
  1709. if rebuild:
  1710. attributes, hashes = self.__get_attributes(attr_json, hash_json, kwargs)
  1711. data_hash = hashes.get("data", "")
  1712. data = kwargs.get(data_hash)
  1713. col_dict = _get_columns_dict(
  1714. attributes.get("columns", {}),
  1715. self._get_accessor().get_cols_description(data_hash, _TaipyData(data, data_hash)),
  1716. attributes.get("date_format"),
  1717. attributes.get("number_format"),
  1718. )
  1719. _enhance_columns(attributes, hashes, t.cast(dict, col_dict), "table(cols)")
  1720. return json.dumps(col_dict, cls=_TaipyJsonEncoder)
  1721. except Exception as e: # pragma: no cover
  1722. _warn("Exception while rebuilding table columns", e)
  1723. return Gui.__DO_NOT_UPDATE_VALUE
  1724. def _chart_conf(
  1725. self, rebuild: bool, rebuild_val: t.Optional[bool], attr_json: str, hash_json: str, **kwargs
  1726. ) -> t.Union[str, _DoNotUpdate]:
  1727. if not self.__is_building():
  1728. try:
  1729. rebuild = rebuild_val if rebuild_val is not None else rebuild
  1730. if rebuild:
  1731. attributes, hashes = self.__get_attributes(attr_json, hash_json, kwargs)
  1732. idx = 0
  1733. data_hashes = []
  1734. while data_hash := hashes.get("data" if idx == 0 else f"data[{idx}]", ""):
  1735. data_hashes.append(data_hash)
  1736. idx += 1
  1737. config = _build_chart_config(
  1738. self,
  1739. attributes,
  1740. [
  1741. self._get_accessor().get_cols_description(
  1742. data_hash, _TaipyData(kwargs.get(data_hash), data_hash)
  1743. )
  1744. for data_hash in data_hashes
  1745. ],
  1746. )
  1747. return json.dumps(config, cls=_TaipyJsonEncoder)
  1748. except Exception as e: # pragma: no cover
  1749. _warn("Exception while rebuilding chart config", e)
  1750. return Gui.__DO_NOT_UPDATE_VALUE
  1751. # Proxy methods for Adapter
  1752. def _add_adapter_for_type(self, type_name: str, adapter: t.Callable) -> None:
  1753. self.__adapter._add_for_type(type_name, adapter)
  1754. def _add_type_for_var(self, var_name: str, type_name: str) -> None:
  1755. self.__adapter._add_type_for_var(var_name, type_name)
  1756. def _get_adapter_for_type(self, type_name: str) -> t.Optional[t.Callable]:
  1757. return self.__adapter._get_for_type(type_name)
  1758. def _get_unique_type_adapter(self, type_name: str) -> str:
  1759. return self.__adapter._get_unique_type(type_name)
  1760. def _run_adapter(
  1761. self, adapter: t.Optional[t.Callable], value: t.Any, var_name: str, id_only=False
  1762. ) -> t.Union[t.Tuple[str, ...], str, None]:
  1763. return self.__adapter._run(adapter, value, var_name, id_only)
  1764. def _get_valid_adapter_result(self, value: t.Any, id_only=False) -> t.Union[t.Tuple[str, ...], str, None]:
  1765. return self.__adapter._get_valid_result(value, id_only)
  1766. def _is_ui_blocked(self):
  1767. return _getscopeattr(self, Gui.__UI_BLOCK_NAME, False)
  1768. def __get_on_cancel_block_ui(self, callback: t.Optional[str]):
  1769. def _taipy_on_cancel_block_ui(a_state: State, id: t.Optional[str], payload: t.Any):
  1770. gui_app = a_state.get_gui()
  1771. if _hasscopeattr(gui_app, Gui.__UI_BLOCK_NAME):
  1772. _setscopeattr(gui_app, Gui.__UI_BLOCK_NAME, False)
  1773. gui_app.__on_action(id, {"action": callback})
  1774. return _taipy_on_cancel_block_ui
  1775. def __add_pages_in_folder(self, folder_name: str, folder_path: str):
  1776. from ._renderers import Html, Markdown
  1777. list_of_files = os.listdir(folder_path)
  1778. for file_name in list_of_files:
  1779. if file_name.startswith("__"):
  1780. continue
  1781. if (re_match := Gui.__RE_HTML.match(file_name)) and f"{re_match.group(1)}.py" not in list_of_files:
  1782. _renderers = Html(os.path.join(folder_path, file_name), frame=None)
  1783. _renderers.modify_taipy_base_url(folder_name)
  1784. self.add_page(name=f"{folder_name}/{re_match.group(1)}", page=_renderers)
  1785. elif (re_match := Gui.__RE_MD.match(file_name)) and f"{re_match.group(1)}.py" not in list_of_files:
  1786. _renderers_md = Markdown(os.path.join(folder_path, file_name), frame=None)
  1787. self.add_page(name=f"{folder_name}/{re_match.group(1)}", page=_renderers_md)
  1788. elif re_match := Gui.__RE_PY.match(file_name):
  1789. module_name = re_match.group(1)
  1790. module_path = os.path.join(folder_name, module_name).replace(os.path.sep, ".")
  1791. try:
  1792. module = importlib.import_module(module_path)
  1793. page_instance = _get_page_from_module(module)
  1794. if page_instance is not None:
  1795. self.add_page(name=f"{folder_name}/{module_name}", page=page_instance)
  1796. except Exception as e:
  1797. _warn(f"Error while importing module '{module_path}'", e)
  1798. elif os.path.isdir(child_dir_path := os.path.join(folder_path, file_name)):
  1799. child_dir_name = f"{folder_name}/{file_name}"
  1800. self.__add_pages_in_folder(child_dir_name, child_dir_path)
  1801. # Proxy methods for LocalsContext
  1802. def _get_locals_context_obj(self) -> _LocalsContext:
  1803. return self.__locals_context
  1804. def _get_variable_directory_obj(self) -> _VariableDirectory:
  1805. return self.__var_dir
  1806. def _get_locals_bind(self) -> t.Dict[str, t.Any]:
  1807. return self.__locals_context.get_locals()
  1808. def _get_default_locals_bind(self) -> t.Dict[str, t.Any]:
  1809. return self.__locals_context.get_default()
  1810. def _get_locals_bind_from_context(self, context: t.Optional[str]) -> t.Dict[str, t.Any]:
  1811. return self.__locals_context._get_locals_bind_from_context(context)
  1812. def _get_locals_context(self) -> str:
  1813. current_context = self.__locals_context.get_context()
  1814. return current_context if current_context is not None else t.cast(str, self.__default_module_name)
  1815. def _set_locals_context(self, context: t.Optional[str]) -> t.ContextManager[None]:
  1816. return self.__locals_context.set_locals_context(context)
  1817. def _has_set_context(self):
  1818. return self.__locals_context.get_context() is not None
  1819. def _get_page_context(self, page_name: str) -> str | None:
  1820. if page_name not in self._config.routes:
  1821. return None
  1822. page = None
  1823. for p in self._config.pages:
  1824. if p._route == page_name:
  1825. page = p
  1826. if page is None:
  1827. return None
  1828. return (
  1829. (page._renderer._get_module_name() or self.__default_module_name)
  1830. if page._renderer is not None
  1831. else self.__default_module_name
  1832. )
  1833. @staticmethod
  1834. def _get_root_page_name():
  1835. return Gui.__root_page_name
  1836. def _set_flask(self, flask: Flask):
  1837. self._flask = flask
  1838. def _get_default_module_name(self):
  1839. return self.__default_module_name
  1840. @staticmethod
  1841. def _get_timezone() -> str:
  1842. return Gui.__LOCAL_TZ
  1843. @staticmethod
  1844. def _set_timezone(tz: str):
  1845. Gui.__LOCAL_TZ = tz
  1846. # Public methods
  1847. def add_page(
  1848. self,
  1849. name: str,
  1850. page: t.Union[str, Page],
  1851. style: t.Optional[str] = "",
  1852. ) -> None:
  1853. """Add a page to the Graphical User Interface.
  1854. Arguments:
  1855. name: The name of the page.
  1856. page (Union[str, Page^]): The content of the page.<br/>
  1857. It can be an instance of `Markdown^` or `Html^`.<br/>
  1858. If *page* is a string, then:
  1859. - If *page* is set to the pathname of a readable file, the page
  1860. content is read as Markdown input text.
  1861. - If it is not, the page content is read from this string as
  1862. Markdown text.
  1863. style (Optional[str]): Additional CSS style to apply to this page.
  1864. - If there is style associated with a page, it is used at a global level
  1865. - If there is no style associated with the page, the style is cleared at a global level
  1866. - If the page is embedded in a block control, the style is ignored
  1867. Note that page names cannot start with the slash ('/') character and that each
  1868. page must have a unique name.
  1869. """
  1870. # Validate name
  1871. if name is None: # pragma: no cover
  1872. raise Exception("name is required for add_page() function.")
  1873. if not Gui.__RE_PAGE_NAME.match(name): # pragma: no cover
  1874. raise SyntaxError(
  1875. f'Page name "{name}" is invalid. It must only contain letters, digits, dash (-), underscore (_), and forward slash (/) characters.' # noqa: E501
  1876. )
  1877. if name.startswith("/"): # pragma: no cover
  1878. raise SyntaxError(f'Page name "{name}" cannot start with forward slash (/) character.')
  1879. if name in self._config.routes: # pragma: no cover
  1880. raise Exception(f'Page name "{name if name != Gui.__root_page_name else "/"}" is already defined.')
  1881. if isinstance(page, str):
  1882. from ._renderers import Markdown
  1883. page = Markdown(page, frame=None)
  1884. elif not isinstance(page, Page): # pragma: no cover
  1885. raise Exception(
  1886. f'Parameter "page" is invalid for page name "{name if name != Gui.__root_page_name else "/"}".'
  1887. )
  1888. # Init a new page
  1889. new_page = _Page()
  1890. new_page._route = name
  1891. new_page._renderer = page
  1892. new_page._style = style or page._get_style()
  1893. new_page._script_paths = page._get_script_paths()
  1894. # Append page to _config
  1895. self._config.pages.append(new_page)
  1896. self._config.routes.append(name)
  1897. # set root page
  1898. if name == Gui.__root_page_name:
  1899. self._config.root_page = new_page
  1900. # Validate Page
  1901. _Hooks().validate_page(self, page)
  1902. # Update locals context
  1903. self._add_page_context(page)
  1904. # Special case needed for page to access gui to trigger reload in notebook
  1905. if _is_in_notebook():
  1906. page._notebook_gui = self
  1907. page._notebook_page = new_page
  1908. # add page to hook
  1909. _Hooks().add_page(self, page)
  1910. def _add_page_context(self, page: Page) -> t.Optional[str]:
  1911. # Update locals context
  1912. module_name = page._get_module_name()
  1913. if not self.__locals_context.has_context(module_name):
  1914. self.__locals_context.add(module_name, page._get_locals())
  1915. # Update variable directory
  1916. if not page._is_class_module():
  1917. self.__var_dir.add_frame(page._frame)
  1918. return module_name
  1919. def add_pages(self, pages: t.Optional[t.Union[t.Mapping[str, t.Union[str, Page]], str]] = None) -> None:
  1920. """Add several pages to the Graphical User Interface.
  1921. Arguments:
  1922. pages (Union[dict[str, Union[str, Page^]], str]): The pages to add.<br/>
  1923. If *pages* is a dictionary, a page is added to this `Gui` instance
  1924. for each of the entries in *pages*:
  1925. - The entry key is used as the page name.
  1926. - The entry value is used as the page content:
  1927. - The value can can be an instance of `Markdown^` or `Html^`, then
  1928. it is used as the page definition.
  1929. - If entry value is a string, then:
  1930. - If it is set to the pathname of a readable file, the page
  1931. content is read as Markdown input text.
  1932. - If it is not, the page content is read from this string as
  1933. Markdown text.
  1934. !!! note "Reading pages from a directory"
  1935. If *pages* is a string that holds the path to a readable directory, then
  1936. this directory is traversed, recursively, to find files that Taipy can build
  1937. pages from.
  1938. For every new directory that is traversed, a new hierarchical level
  1939. for pages is created.
  1940. For every file that is found:
  1941. - If the filename extension is *.md*, it is read as Markdown content and
  1942. a new page is created with the base name of this filename.
  1943. - If the filename extension is *.html*, it is read as HTML content and
  1944. a new page is created with the base name of this filename.
  1945. For example, say you have the following directory structure:
  1946. ```
  1947. reports
  1948. ├── home.html
  1949. ├── budget/
  1950. │ ├── expenses/
  1951. │ │ ├── marketing.md
  1952. │ │ └── production.md
  1953. │ └── revenue/
  1954. │ ├── EMAE.md
  1955. │ ├── USA.md
  1956. │ └── ASIA.md
  1957. └── cashflow/
  1958. ├── weekly.md
  1959. ├── monthly.md
  1960. └── yearly.md
  1961. ```
  1962. Calling `gui.add_pages('reports')` is equivalent to calling:
  1963. ```py
  1964. gui.add_pages({
  1965. "reports/home", Html("reports/home.html"),
  1966. "reports/budget/expenses/marketing", Markdown("reports/budget/expenses/marketing.md"),
  1967. "reports/budget/expenses/production", Markdown("reports/budget/expenses/production.md"),
  1968. "reports/budget/revenue/EMAE", Markdown("reports/budget/revenue/EMAE.md"),
  1969. "reports/budget/revenue/USA", Markdown("reports/budget/revenue/USA.md"),
  1970. "reports/budget/revenue/ASIA", Markdown("reports/budget/revenue/ASIA.md"),
  1971. "reports/cashflow/weekly", Markdown("reports/cashflow/weekly.md"),
  1972. "reports/cashflow/monthly", Markdown("reports/cashflow/monthly.md"),
  1973. "reports/cashflow/yearly", Markdown("reports/cashflow/yearly.md")
  1974. })
  1975. ```
  1976. """
  1977. if isinstance(pages, dict):
  1978. for k, v in pages.items():
  1979. if k == "/":
  1980. k = Gui.__root_page_name
  1981. self.add_page(name=k, page=v)
  1982. elif isinstance(folder_name := pages, str):
  1983. if not hasattr(self, "_root_dir"):
  1984. self._root_dir = os.path.dirname(getabsfile(self.__frame))
  1985. folder_path = folder_name if os.path.isabs(folder_name) else os.path.join(self._root_dir, folder_name)
  1986. folder_name = os.path.basename(folder_path)
  1987. if not os.path.isdir(folder_path): # pragma: no cover
  1988. raise RuntimeError(f"Path {folder_path} is not a valid directory")
  1989. if folder_name in self.__directory_name_of_pages: # pragma: no cover
  1990. raise Exception(f"Base directory name {folder_name} of path {folder_path} is not unique")
  1991. if folder_name in Gui.__reserved_routes: # pragma: no cover
  1992. raise Exception(f"Invalid directory. Directory {folder_name} is a reserved route")
  1993. self.__directory_name_of_pages.append(folder_name)
  1994. self.__add_pages_in_folder(folder_name, folder_path)
  1995. # partials
  1996. def add_partial(
  1997. self,
  1998. page: t.Union[str, Page],
  1999. ) -> Partial:
  2000. """Create a new `Partial^`.
  2001. The [User Manual section on Partials](../../../../../userman/gui/pages/partial/index.md)
  2002. gives details on when and how to use this class.
  2003. Arguments:
  2004. page (Union[str, Page^]): The page to create a new Partial from.<br/>
  2005. It can be an instance of `Markdown^` or `Html^`.<br/>
  2006. If *page* is a string, then:
  2007. - If *page* is set to the pathname of a readable file, the content of
  2008. the new `Partial` is read as Markdown input text.
  2009. - If it is not, the content of the new `Partial` is read from this string
  2010. as Markdown text.
  2011. Returns:
  2012. The new `Partial` object defined by *page*.
  2013. """
  2014. new_partial = Partial()
  2015. # Validate name
  2016. if (
  2017. new_partial._route in self._config.partial_routes or new_partial._route in self._config.routes
  2018. ): # pragma: no cover
  2019. _warn(f'Partial name "{new_partial._route}" is already defined.')
  2020. if isinstance(page, str):
  2021. from ._renderers import Markdown
  2022. page = Markdown(page, frame=None)
  2023. elif not isinstance(page, Page): # pragma: no cover
  2024. raise Exception(f'Partial name "{new_partial._route}" has an invalid Page.')
  2025. new_partial._renderer = page
  2026. # Append partial to _config
  2027. self._config.partials.append(new_partial)
  2028. self._config.partial_routes.append(str(new_partial._route))
  2029. # Update locals context
  2030. self._add_page_context(page)
  2031. return new_partial
  2032. def _update_partial(self, partial: Partial):
  2033. partials = _getscopeattr(self, Partial._PARTIALS, {})
  2034. partials[partial._route] = partial
  2035. _setscopeattr(self, Partial._PARTIALS, partials)
  2036. self.__send_ws_partial(str(partial._route))
  2037. def _get_partial(self, route: str) -> t.Optional[Partial]:
  2038. partials = _getscopeattr(self, Partial._PARTIALS, {})
  2039. partial = partials.get(route)
  2040. if partial is None:
  2041. partial = next((p for p in self._config.partials if p._route == route), None)
  2042. partials[route] = partial
  2043. _setscopeattr(self, Partial._PARTIALS, partials)
  2044. return partial
  2045. # Main binding method (bind in markdown declaration)
  2046. def _bind_var(self, var_name: str) -> str:
  2047. bind_context = None
  2048. if var_name in self._get_locals_bind().keys():
  2049. bind_context = self._get_locals_context()
  2050. if bind_context is None:
  2051. encoded_var_name = self.__var_dir.add_var(var_name, self._get_locals_context(), var_name)
  2052. else:
  2053. encoded_var_name = self.__var_dir.add_var(var_name, bind_context)
  2054. if not hasattr(self._bindings(), encoded_var_name):
  2055. bind_locals = self._get_locals_bind_from_context(bind_context)
  2056. if var_name in bind_locals.keys():
  2057. self._bind(encoded_var_name, bind_locals[var_name])
  2058. else:
  2059. _warn(
  2060. f"Variable '{var_name}' is not available in either the '{self._get_locals_context()}' or '__main__' modules." # noqa: E501
  2061. )
  2062. return encoded_var_name
  2063. def _bind_var_val(self, var_name: str, value: t.Any) -> bool:
  2064. if not _is_moduled_variable(var_name):
  2065. var_name = self.__var_dir.add_var(var_name, self._get_locals_context())
  2066. if not hasattr(self._bindings(), var_name):
  2067. self._bind(var_name, value)
  2068. return True
  2069. return False
  2070. def __bind_local_func(self, name: str):
  2071. func = getattr(self, name, None)
  2072. if func is not None and not _is_function(func): # pragma: no cover
  2073. _warn(f"{self.__class__.__name__}.{name}: {func} should be a function; looking for {name} in the script.")
  2074. func = None
  2075. if func is None:
  2076. func = self._get_locals_bind().get(name)
  2077. if func is not None:
  2078. if _is_function(func):
  2079. setattr(self, name, func)
  2080. else: # pragma: no cover
  2081. _warn(f"{name}: {func} should be a function.")
  2082. def load_config(self, config: Config) -> None:
  2083. self._config._load(config)
  2084. def _broadcast(
  2085. self,
  2086. name: str,
  2087. value: t.Any,
  2088. client_id: t.Optional[str] = None,
  2089. message_type: t.Optional[_WsType] = None,
  2090. ):
  2091. """NOT DOCUMENTED
  2092. Send the new value of a variable to all connected clients.
  2093. Arguments:
  2094. name: The name of the variable to update or create.
  2095. value: The value (must be serializable to the JSON format).
  2096. client_id: The client id (broadcast to all client if None)
  2097. """
  2098. self.__send_ws_broadcast(name, value, client_id, message_type)
  2099. def _broadcast_all_clients(self, name: str, value: t.Any):
  2100. try:
  2101. self._set_broadcast()
  2102. self._update_var(name, value)
  2103. finally:
  2104. self._set_broadcast(False)
  2105. def _set_broadcast(self, broadcast: bool = True):
  2106. with contextlib.suppress(RuntimeError):
  2107. setattr(g, Gui.__BROADCAST_G_ID, broadcast)
  2108. def _is_broadcasting(self) -> bool:
  2109. try:
  2110. return getattr(g, Gui.__BROADCAST_G_ID, False)
  2111. except RuntimeError:
  2112. return False
  2113. def _download(
  2114. self, content: t.Any, name: t.Optional[str] = "", on_action: t.Optional[t.Union[str, t.Callable]] = ""
  2115. ):
  2116. if _is_function(on_action):
  2117. on_action_name = (
  2118. _get_lambda_id(t.cast(LambdaType, on_action))
  2119. if _is_unnamed_function(on_action)
  2120. else _get_expr_var_name(t.cast(t.Callable, on_action).__name__)
  2121. )
  2122. if on_action_name:
  2123. self._bind_var_val(on_action_name, on_action)
  2124. on_action = on_action_name
  2125. else:
  2126. _warn("download() on_action is invalid.")
  2127. content_str = self._get_content("Gui.download", content, False)
  2128. self.__send_ws_download(content_str, str(name), str(on_action) if on_action is not None else "")
  2129. def _notify(
  2130. self,
  2131. notification_type: str = "I",
  2132. message: str = "",
  2133. system_notification: t.Optional[bool] = None,
  2134. duration: t.Optional[int] = None,
  2135. notification_id: t.Optional[str] = None,
  2136. ):
  2137. if not notification_id:
  2138. notification_id = str(uuid.uuid4())
  2139. self.__send_ws_alert(
  2140. notification_type,
  2141. message,
  2142. self._get_config("system_notification", False) if system_notification is None else system_notification,
  2143. self._get_config("notification_duration", 3000) if duration is None else duration,
  2144. notification_id,
  2145. )
  2146. return notification_id
  2147. def _close_notification(
  2148. self,
  2149. notification_id: str,
  2150. ):
  2151. if notification_id:
  2152. self.__send_ws_alert(
  2153. type="", # Since you're closing, set type to an empty string or a predefined "close" type
  2154. message="", # No need for a message when closing
  2155. system_notification=False, # System notification not needed for closing
  2156. duration=0, # No duration since it's an immediate close
  2157. notification_id=notification_id,
  2158. )
  2159. def _hold_actions(
  2160. self,
  2161. callback: t.Optional[t.Union[str, t.Callable]] = None,
  2162. message: t.Optional[str] = "Work in Progress...",
  2163. ): # pragma: no cover
  2164. if _is_unnamed_function(callback):
  2165. action_name = _get_lambda_id(t.cast(LambdaType, callback))
  2166. self._bind_var_val(action_name, callback)
  2167. else:
  2168. action_name = (
  2169. callback if isinstance(callback, str) else (callback.__name__ if callback is not None else None)
  2170. )
  2171. func = self.__get_on_cancel_block_ui(action_name)
  2172. def_action_name = func.__name__
  2173. _setscopeattr(self, def_action_name, func)
  2174. if _hasscopeattr(self, Gui.__UI_BLOCK_NAME):
  2175. _setscopeattr(self, Gui.__UI_BLOCK_NAME, True)
  2176. else:
  2177. self._bind(Gui.__UI_BLOCK_NAME, True)
  2178. self.__send_ws_block(action=def_action_name, message=message, cancel=bool(action_name))
  2179. def _resume_actions(self): # pragma: no cover
  2180. if _hasscopeattr(self, Gui.__UI_BLOCK_NAME):
  2181. _setscopeattr(self, Gui.__UI_BLOCK_NAME, False)
  2182. self.__send_ws_block(close=True)
  2183. def _navigate(
  2184. self,
  2185. to: t.Optional[str] = "",
  2186. params: t.Optional[t.Dict[str, str]] = None,
  2187. tab: t.Optional[str] = None,
  2188. force: t.Optional[bool] = False,
  2189. ):
  2190. to = to or Gui.__root_page_name
  2191. if not to.startswith("/") and to not in self._config.routes and not urlparse(to).netloc:
  2192. _warn(f'Cannot navigate to "{to if to != Gui.__root_page_name else "/"}": unknown page.')
  2193. return False
  2194. self.__send_ws_navigate(to if to != Gui.__root_page_name else "/", params, tab, force or False)
  2195. return True
  2196. def __init_libs(self):
  2197. for name, libs in self.__extensions.items():
  2198. for lib in libs:
  2199. if not isinstance(lib, ElementLibrary):
  2200. continue
  2201. try:
  2202. self._call_function_with_state(lib.on_user_init)
  2203. except Exception as e: # pragma: no cover
  2204. if not self._call_on_exception(f"{name}.on_user_init", e):
  2205. _warn(f"Exception raised in {name}.on_user_init()", e)
  2206. def __init_route(self):
  2207. self.__set_client_id_in_context(force=True)
  2208. if not _hasscopeattr(self, Gui.__ON_INIT_NAME):
  2209. _setscopeattr(self, Gui.__ON_INIT_NAME, True)
  2210. self.__pre_render_pages()
  2211. self.__init_libs()
  2212. if hasattr(self, "on_init") and _is_function(self.on_init):
  2213. try:
  2214. self._call_function_with_state(t.cast(t.Callable, self.on_init))
  2215. except Exception as e: # pragma: no cover
  2216. if not self._call_on_exception("on_init", e):
  2217. _warn("Exception raised in on_init()", e)
  2218. return self._render_route()
  2219. def _call_on_exception(self, function: t.Any, exception: Exception) -> bool:
  2220. if hasattr(self, "on_exception") and _is_function(self.on_exception):
  2221. function_name = _function_name(function) if callable(function) else str(function)
  2222. try:
  2223. self._call_function_with_state(t.cast(t.Callable, self.on_exception), [function_name, exception])
  2224. except Exception as e: # pragma: no cover
  2225. _warn("Exception raised in on_exception()", e)
  2226. return True
  2227. return False
  2228. def __call_on_status(self) -> t.Optional[str]:
  2229. if hasattr(self, "on_status") and _is_function(self.on_status):
  2230. try:
  2231. return self._call_function_with_state(t.cast(t.Callable, self.on_status))
  2232. except Exception as e: # pragma: no cover
  2233. if not self._call_on_exception("on_status", e):
  2234. _warn("Exception raised in on_status", e)
  2235. return None
  2236. def __pre_render_pages(self) -> None:
  2237. """Pre-render all pages to have a proper initialization of all variables"""
  2238. self.__set_client_id_in_context()
  2239. scope_metadata = self._get_data_scope_metadata()
  2240. if scope_metadata[_DataScopes._META_PRE_RENDER]:
  2241. return
  2242. for page in self._config.pages:
  2243. if page is not None:
  2244. with contextlib.suppress(Exception):
  2245. if isinstance(page._renderer, CustomPage):
  2246. self._bind_custom_page_variables(page._renderer, self._get_client_id())
  2247. else:
  2248. page.render(self, silent=True)
  2249. if additional_pages := _Hooks()._get_additional_pages():
  2250. for page in additional_pages:
  2251. if isinstance(page, Page):
  2252. with contextlib.suppress(Exception):
  2253. if isinstance(page, CustomPage):
  2254. self._bind_custom_page_variables(page, self._get_client_id())
  2255. else:
  2256. new_page = _Page()
  2257. new_page._renderer = page
  2258. new_page.render(self, silent=True)
  2259. scope_metadata[_DataScopes._META_PRE_RENDER] = True
  2260. def _get_navigated_page(self, page_name: str) -> t.Any:
  2261. nav_page = page_name
  2262. if hasattr(self, "on_navigate") and _is_function(self.on_navigate):
  2263. try:
  2264. params = request.args.to_dict() if hasattr(request, "args") else {}
  2265. params.pop("client_id", None)
  2266. params.pop("v", None)
  2267. nav_page = self._call_function_with_state(
  2268. t.cast(t.Callable, self.on_navigate),
  2269. ["/" if page_name == Gui.__root_page_name else page_name, params],
  2270. )
  2271. if nav_page == "/":
  2272. nav_page = Gui.__root_page_name
  2273. if nav_page != page_name:
  2274. if isinstance(nav_page, str):
  2275. if self._navigate(nav_page):
  2276. return ("Root page cannot be re-routed by on_navigate().", 302)
  2277. else:
  2278. _warn(f"on_navigate() returned an invalid page name '{nav_page}'.")
  2279. nav_page = page_name
  2280. except Exception as e: # pragma: no cover
  2281. if not self._call_on_exception("on_navigate", e):
  2282. _warn("Exception raised in on_navigate()", e)
  2283. return nav_page
  2284. def _call_on_page_load(self, page_name: str) -> None:
  2285. if page_name == Gui.__root_page_name:
  2286. page_name = "/"
  2287. on_page_load_fn = self._get_user_function("on_page_load")
  2288. if not _is_function(on_page_load_fn):
  2289. return
  2290. try:
  2291. self._call_function_with_state(t.cast(t.Callable, on_page_load_fn), [page_name])
  2292. except Exception as e:
  2293. if not self._call_on_exception("on_page_load", e):
  2294. _warn("Exception raised in on_page_load()", e)
  2295. def _get_page(self, page_name: str):
  2296. return next((page_i for page_i in self._config.pages if page_i._route == page_name), None)
  2297. def _bind_custom_page_variables(self, page: CustomPage, client_id: t.Optional[str]):
  2298. """Handle the bindings of custom page variables"""
  2299. if not isinstance(page, CustomPage):
  2300. return
  2301. with self.get_flask_app().app_context() if has_app_context() else contextlib.nullcontext(): # type: ignore[attr-defined]
  2302. self.__set_client_id_in_context(client_id)
  2303. with self._set_locals_context(page._get_module_name()):
  2304. for k, v in self._get_locals_bind().items():
  2305. if (
  2306. (not page._binding_variables or k in page._binding_variables)
  2307. and not k.startswith("_")
  2308. and not isinstance(v, ModuleType)
  2309. ):
  2310. self._bind_var(k)
  2311. def __render_page(self, page_name: str) -> t.Any:
  2312. self.__set_client_id_in_context()
  2313. nav_page = self._get_navigated_page(page_name)
  2314. if not isinstance(nav_page, str):
  2315. return nav_page
  2316. page = self._get_page(nav_page)
  2317. # Try partials
  2318. if page is None:
  2319. page = self._get_partial(nav_page)
  2320. # Make sure that there is a page instance found
  2321. if page is None:
  2322. return (
  2323. jsonify({"error": f"Page '{nav_page}' doesn't exist."}),
  2324. 400,
  2325. {"Content-Type": "application/json; charset=utf-8"},
  2326. )
  2327. # Handle custom pages
  2328. if (pr := page._renderer) is not None and isinstance(pr, CustomPage):
  2329. if self._navigate(
  2330. to=page_name,
  2331. params={
  2332. _Server._RESOURCE_HANDLER_ARG: pr._resource_handler.get_id(),
  2333. },
  2334. ):
  2335. # Proactively handle the bindings of custom page variables
  2336. self._bind_custom_page_variables(pr, self._get_client_id())
  2337. return ("Successfully redirect to custom resource handler", 200)
  2338. return ("Failed to navigate to custom resource handler", 500)
  2339. # Handle page rendering
  2340. context = page.render(self)
  2341. if (
  2342. nav_page == Gui.__root_page_name
  2343. and page._rendered_jsx is not None
  2344. and "<PageContent" not in page._rendered_jsx
  2345. ):
  2346. page._rendered_jsx += "<PageContent />"
  2347. # Return jsx page
  2348. if page._rendered_jsx is not None:
  2349. with self._set_locals_context(context):
  2350. self._call_on_page_load(nav_page)
  2351. return self._server._render(
  2352. page._rendered_jsx,
  2353. page._script_paths if page._script_paths is not None else [],
  2354. page._style if page._style is not None else "",
  2355. page._head,
  2356. context, # noqa: E501
  2357. )
  2358. else:
  2359. return ("No page template", 404)
  2360. def _render_route(self) -> t.Any:
  2361. return self._server._direct_render_json(
  2362. {
  2363. "locations": {
  2364. "/" if route == Gui.__root_page_name else f"/{route}": f"/{route}" for route in self._config.routes
  2365. },
  2366. "blockUI": self._is_ui_blocked(),
  2367. }
  2368. )
  2369. def get_flask_app(self) -> Flask:
  2370. """Get the internal Flask application.
  2371. This method must be called **after** `(Gui.)run()^` was invoked.
  2372. Returns:
  2373. The Flask instance used.
  2374. """
  2375. if hasattr(self, "_server"):
  2376. return t.cast(Flask, self._server.get_flask())
  2377. raise RuntimeError("get_flask_app() cannot be invoked before run() has been called.")
  2378. def _get_port(self) -> int:
  2379. return self._server.get_port()
  2380. def _set_frame(self, frame: t.Optional[FrameType]):
  2381. if not isinstance(frame, FrameType): # pragma: no cover
  2382. raise RuntimeError("frame must be a FrameType where Gui can collect the local variables.")
  2383. self.__frame = frame
  2384. self.__default_module_name = _get_module_name_from_frame(self.__frame)
  2385. def _set_css_file(self, css_file: t.Optional[str] = None):
  2386. script_file = Path(self.__frame.f_code.co_filename or ".").resolve()
  2387. if css_file is None:
  2388. if script_file.with_suffix(".css").exists():
  2389. css_file = f"{script_file.stem}.css"
  2390. elif script_file.is_dir() and (script_file / "taipy.css").exists():
  2391. css_file = "taipy.css"
  2392. if css_file is None:
  2393. script_file = script_file.with_name("taipy").with_suffix(".css")
  2394. if script_file.exists():
  2395. css_file = f"{script_file.stem}.css"
  2396. self.__css_file = css_file
  2397. def _set_state(self, state: State):
  2398. if isinstance(state, State):
  2399. self.__state = state
  2400. def _get_webapp_path(self):
  2401. _conf_webapp_path = (
  2402. Path(self._get_config("webapp_path", None)) if self._get_config("webapp_path", None) else None
  2403. )
  2404. _webapp_path = str((Path(__file__).parent / "webapp").resolve())
  2405. if _conf_webapp_path:
  2406. if _conf_webapp_path.is_dir():
  2407. _webapp_path = str(_conf_webapp_path.resolve())
  2408. _warn(f"Using webapp_path: '{_conf_webapp_path}'.")
  2409. else: # pragma: no cover
  2410. _warn(
  2411. f"webapp_path: '{_conf_webapp_path}' is not a valid directory. Falling back to '{_webapp_path}'." # noqa: E501
  2412. )
  2413. return _webapp_path
  2414. def __get_client_config(self) -> t.Dict[str, t.Any]:
  2415. config = {
  2416. "timeZone": self._config.get_time_zone(),
  2417. "darkMode": self._get_config("dark_mode", True),
  2418. "baseURL": self._config._get_config("base_url", "/"),
  2419. }
  2420. if themes := self._get_themes():
  2421. config["themes"] = themes
  2422. if len(self.__extensions):
  2423. config["extensions"] = {}
  2424. for libs in self.__extensions.values():
  2425. for lib in libs:
  2426. config["extensions"][f"./{Gui._EXTENSION_ROOT}/{lib.get_js_module_name()}"] = [
  2427. e._get_js_name(n)
  2428. for n, e in lib.get_elements().items()
  2429. if isinstance(e, Element) and not e._is_server_only()
  2430. ]
  2431. if stylekit := self._get_config("stylekit", _default_stylekit):
  2432. config["stylekit"] = {_to_camel_case(k): v for k, v in stylekit.items()}
  2433. return config
  2434. def __get_css_vars(self) -> str:
  2435. css_vars = []
  2436. if stylekit := self._get_config("stylekit", _default_stylekit):
  2437. for k, v in stylekit.items():
  2438. css_vars.append(f"--{k.replace('_', '-')}:{_get_css_var_value(v)};")
  2439. return " ".join(css_vars)
  2440. def __init_server(self):
  2441. app_config = self._config.config
  2442. # Init server if there is no server
  2443. if not hasattr(self, "_server"):
  2444. self._server = _Server(
  2445. self,
  2446. path_mapping=self._path_mapping,
  2447. flask=self._flask,
  2448. async_mode=app_config.get("async_mode"),
  2449. allow_upgrades=not app_config.get("notebook_proxy"),
  2450. server_config=app_config.get("server_config"),
  2451. )
  2452. # Stop and reinitialize the server if it is still running as a thread
  2453. if (_is_in_notebook() or app_config.get("run_in_thread")) and hasattr(self._server, "_thread"):
  2454. self.stop()
  2455. self._flask_blueprint = []
  2456. self._server = _Server(
  2457. self,
  2458. path_mapping=self._path_mapping,
  2459. flask=self._flask,
  2460. async_mode=app_config.get("async_mode"),
  2461. allow_upgrades=not app_config.get("notebook_proxy"),
  2462. server_config=app_config.get("server_config"),
  2463. )
  2464. self._bindings()._new_scopes()
  2465. def __init_ngrok(self):
  2466. app_config = self._config.config
  2467. if hasattr(self, "_ngrok"):
  2468. # Keep the ngrok instance if token has not changed
  2469. if app_config.get("ngrok_token") == self._ngrok[1]:
  2470. _TaipyLogger._get_logger().info(f" * NGROK Public Url: {self._ngrok[0].public_url}")
  2471. return
  2472. # Close the old tunnel so new tunnel can open for new token
  2473. ngrok.disconnect(self._ngrok[0].public_url) # type: ignore[reportPossiblyUnboundVariable]
  2474. if app_config.get("run_server") and (token := app_config.get("ngrok_token")): # pragma: no cover
  2475. if not util.find_spec("pyngrok"):
  2476. raise RuntimeError("Cannot use ngrok as pyngrok package is not installed.")
  2477. ngrok.set_auth_token(token) # type: ignore[reportPossiblyUnboundVariable]
  2478. self._ngrok = (ngrok.connect(app_config.get("port"), "http"), token) # type: ignore[reportPossiblyUnboundVariable]
  2479. _TaipyLogger._get_logger().info(f" * NGROK Public Url: {self._ngrok[0].public_url}")
  2480. def __bind_default_function(self):
  2481. with self.get_flask_app().app_context():
  2482. if additional_pages := _Hooks()._get_additional_pages():
  2483. # add page context for additional pages so that they can be managed by the variable directory
  2484. for page in additional_pages:
  2485. if isinstance(page, Page) and not isinstance(page, CustomPage):
  2486. self._add_page_context(page)
  2487. self.__var_dir.process_imported_var()
  2488. # bind on_* function if available
  2489. self.__bind_local_func("on_init")
  2490. self.__bind_local_func("on_change")
  2491. self.__bind_local_func("on_action")
  2492. self.__bind_local_func("on_page_load")
  2493. self.__bind_local_func("on_navigate")
  2494. self.__bind_local_func("on_exception")
  2495. self.__bind_local_func("on_status")
  2496. self.__bind_local_func("on_user_content")
  2497. def __register_blueprint(self):
  2498. # add en empty main page if it is not defined
  2499. if Gui.__root_page_name not in self._config.routes:
  2500. new_page = _Page()
  2501. new_page._route = Gui.__root_page_name
  2502. new_page._renderer = _EmptyPage()
  2503. self._config.pages.append(new_page)
  2504. self._config.routes.append(Gui.__root_page_name)
  2505. pages_bp = Blueprint("taipy_pages", __name__)
  2506. self._flask_blueprint.append(pages_bp)
  2507. # server URL Rule for taipy images
  2508. images_bp = Blueprint("taipy_images", __name__)
  2509. images_bp.add_url_rule(f"/{Gui.__CONTENT_ROOT}/<path:path>", view_func=self.__serve_content)
  2510. self._flask_blueprint.append(images_bp)
  2511. # server URL for uploaded files
  2512. upload_bp = Blueprint("taipy_upload", __name__)
  2513. upload_bp.add_url_rule(f"/{Gui.__UPLOAD_URL}", view_func=self.__upload_files, methods=["POST"])
  2514. self._flask_blueprint.append(upload_bp)
  2515. # server URL for user content
  2516. user_content_bp = Blueprint("taipy_user_content", __name__)
  2517. user_content_bp.add_url_rule(f"/{Gui.__USER_CONTENT_URL}/<path:path>", view_func=self.__serve_user_content)
  2518. self._flask_blueprint.append(user_content_bp)
  2519. # server URL for extension resources
  2520. extension_bp = Blueprint("taipy_extensions", __name__)
  2521. extension_bp.add_url_rule(f"/{Gui._EXTENSION_ROOT}/<path:path>", view_func=self.__serve_extension)
  2522. scripts = [
  2523. s if bool(urlparse(s).netloc) else f"{Gui._EXTENSION_ROOT}/{name}/{s}{lib.get_query(s)}"
  2524. for name, libs in Gui.__extensions.items()
  2525. for lib in libs
  2526. for s in (lib._do_get_relative_paths(lib.get_scripts()))
  2527. ]
  2528. styles = [
  2529. s if bool(urlparse(s).netloc) else f"{Gui._EXTENSION_ROOT}/{name}/{s}{lib.get_query(s)}"
  2530. for name, libs in Gui.__extensions.items()
  2531. for lib in libs
  2532. for s in (lib._do_get_relative_paths(lib.get_styles()))
  2533. ]
  2534. if self._get_config("stylekit", True):
  2535. styles.append("stylekit/stylekit.css")
  2536. else:
  2537. styles.append(Gui.__ROBOTO_FONT)
  2538. if self.__css_file:
  2539. styles.append(f"{self.__css_file}")
  2540. if self.__script_files:
  2541. scripts.extend(self.__script_files)
  2542. self._flask_blueprint.append(extension_bp)
  2543. _webapp_path = self._get_webapp_path()
  2544. self._flask_blueprint.append(
  2545. self._server._get_default_blueprint(
  2546. static_folder=_webapp_path,
  2547. template_folder=_webapp_path,
  2548. title=self._get_config("title", "Taipy App"),
  2549. favicon=self._get_config("favicon", Gui.__DEFAULT_FAVICON_URL),
  2550. root_margin=self._get_config("margin", None),
  2551. scripts=scripts,
  2552. styles=styles,
  2553. version=self.__get_version(),
  2554. client_config=self.__get_client_config(),
  2555. watermark=self._get_config("watermark", None),
  2556. css_vars=self.__get_css_vars(),
  2557. base_url=self._get_config("base_url", "/"),
  2558. )
  2559. )
  2560. # Run parse markdown to force variables binding at runtime
  2561. pages_bp.add_url_rule(f"/{Gui.__JSX_URL}/<path:page_name>", view_func=self.__render_page)
  2562. # server URL Rule for flask rendered react-router
  2563. pages_bp.add_url_rule(f"/{Gui.__INIT_URL}", view_func=self.__init_route)
  2564. _Hooks()._add_external_blueprint(self, __name__)
  2565. # Register Flask Blueprint if available
  2566. for bp in self._flask_blueprint:
  2567. t.cast(Flask, self._server.get_flask()).register_blueprint(bp)
  2568. def _get_accessor(self):
  2569. if self.__accessors is None:
  2570. self.__accessors = _DataAccessors(self)
  2571. return self.__accessors
  2572. def run(
  2573. self,
  2574. run_server: bool = True,
  2575. run_in_thread: bool = False,
  2576. async_mode: str = "gevent",
  2577. **kwargs,
  2578. ) -> t.Optional[Flask]:
  2579. """Start the server that delivers pages to web clients.
  2580. Once you enter `run()`, users can run web browsers and point to the web server
  2581. URL that `Gui` serves. The default is to listen to the *localhost* address
  2582. (127.0.0.1) on the port number 5000. However, the configuration of this `Gui`
  2583. object may impact that (see the
  2584. [Configuration](../../../../../userman/advanced_features/configuration/gui-config.md#configuring-the-gui-instance)
  2585. section of the User Manual for details).
  2586. Arguments:
  2587. run_server (bool): Whether or not to run a web server locally.
  2588. If set to *False*, a web server is *not* created and started.
  2589. run_in_thread (bool): Whether or not to run a web server in a separated thread.
  2590. If set to *True*, the web server runs is a separated thread.<br/>
  2591. Note that if you are running in an IPython notebook context, the web
  2592. server always runs in a separate thread.
  2593. async_mode (str): The asynchronous model to use for the Flask-SocketIO.
  2594. Valid values are:<br/>
  2595. - "gevent": Use a [gevent](https://www.gevent.org/servers.html) server.
  2596. - "threading": Use the Flask Development Server. This allows the application to use
  2597. the Flask reloader (the *use_reloader* option) and Debug mode (the *debug* option).
  2598. - "eventlet": Use an [*eventlet*](https://flask.palletsprojects.com/en/2.2.x/deploying/eventlet/)
  2599. event-driven WSGI server.
  2600. The default value is "gevent"<br/>
  2601. Note that only the "threading" value provides support for the development reloader
  2602. functionality (*use_reloader* option). Any other value makes the *use_reloader* configuration parameter
  2603. ignored.<br/>
  2604. Also note that setting the *debug* argument to True forces *async_mode* to "threading".
  2605. **kwargs (dict[str, any]): Additional keyword arguments that configure how this `Gui` is run.
  2606. Please refer to the gui config section
  2607. [page](../../../../../userman/advanced_features/configuration/gui-config.md#configuring-the-gui-instance)
  2608. of the User Manual for more information.
  2609. Returns:
  2610. The Flask instance if *run_server* is False else None.
  2611. """
  2612. # --------------------------------------------------------------------------------
  2613. # The ssl_context argument was removed just after 1.1. It was defined as:
  2614. # t.Optional[t.Union[ssl.SSLContext, t.Tuple[str, t.Optional[str]], t.Literal["adhoc"]]] = None
  2615. #
  2616. # With the doc:
  2617. # ssl_context (Optional[Union[ssl.SSLContext, Tuple[str, Optional[str]], t.Literal['adhoc']]]):
  2618. # Configures TLS to serve over HTTPS. This value can be:
  2619. #
  2620. # - An `ssl.SSLContext` object
  2621. # - A `(cert_file, key_file)` tuple to create a typical context
  2622. # - The string "adhoc" to generate a temporary self-signed certificate.
  2623. #
  2624. # The default value is None.
  2625. # --------------------------------------------------------------------------------
  2626. app_config = self._config.config
  2627. run_root_dir = os.path.dirname(getabsfile(self.__frame))
  2628. # Register _root_dir for abs path
  2629. if not hasattr(self, "_root_dir"):
  2630. self._root_dir = run_root_dir
  2631. is_reloading = kwargs.pop("_reload", False)
  2632. if not is_reloading:
  2633. self.__run_kwargs = kwargs = {
  2634. **kwargs,
  2635. "run_server": run_server,
  2636. "run_in_thread": run_in_thread,
  2637. "async_mode": async_mode,
  2638. }
  2639. # Load application config from multiple sources (env files, kwargs, command line)
  2640. self._config._build_config(run_root_dir, self.__env_filename, kwargs)
  2641. self._config.resolve()
  2642. TaipyGuiWarning.set_debug_mode(self._get_config("debug", False))
  2643. # setup run function with gui hooks
  2644. _Hooks().run(self, **kwargs)
  2645. self.__init_server()
  2646. self.__init_ngrok()
  2647. locals_bind = _filter_locals(self.__frame.f_locals)
  2648. self.__locals_context.set_default(locals_bind, t.cast(str, self.__default_module_name))
  2649. self.__var_dir.set_default(self.__frame)
  2650. self.__bind_default_function()
  2651. if self.__state is None or is_reloading:
  2652. self.__state = _GuiState(
  2653. self, self.__locals_context.get_all_keys(), self.__locals_context.get_all_context()
  2654. )
  2655. if _is_in_notebook():
  2656. # Allow gui.state.x in notebook mode
  2657. self.state = self.__state
  2658. """Only defined when running in an IPython notebook context.
  2659. The unique instance of State that you can use to change bound variables directly,
  2660. potentially impacting the user interface in real-time.
  2661. """
  2662. # Base global ctx is TaipyHolder classes + script modules and callables
  2663. glob_ctx: t.Dict[str, t.Any] = {t.__name__: t for t in _TaipyBase.__subclasses__()}
  2664. glob_ctx[Gui.__SELF_VAR] = self
  2665. glob_ctx["state"] = self.__state
  2666. glob_ctx.update({k: v for k, v in locals_bind.items() if ismodule(v) or callable(v)})
  2667. # Call on_init on each library
  2668. for name, libs in self.__extensions.items():
  2669. for lib in libs:
  2670. if not isinstance(lib, ElementLibrary):
  2671. continue
  2672. try:
  2673. lib_context = lib.on_init(self)
  2674. if (
  2675. isinstance(lib_context, tuple)
  2676. and len(lib_context) > 1
  2677. and isinstance(lib_context[0], str)
  2678. and lib_context[0].isidentifier()
  2679. ):
  2680. if lib_context[0] in glob_ctx:
  2681. _warn(f"Method {name}.on_init() returned a name already defined '{lib_context[0]}'.")
  2682. else:
  2683. glob_ctx[lib_context[0]] = lib_context[1]
  2684. elif lib_context:
  2685. _warn(
  2686. f"Method {name}.on_init() should return a Tuple[str, Any] where the first element must be a valid Python identifier." # noqa: E501
  2687. )
  2688. except Exception as e: # pragma: no cover
  2689. if not self._call_on_exception(f"{name}.on_init", e):
  2690. _warn(f"Method {name}.on_init() raised an exception", e)
  2691. # Initiate the Evaluator with the right context
  2692. self.__evaluator = _Evaluator(glob_ctx, self.__shared_variables)
  2693. self.__register_blueprint()
  2694. # Register data accessor communication data format (JSON, Apache Arrow)
  2695. self._get_accessor().set_data_format(
  2696. _DataFormat.APACHE_ARROW if app_config.get("use_arrow") else _DataFormat.JSON
  2697. )
  2698. # Use multi user or not
  2699. self._bindings()._set_single_client(bool(app_config.get("single_client")))
  2700. # Start Flask Server
  2701. if not run_server:
  2702. return self.get_flask_app()
  2703. return self._server.run(
  2704. host=app_config.get("host"),
  2705. port=app_config.get("port"),
  2706. client_url=app_config.get("client_url"),
  2707. debug=app_config.get("debug"),
  2708. use_reloader=app_config.get("use_reloader"),
  2709. flask_log=app_config.get("flask_log"),
  2710. run_in_thread=app_config.get("run_in_thread"),
  2711. allow_unsafe_werkzeug=app_config.get("allow_unsafe_werkzeug"),
  2712. notebook_proxy=app_config.get("notebook_proxy"),
  2713. port_auto_ranges=app_config.get("port_auto_ranges"),
  2714. )
  2715. def reload(self): # pragma: no cover
  2716. """Reload the web server.
  2717. This function reloads the underlying web server only in the situation where
  2718. it was run in a separated thread: the *run_in_thread* parameter to the
  2719. `(Gui.)run^` method was set to True, or you are running in an IPython notebook
  2720. context.
  2721. """
  2722. if hasattr(self, "_server") and hasattr(self._server, "_thread") and self._server._is_running:
  2723. self._server.stop_thread()
  2724. self.run(**self.__run_kwargs, _reload=True)
  2725. _TaipyLogger._get_logger().info("Gui server has been reloaded.")
  2726. def stop(self):
  2727. """Stop the web server.
  2728. This function stops the underlying web server only in the situation where
  2729. it was run in a separated thread: the *run_in_thread* parameter to the
  2730. `(Gui.)run()^` method was set to True, or you are running in an IPython notebook
  2731. context.
  2732. """
  2733. if hasattr(self, "_server") and hasattr(self._server, "_thread") and self._server._is_running:
  2734. self._server.stop_thread()
  2735. _TaipyLogger._get_logger().info("Gui server has been stopped.")
  2736. def _get_authorization(self, client_id: t.Optional[str] = None, system: t.Optional[bool] = False):
  2737. try:
  2738. return _Hooks()._get_authorization(self, client_id, system) or contextlib.nullcontext()
  2739. except Exception as e:
  2740. _warn("Hooks:", e)
  2741. return contextlib.nullcontext()
  2742. def set_favicon(self, favicon_path: t.Union[str, Path], state: t.Optional[State] = None):
  2743. """Change the favicon for all clients.
  2744. This function dynamically changes the favicon (the icon associated with the application's
  2745. pages) of Taipy GUI pages for a single or all connected clients.
  2746. Note that the *favicon* parameter to `(Gui.)run()^` can also be used to change
  2747. the favicon when the application starts.
  2748. Arguments:
  2749. favicon_path: The path to the image file to use.<br/>
  2750. This can be expressed as a path name or a URL (relative or not).
  2751. state: The state to apply the change to.<br/>
  2752. If no set or set to None, all the application's clients are impacted.
  2753. """
  2754. if state or self.__favicon != favicon_path:
  2755. if not state:
  2756. self.__favicon = favicon_path
  2757. url = self._get_content("__taipy_favicon", favicon_path, True)
  2758. self._broadcast(
  2759. "taipy_favicon", url, self._get_client_id() if state else None, message_type=_WsType.FAVICON
  2760. )
  2761. @staticmethod
  2762. def _add_event_listener(
  2763. event_name: str,
  2764. listener: t.Union[
  2765. t.Callable[[str, t.Dict[str, t.Any]], None], t.Callable[[State, str, t.Dict[str, t.Any]], None]
  2766. ],
  2767. with_state: t.Optional[bool] = False,
  2768. ):
  2769. _Hooks()._add_event_listener(event_name, listener, with_state)
  2770. def _fire_event(
  2771. self, event_name: str, client_id: t.Optional[str] = None, payload: t.Optional[t.Dict[str, t.Any]] = None
  2772. ):
  2773. # the event manager will take care of starting the thread
  2774. # once the current callback (or the next one) is finished
  2775. self.__event_manager._add_thread(
  2776. Thread(
  2777. target=self.__do_fire_event,
  2778. args=(event_name, client_id, payload),
  2779. )
  2780. )
  2781. def __do_fire_event(
  2782. self, event_name: str, client_id: t.Optional[str] = None, payload: t.Optional[t.Dict[str, t.Any]] = None
  2783. ):
  2784. this_sid = None
  2785. if request:
  2786. # avoid messing with the client_id => Set(ws id)
  2787. this_sid = getattr(request, "sid", None)
  2788. request.sid = None # type: ignore[attr-defined]
  2789. try:
  2790. with self.get_flask_app().app_context(), self.__event_manager:
  2791. if client_id:
  2792. setattr(g, Gui.__ARG_CLIENT_ID, client_id)
  2793. _Hooks()._fire_event(event_name, client_id, payload)
  2794. finally:
  2795. if this_sid:
  2796. request.sid = this_sid # type: ignore[attr-defined]