Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces Jupyter Notebook Extras to automatically load interactive conveniences like explore_dataframe(), %dpip, and %%sparksql when importing the package inside an IPython kernel. It includes configuration options to opt out of these extras, updates dependencies, and adds comprehensive tests. The review feedback highlights a potential memory leak in _ipython.py due to strong references to the IPython shell in _SHELL_STATES, recommending the use of weakref.WeakKeyDictionary. Additionally, a minor grammatical redundancy was pointed out in the README.md documentation.
Automatically initialize interactive notebook extras when importing google.cloud.managed_spark_connect inside an IPython kernel: - Inject colabsqlviz's explore_dataframe() into IPython user_ns - Load the %dpip line magic extension (google.cloud.managed_spark_magics) - Load the %%sparksql cell magic extension (sparksql_magic) - Add google-colabsqlviz>=0.3.0 and sparksql-magic>=0.0.3 to dependencies Add opt-out and runtime configuration controls: - Environment variable: MANAGED_SPARK_CONNECT_ENABLE_EXTRAS=false - IPython traitlet: ManagedSparkConnect.enable_extras = False (supports both persistent file-based config and runtime toggling via %config, tracking and only undoing changes that managed_spark_connect itself performed).
95f3ebd to
62a606f
Compare
| # Imported directly by managed_spark_connect._ipython and | ||
| # managed_spark_magics; previously these only arrived transitively via | ||
| # google-colabsqlviz and sparksql-magic. | ||
| "ipython>=8.0", |
There was a problem hiding this comment.
I feel like we may want to make all of these optional - we have customers that do not use notebooks w/ Spark Connect, for them these dependencies are not necessary.
| # google-colabsqlviz and sparksql-magic. | ||
| "ipython>=8.0", | ||
| "packaging>=20.0", | ||
| "pyspark[connect]~=4.0.0", |
There was a problem hiding this comment.
Did we decide to remove this dependency and rely on manually installed one, so it can work w/ pyspark-client as well?
| except Exception: | ||
| pass | ||
|
|
||
| _init_extras() |
There was a problem hiding this comment.
Did we measure latency of this call?
Automatically initialize interactive notebook extras when importing google.cloud.managed_spark_connect inside an IPython kernel:
Add opt-out and runtime configuration controls:
Dependency & Footprint Justification
The new hard dependencies added by this PR (
google-colabsqlviz,sparksql-magic) add just a few MiB of wheel downloads. In more detail:All core data dependencies (
pyspark,pandas>=2.0.0,pyarrow>=10.0.1,protobuf>=4.24.0,packaging>=20.0) are already satisfied bypyspark[connect]andgoogle-api-core. Installing both libraries causes 0 upgrades or downgrades to existing packages.sparksql-magicHas Zero Extra Transitive Cost:sparksql-magic(4.2 KiBwheel,6.9 KiBextracted,14.4 KiBwith.pyc) only depends onpysparkandipython—a strict subset ofgoogle-colabsqlvizandipykernel. Adding it alongsidegoogle-colabsqlvizadds 0 additional transitive dependencies.In local notebook kernels (e.g., VS Code or Jupyter, which require
ipykernel), the entireipythonstack is already present. Adding both libraries requires downloading just 3.59 MiB of wheels (~1.7% of the existing install size dominated bypyspark@ ~435 MiB wheel andpyarrow@ ~130 MiB):.pyc, e.g.uv).pyc(pip)pip)ipykernel+ipywidgets)google-colabsqlviz,sparksql-magic,anywidget,psygnal)ipykernelonly)ipywidgets,jupyterlab-widgets,widgetsnbextension)ipythonstack)(Note: Pure Python code across all 7 packages added in the minimal VS Code kernel scenario is < 1 MiB. The remaining extracted space is static frontend JS bundles/source maps in
widgetsnbextensionandanywidget[~11.5 MiB],psygnal's compiled mypyc.sobinary [~1.3 MiB], andpip's.pycbytecode cache duplicating embedded JS strings incolabsqlviz[~1.0 MiB].)