1
+ import os
2
+ import logging
3
+ import shutil
4
+ import pwd
5
+ import getpass
6
+ from jupyter_pgweb_proxy .jupyter_config import config
7
+
8
+
9
+ def _get_env (port , base_url ):
10
+ """
11
+ Returns a dict containing environment settings to launch the Web App.
12
+ Args:
13
+ port (int): Port number on which the Web app will be started. Ex: 8888
14
+ base_url (str): Controls the prefix in the url on which
15
+ the Web App will be available.
16
+ Ex: localhost:8888/base_url/index.html
17
+ Returns:
18
+ [Dict]: Containing environment settings to launch the Web application.
19
+ """
20
+
21
+ return {
22
+ "PGWEB_RUN_PORT" : str (port ),
23
+ "PGWEB_URL_PREFIX" : f"{ base_url } pgweb/" .lstrip ("/" ),
24
+ }
25
+
26
+
27
+ def get_icon_path ():
28
+ return os .path .join (
29
+ os .path .dirname (os .path .abspath (__file__ )), "pgweb.svg"
30
+ )
31
+
32
+
33
+ def _get_timeout (default = 15 ):
34
+ try :
35
+ return float (os .getenv ('RSESSION_TIMEOUT' , default ))
36
+ except Exception :
37
+ return default
38
+
39
+
40
+ def get_system_user ():
41
+ try :
42
+ user = pwd .getpwuid (os .getuid ())[0 ]
43
+ except Exception :
44
+ user = os .getenv ('USER' , getpass .getuser ())
45
+ return user
46
+
47
+
48
+ def run_app ():
49
+ """
50
+ Setup application.
51
+ This method is run by jupyter-server-proxy package to launch the Web app.
52
+ """
53
+
54
+ logging .basicConfig (level = "INFO" )
55
+ logger = logging .getLogger ("pgweb" )
56
+ logger .setLevel ("INFO" )
57
+ logger .info ("Initializing Jupyter pgweb Proxy" )
58
+
59
+ icon_path = get_icon_path ()
60
+ try :
61
+ executable_name = shutil .which ("pgweb" )
62
+ except Exception :
63
+ executable_name = "pgweb"
64
+ host = "127.0.0.1"
65
+ user = get_system_user ()
66
+ logger .debug (f"[{ user } ] Icon_path: { icon_path } " )
67
+ logger .debug (f"[{ user } ] Launch Command: { executable_name } " )
68
+ return {
69
+ "command" : [
70
+ executable_name ,
71
+ f"--host={ host } " ,
72
+ "--listen={port}" ,
73
+ "--debug" ,
74
+ "--sessions" ,
75
+ ],
76
+ "timeout" : 100 ,
77
+ "environment" : _get_env ,
78
+ "absolute_url" : True ,
79
+ # "rewrite_response": rewrite_netloc,
80
+ "launcher_entry" : {
81
+ "title" : "pgweb" ,
82
+ "icon_path" : icon_path
83
+ },
84
+ }
0 commit comments