Created
July 19, 2026 11:44
-
-
Save vjcitn/46fdf15dad4990cec455ce53605e6082 to your computer and use it in GitHub Desktop.
code provided by google search to 'rewrite' prompt cell with python code, start the cell with %%antigravity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from IPython.core.magic import register_cell_magic | |
| from IPython.display import Javascript | |
| import IPython | |
| @register_cell_magic | |
| def antigravity(line, cell): | |
| """Invokes Google Antigravity CLI to rewrite the current cell based on a prompt.""" | |
| import subprocess | |
| import json | |
| import os | |
| # Target the active notebook file | |
| # (Note: In JupyterLab/Classic, you may need to manually specify your file name string) | |
| notebook_path = line.strip() if line.strip() else "analysis.ipynb" | |
| prompt = cell.strip() | |
| # 1. Ask agy to generate the raw code logic | |
| cmd = f'agy -p "Generate clean Python code for: {prompt}. Output raw executable code only, no markdown formatting."' | |
| try: | |
| generated_code = subprocess.check_output(cmd, shell=True, text=True).strip() | |
| # Strip trailing/leading backticks if the agent returns markdown blocks | |
| if generated_code.startswith("```"): | |
| generated_code = "\n".join(generated_code.split("\n")[1:-1]) | |
| # 2. Inject the code directly into the active cell using frontend Javascript | |
| encoded_code = json.dumps(generated_code) | |
| js_code = f""" | |
| var cell = Jupyter.notebook.get_selected_cell(); | |
| cell.set_text({encoded_code}); | |
| """ | |
| display(Javascript(js_code)) | |
| print("✨ Antigravity code successfully injected!") | |
| except subprocess.CalledProcessError as e: | |
| print(f"❌ Antigravity CLI Error: {e.output}", file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment