123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- # Copyright 2021-2024 Avaiga Private Limited
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- # the License. You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- # specific language governing permissions and limitations under the License.
- from __future__ import annotations
- import contextlib
- import logging
- import os
- import pathlib
- import re
- import sys
- import time
- import typing as t
- import webbrowser
- from importlib import util
- from random import randint
- from flask import Blueprint, Flask, json, jsonify, render_template, request, send_from_directory
- from flask_cors import CORS
- from flask_socketio import SocketIO
- from gitignore_parser import parse_gitignore
- from kthread import KThread
- from werkzeug.serving import is_running_from_reloader
- import __main__
- from taipy.logger._taipy_logger import _TaipyLogger
- from ._renderers.json import _TaipyJsonProvider
- from .config import ServerConfig
- from .custom._page import _ExternalResourceHandlerManager
- from .utils import _is_in_notebook, _is_port_open, _RuntimeManager
- if t.TYPE_CHECKING:
- from .gui import Gui
- class _Server:
- __RE_OPENING_CURLY = re.compile(r"([^\"])(\{)")
- __RE_CLOSING_CURLY = re.compile(r"(\})([^\"])")
- __OPENING_CURLY = r"\1{"
- __CLOSING_CURLY = r"}\2"
- _RESOURCE_HANDLER_ARG = "tprh"
- _CUSTOM_PAGE_META_ARG = "tp_cp_meta"
- def __init__(
- self,
- gui: Gui,
- flask: t.Optional[Flask] = None,
- path_mapping: t.Optional[dict] = None,
- async_mode: t.Optional[str] = None,
- allow_upgrades: bool = True,
- server_config: t.Optional[ServerConfig] = None,
- ):
- self._gui = gui
- server_config = server_config or {}
- self._flask = flask
- if self._flask is None:
- flask_config: t.Dict[str, t.Any] = {"import_name": "Taipy"}
- if "flask" in server_config and isinstance(server_config["flask"], dict):
- flask_config.update(server_config["flask"])
- self._flask = Flask(**flask_config)
- if "SECRET_KEY" not in self._flask.config or not self._flask.config["SECRET_KEY"]:
- self._flask.config["SECRET_KEY"] = "TaIpY"
- # setup cors
- if "cors" not in server_config or (
- "cors" in server_config and (isinstance(server_config["cors"], dict) or server_config["cors"] is True)
- ):
- cors_config = (
- server_config["cors"] if "cors" in server_config and isinstance(server_config["cors"], dict) else {}
- )
- CORS(self._flask, **cors_config)
- # setup socketio
- socketio_config: t.Dict[str, t.Any] = {
- "cors_allowed_origins": "*",
- "ping_timeout": 10,
- "ping_interval": 5,
- "json": json,
- "async_mode": async_mode,
- "allow_upgrades": allow_upgrades,
- }
- if "socketio" in server_config and isinstance(server_config["socketio"], dict):
- socketio_config.update(server_config["socketio"])
- self._ws = SocketIO(self._flask, **socketio_config)
- self._apply_patch()
- # set json encoder (for Taipy specific types)
- self._flask.json_provider_class = _TaipyJsonProvider
- self._flask.json = self._flask.json_provider_class(self._flask) # type: ignore
- self.__path_mapping = path_mapping or {}
- self.__ssl_context = server_config.get("ssl_context", None)
- self._is_running = False
- # Websocket (handle json message)
- # adding args for the one call with a server ack request
- @self._ws.on("message")
- def handle_message(message, *args) -> None:
- if "status" in message:
- _TaipyLogger._get_logger().info(message["status"])
- elif "type" in message:
- gui._manage_message(message["type"], message)
- def __is_ignored(self, file_path: str) -> bool:
- if not hasattr(self, "_ignore_matches"):
- __IGNORE_FILE = ".taipyignore"
- ignore_file = (
- (pathlib.Path(__main__.__file__).parent / __IGNORE_FILE) if hasattr(__main__, "__file__") else None
- )
- if not ignore_file or not ignore_file.is_file():
- ignore_file = pathlib.Path(self._gui._root_dir) / __IGNORE_FILE
- self._ignore_matches = (
- parse_gitignore(ignore_file) if ignore_file.is_file() and os.access(ignore_file, os.R_OK) else None
- )
- if callable(self._ignore_matches):
- return self._ignore_matches(file_path)
- return False
- def _get_default_blueprint(
- self,
- static_folder: str,
- template_folder: str,
- title: str,
- favicon: str,
- root_margin: str,
- scripts: t.List[str],
- styles: t.List[str],
- version: str,
- client_config: t.Dict[str, t.Any],
- watermark: t.Optional[str],
- css_vars: str,
- base_url: str,
- ) -> Blueprint:
- taipy_bp = Blueprint("Taipy", __name__, static_folder=static_folder, template_folder=template_folder)
- # Serve static react build
- @taipy_bp.route("/", defaults={"path": ""})
- @taipy_bp.route("/<path:path>")
- def my_index(path):
- resource_handler_id = request.cookies.get(_Server._RESOURCE_HANDLER_ARG, None)
- if resource_handler_id is not None:
- resource_handler = _ExternalResourceHandlerManager().get(resource_handler_id)
- if resource_handler is None:
- return (f"Invalid value for query {_Server._RESOURCE_HANDLER_ARG}", 404)
- try:
- return resource_handler.get_resources(path, static_folder)
- except Exception as e:
- raise RuntimeError("Can't get resources from custom resource handler") from e
- if path == "" or path == "index.html" or "." not in path:
- try:
- return render_template(
- "index.html",
- title=title,
- favicon=favicon,
- root_margin=root_margin,
- watermark=watermark,
- config=client_config,
- scripts=scripts,
- styles=styles,
- version=version,
- css_vars=css_vars,
- base_url=base_url,
- )
- except Exception:
- raise RuntimeError(
- "Something is wrong with the taipy-gui front-end installation. Check that the js bundle has been properly built (is Node.js installed?)." # noqa: E501
- ) from None
- if path == "taipy.status.json":
- return self._direct_render_json(self._gui._serve_status(pathlib.Path(template_folder) / path))
- if str(os.path.normpath(file_path := ((base_path := static_folder + os.path.sep) + path))).startswith(
- base_path
- ) and os.path.isfile(file_path):
- return send_from_directory(base_path, path)
- # use the path mapping to detect and find resources
- for k, v in self.__path_mapping.items():
- if (
- path.startswith(f"{k}/")
- and str(
- os.path.normpath(file_path := ((base_path := v + os.path.sep) + path[len(k) + 1 :]))
- ).startswith(base_path)
- and os.path.isfile(file_path)
- ):
- return send_from_directory(base_path, path[len(k) + 1 :])
- if (
- hasattr(__main__, "__file__")
- and str(
- os.path.normpath(
- file_path := ((base_path := os.path.dirname(__main__.__file__) + os.path.sep) + path)
- )
- ).startswith(base_path)
- and os.path.isfile(file_path)
- and not self.__is_ignored(file_path)
- ):
- return send_from_directory(base_path, path)
- if (
- str(os.path.normpath(file_path := (base_path := self._gui._root_dir + os.path.sep) + path)).startswith(
- base_path
- )
- and os.path.isfile(file_path)
- and not self.__is_ignored(file_path)
- ):
- return send_from_directory(base_path, path)
- return ("", 404)
- return taipy_bp
- # Update to render as JSX
- def _render(self, html_fragment, style, head, context):
- template_str = _Server.__RE_OPENING_CURLY.sub(_Server.__OPENING_CURLY, html_fragment)
- template_str = _Server.__RE_CLOSING_CURLY.sub(_Server.__CLOSING_CURLY, template_str)
- template_str = template_str.replace('"{!', "{")
- template_str = template_str.replace('!}"', "}")
- return self._direct_render_json(
- {
- "jsx": template_str,
- "style": (style + os.linesep) if style else "",
- "head": head or [],
- "context": context or self._gui._get_default_module_name(),
- }
- )
- def _direct_render_json(self, data):
- return jsonify(data)
- def get_flask(self):
- return self._flask
- def test_client(self):
- return self._flask.test_client()
- def _run_notebook(self):
- self._is_running = True
- self._ws.run(self._flask, host=self._host, port=self._port, debug=False, use_reloader=False)
- def _get_async_mode(self) -> str:
- return self._ws.async_mode
- def _apply_patch(self):
- if self._get_async_mode() == "gevent" and util.find_spec("gevent"):
- from gevent import monkey
- if not monkey.is_module_patched("time"):
- monkey.patch_time()
- if self._get_async_mode() == "eventlet" and util.find_spec("eventlet"):
- from eventlet import monkey_patch, patcher
- if not patcher.is_monkey_patched("time"):
- monkey_patch(time=True)
- def _get_random_port(self): # pragma: no cover
- while True:
- port = randint(49152, 65535)
- if port not in _RuntimeManager().get_used_port() and not _is_port_open(self._host, port):
- return port
- def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_unsafe_werkzeug, notebook_proxy):
- host_value = host if host != "0.0.0.0" else "localhost"
- self._host = host
- if port == "auto":
- port = self._get_random_port()
- self._port = port
- if _is_in_notebook() and notebook_proxy: # pragma: no cover
- from .utils.proxy import NotebookProxy
- # Start proxy if not already started
- self._proxy = NotebookProxy(gui=self._gui, listening_port=port)
- self._proxy.run()
- self._port = self._get_random_port()
- if _is_in_notebook() or run_in_thread:
- runtime_manager = _RuntimeManager()
- runtime_manager.add_gui(self._gui, port)
- if debug and not is_running_from_reloader() and _is_port_open(host_value, port):
- raise ConnectionError(
- "Port {port} is already opened on {host} because another application is running on the same port. Please pick another port number and rerun with the 'port=<new_port>' option. You can also let Taipy choose a port number for you by running with the 'port=\"auto\"' option." # noqa: E501
- )
- if not flask_log:
- log = logging.getLogger("werkzeug")
- log.disabled = True
- if not is_running_from_reloader():
- _TaipyLogger._get_logger().info(f" * Server starting on http://{host_value}:{port}")
- else:
- _TaipyLogger._get_logger().info(f" * Server reloaded on http://{host_value}:{port}")
- if not is_running_from_reloader() and self._gui._get_config("run_browser", False):
- webbrowser.open(f"http://{host_value}{f':{port}' if port else ''}", new=2)
- if _is_in_notebook() or run_in_thread:
- self._thread = KThread(target=self._run_notebook)
- self._thread.start()
- return
- self._is_running = True
- run_config = {
- "app": self._flask,
- "host": host,
- "port": port,
- "debug": debug,
- "use_reloader": use_reloader,
- }
- if self.__ssl_context is not None:
- run_config["ssl_context"] = self.__ssl_context
- # flask-socketio specific conditions for 'allow_unsafe_werkzeug' parameters to be popped out of kwargs
- if self._get_async_mode() == "threading" and (not sys.stdin or not sys.stdin.isatty()):
- run_config = {**run_config, "allow_unsafe_werkzeug": allow_unsafe_werkzeug}
- self._ws.run(**run_config)
- def stop_thread(self):
- if hasattr(self, "_thread") and self._thread.is_alive() and self._is_running:
- self._is_running = False
- with contextlib.suppress(Exception):
- if self._get_async_mode() == "gevent":
- if self._ws.wsgi_server is not None:
- self._ws.wsgi_server.stop()
- else:
- self._thread.kill()
- else:
- self._thread.kill()
- while _is_port_open(self._host, self._port):
- time.sleep(0.1)
- def stop_proxy(self):
- if hasattr(self, "_proxy"):
- self._proxy.stop()
|