4
4
import copy , hashlib , numpy as np , pandas as pd , pyarrow as pa , sys , uuid
5
5
from functools import lru_cache
6
6
from weakref import WeakValueDictionary
7
+ import warnings
7
8
8
9
from graphistry .privacy import Privacy , Mode
9
- from graphistry .client_session import ClientSession
10
+ from graphistry .client_session import ClientSession , SessionManagerProtocol , DatasetInfo
10
11
11
12
from .constants import SRC , DST , NODE
12
13
from .plugins_types .kusto_types import KustoConfig
@@ -121,8 +122,17 @@ def reset_caches(self):
121
122
def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
122
123
super ().__init__ (* args , ** kwargs )
123
124
124
- from .pygraphistry import PyGraphistry
125
- self ._session = kwargs .get ('graphistry_client_session' , PyGraphistry ._session )
125
+ # Use late import to avoid circular dependency
126
+ pygraphistry = kwargs .get ('pygraphistry_session' , None )
127
+ if pygraphistry is None :
128
+ from .pygraphistry import PyGraphistry
129
+ pygraphistry = PyGraphistry
130
+
131
+ if self is PyGraphistry :
132
+ # NOTE: This may use global session when that isn't desired.
133
+ warnings .warn ("Plotter initialized without pygraphistry_session, falling back to global PyGraphistry" , UserWarning )
134
+ self ._pygraphistry : SessionManagerProtocol = pygraphistry
135
+ self ._session : ClientSession = self ._pygraphistry ._session
126
136
127
137
# Bindings
128
138
self ._edges : Any = None
@@ -1502,21 +1512,19 @@ def server(self, v: Optional[str] = None) -> str:
1502
1512
1503
1513
Note that sets are global as PyGraphistry._config entries, so be careful in multi-user environments.
1504
1514
"""
1505
- from .pygraphistry import PyGraphistry
1506
1515
if v is not None :
1507
- PyGraphistry ._session .hostname = v
1508
- return PyGraphistry ._session .hostname
1516
+ self ._session .hostname = v
1517
+ return self ._session .hostname
1509
1518
1510
1519
def protocol (self , v : Optional [str ] = None ) -> str :
1511
1520
"""
1512
1521
Get or set the server protocol, e.g., "https"
1513
1522
1514
1523
Note that sets are global as PyGraphistry._config entries, so be careful in multi-user environments.
1515
1524
"""
1516
- from .pygraphistry import PyGraphistry
1517
1525
if v is not None :
1518
- PyGraphistry ._session .protocol = v
1519
- return PyGraphistry ._session .protocol
1526
+ self ._session .protocol = v
1527
+ return self ._session .protocol
1520
1528
1521
1529
def client_protocol_hostname (self , v : Optional [str ] = None ) -> str :
1522
1530
"""
@@ -1526,18 +1534,15 @@ def client_protocol_hostname(self, v: Optional[str] = None) -> str:
1526
1534
1527
1535
Note that sets are global as PyGraphistry._config entries, so be careful in multi-user environments.
1528
1536
"""
1529
- from .pygraphistry import PyGraphistry
1530
1537
if v is not None :
1531
- PyGraphistry ._session .client_protocol_hostname = v
1532
- return PyGraphistry ._session .client_protocol_hostname or f"{ PyGraphistry .protocol ()} ://{ PyGraphistry .server ()} "
1538
+ self ._session .client_protocol_hostname = v
1539
+ return self ._session .client_protocol_hostname or f"{ self .protocol ()} ://{ self .server ()} "
1533
1540
1534
1541
def base_url_server (self , v : Optional [str ] = None ) -> str :
1535
- from .pygraphistry import PyGraphistry
1536
- return "%s://%s" % (PyGraphistry .protocol (), PyGraphistry .server ())
1542
+ return "%s://%s" % (self .protocol (), self .server ())
1537
1543
1538
1544
def base_url_client (self , v : Optional [str ] = None ) -> str :
1539
- from .pygraphistry import PyGraphistry
1540
- return PyGraphistry .client_protocol_hostname ()
1545
+ return self .client_protocol_hostname ()
1541
1546
1542
1547
def upload (
1543
1548
self ,
@@ -1658,8 +1663,7 @@ def plot(
1658
1663
.plot(es)
1659
1664
1660
1665
"""
1661
- from .pygraphistry import PyGraphistry
1662
- logger .debug ("1. @PloatterBase plot: PyGraphistry.org_name(): {}" .format (PyGraphistry .org_name ()))
1666
+ logger .debug ("1. @PloatterBase plot: _pygraphistry.org_name: {}" .format (self ._session .org_name ))
1663
1667
1664
1668
if graph is None :
1665
1669
if self ._edges is None :
@@ -1673,36 +1677,34 @@ def plot(
1673
1677
1674
1678
self ._check_mandatory_bindings (not isinstance (n , type (None )))
1675
1679
1676
- # from .pygraphistry import PyGraphistry
1677
- api_version = PyGraphistry .api_version ()
1678
- logger .debug ("2. @PloatterBase plot: PyGraphistry.org_name(): {}" .format (PyGraphistry .org_name ()))
1680
+ logger .debug ("2. @PloatterBase plot: self._pygraphistry.org_name: {}" .format (self ._session .org_name ))
1679
1681
dataset : Union [ArrowUploader , Dict [str , Any ], None ] = None
1680
- if api_version == 1 :
1682
+ if self . _session . api_version == 1 :
1681
1683
dataset = self ._plot_dispatch (g , n , name , description , 'json' , self ._style , memoize )
1682
1684
if skip_upload :
1683
1685
return dataset
1684
- info = PyGraphistry ._etl1 (dataset )
1685
- elif api_version == 3 :
1686
- logger .debug ("3. @PloatterBase plot: PyGraphistry. org_name() : {}" .format (PyGraphistry . org_name () ))
1687
- PyGraphistry .refresh ()
1688
- logger .debug ("4. @PloatterBase plot: PyGraphistry. org_name() : {}" .format (PyGraphistry . org_name () ))
1686
+ info = self . _pygraphistry ._etl1 (dataset )
1687
+ elif self . _session . api_version == 3 :
1688
+ logger .debug ("3. @PloatterBase plot: self._pygraphistry. org_name: {}" .format (self . _session . org_name ))
1689
+ self . _pygraphistry .refresh ()
1690
+ logger .debug ("4. @PloatterBase plot: self._pygraphistry. org_name: {}" .format (self . _session . org_name ))
1689
1691
1690
1692
uploader = dataset = self ._plot_dispatch_arrow (g , n , name , description , self ._style , memoize )
1691
1693
assert uploader is not None
1692
1694
if skip_upload :
1693
1695
return uploader
1694
- uploader .token = PyGraphistry . api_token () # type: ignore[assignment]
1696
+ uploader .token = self . _session . api_token # type: ignore[assignment]
1695
1697
uploader .post (as_files = as_files , memoize = memoize , validate = validate , erase_files_on_fail = erase_files_on_fail )
1696
1698
uploader .maybe_post_share_link (self )
1697
- info = {
1699
+ info : DatasetInfo = {
1698
1700
'name' : uploader .dataset_id ,
1699
1701
'type' : 'arrow' ,
1700
1702
'viztoken' : str (uuid .uuid4 ())
1701
1703
}
1702
1704
1703
- viz_url = PyGraphistry ._viz_url (info , self ._url_params )
1704
- cfg_client_protocol_hostname = PyGraphistry ._session .client_protocol_hostname
1705
- full_url = ('%s:%s' % (PyGraphistry ._session .protocol , viz_url )) if cfg_client_protocol_hostname is None else viz_url
1705
+ viz_url = self . _pygraphistry ._viz_url (info , self ._url_params )
1706
+ cfg_client_protocol_hostname = self ._session .client_protocol_hostname
1707
+ full_url = ('%s:%s' % (self ._session .protocol , viz_url )) if cfg_client_protocol_hostname is None else viz_url
1706
1708
1707
1709
render_mode = resolve_render_mode (self , render )
1708
1710
if render_mode == "url" :
@@ -2245,8 +2247,6 @@ def _make_dataset(self, edges, nodes, name, description, mode, metadata=None, me
2245
2247
# Main helper for creating ETL1 payload
2246
2248
def _make_json_dataset (self , edges , nodes , name ) -> Dict [str , Any ]:
2247
2249
2248
- from .pygraphistry import PyGraphistry
2249
-
2250
2250
def flatten_categorical (df ):
2251
2251
# Avoid cat_col.where(...)-related exceptions
2252
2252
df2 = df .copy ()
@@ -2260,7 +2260,7 @@ def flatten_categorical(df):
2260
2260
2261
2261
bindings = {'idField' : self ._node or PlotterBase ._defaultNodeId ,
2262
2262
'destinationField' : self ._destination , 'sourceField' : self ._source }
2263
- dataset : Dict [str , Any ] = {'name' : PyGraphistry ._session .dataset_prefix + name ,
2263
+ dataset : Dict [str , Any ] = {'name' : self ._session .dataset_prefix + name ,
2264
2264
'bindings' : bindings , 'type' : 'edgelist' , 'graph' : edict }
2265
2265
2266
2266
if nlist is not None :
@@ -2271,20 +2271,20 @@ def flatten_categorical(df):
2271
2271
2272
2272
def _make_arrow_dataset (self , edges : pa .Table , nodes : pa .Table , name : str , description : str , metadata : Optional [Dict [str , Any ]]) -> ArrowUploader :
2273
2273
2274
- from .pygraphistry import PyGraphistry
2275
2274
au : ArrowUploader = ArrowUploader (
2276
- server_base_path = PyGraphistry .protocol () + '://' + PyGraphistry .server (),
2275
+ client_session = self ._session ,
2276
+ server_base_path = self ._session .protocol + '://' + self ._session .hostname ,
2277
2277
edges = edges , nodes = nodes ,
2278
2278
name = name , description = description ,
2279
2279
metadata = {
2280
- 'usertag' : PyGraphistry ._tag ,
2281
- 'key' : PyGraphistry . api_key () ,
2280
+ 'usertag' : self . _session ._tag ,
2281
+ 'key' : self . _session . api_key ,
2282
2282
'agent' : 'pygraphistry' ,
2283
2283
'apiversion' : '3' ,
2284
2284
'agentversion' : sys .modules ['graphistry' ].__version__ , # type: ignore
2285
2285
** (metadata or {})
2286
2286
},
2287
- certificate_validation = PyGraphistry .certificate_validation ())
2287
+ certificate_validation = self . _pygraphistry .certificate_validation ())
2288
2288
2289
2289
au .edge_encodings = au .g_to_edge_encodings (self )
2290
2290
au .node_encodings = au .g_to_node_encodings (self )
@@ -2485,10 +2485,8 @@ def cypher(self, query: str, params: Dict[str, Any] = {}) -> Plottable:
2485
2485
2486
2486
"""
2487
2487
2488
- from .pygraphistry import PyGraphistry
2489
-
2490
2488
res = copy .copy (self )
2491
- driver = self ._bolt_driver or PyGraphistry ._session .bolt_driver
2489
+ driver = self ._bolt_driver or self ._session .bolt_driver
2492
2490
if driver is None :
2493
2491
raise ValueError ("BOLT connection information not provided. Must first call graphistry.register(bolt=...) or g.bolt(...)." )
2494
2492
with driver .session () as session :
0 commit comments