gui.py 114 KB

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