This is a guide for working in Google Colab with Gemini. It contains some usage recommendations and workflow guides, and some helper scripts to make switching between a codebase and a colab notebook less painful.
NOTE: You should use Gemini and Google Colab through a Google account that is suitable for the task at hand. Don't use your personal account for work projects, and don't use your work account for personal projects.
Gemini in Colab shares many features with using code assistants in their CLIs (e.g. Claude Code etc):
- Model directly edits code
- Code changes are highlighted before edits are applied
- User retains control of command execution
- Reasoning explained and to-do list produced for complex tasks
- Codebase rules (styling, preferred unit test framework etc) can be set, albeit manually for each session
However, there are some things that CLI code assistants can do but Gemini in Colab cannot:
- No direct reading and editing of files. Instead, code must be copied in and out of a Colab notebook (but see the Workflow Guide for a workaround)
- No automatic mechanism to set a system prompt (but see Usage Tips for a workaround)
- No autonomous running of bash commands e.g. launching UIs
- No memory of previous coding sessions
The main source of friction when using Gemini in Colab is in copying your code into a Colab notebook at the start, and copying it out again at the end. To make this easier, there are helper scripts in this repo that automate this process.
The helper scripts use modules that come with a standard python install, so this repo can run in any python environment. To download the helper scripts, run:
git clone https://github.com/SamuelHLewis/colab_gemini_utilsAnd then move into the repo directory with:
cd colab_gemini_utilscodebase_to_colab.py copies each file in your codebase to a code cell in a jupyter notebook, and writes the name of each file to a markdown cell above its contents. It has the following arguments:
--codebase_path: Path to the codebase directory
--output_notebook: Output notebook filename (e.g. notebook.ipynb)
--gemini_prompt_path: Path to a markdown file containing the system prompt for gemini
A demo codebase is supplied in the demo folder to demonstrate this. You can run the script on the demo repo as follows:
python codebase_to_colab.py --codebase_path=./demo --output_notebook=codebase.ipynb --gemini_prompt_path=./prompts/example_prompt.mdThis will generate a notebook called codebase.ipynb, which you can then upload to Google Drive and start working in Colab with Gemini.
NOTE if the above command doesn't work, you may have to invoke it with python3 instead:
python3 codebase_to_colab.py --codebase_path=./demo --output_notebook=codebase.ipynb --gemini_prompt_path=./prompts/example_prompt.mdNOTE: this script ignores any files that are in the colab.ignore file. If you have secrets, API keys, IP addresses etc that you do not want to copy out of your codebase, add them to colab.ignore and they will be skipped.
colab_to_codebase.py reconstructs your codebase from a colab notebook. It does this by extracting the pairs of markdown cell - code cell from your colab notebook, and writing each to a file, where the file name is what was specified in the markdown cell, and the file contents are what was in the corresponding code cell. It has the following arguments:
--notebook_path: Path to the notebook to be converted to a codebase
Once you have imported the demo codebase to a notebook (see above), you can run colab_to_codebase.py to export the demo repo back out again as follows:
python ./scripts/colab_to_codebase.py --notebook_path demo.ipynbThis will detect any files that have changes in the colab notebook vs on disk, and ask for confirmation before overwriting them with these changes. You can then use git to track and manage changes in the standard way.
To enforce a set of standards for styling, infrastructure etc, follow this process:
-
When you launch a new Colab session, write your standards into a markdown cell at the top
- if you are using
scripts/codebase_to_colab.py, this will be done for you if you save your prompt to a markdown file and specify it with--gemini_prompt_path
- if you are using
-
As your first prompt to Gemini, tell it to read the cell. For example, use the prompt:
The Gemini System Prompt cell holds the rules that I want you to follow. Confirm that you understand them, and ask about any ambiguous rules. -
Check Gemini's response, and clarify any ambiguities that it has highlighted.
Your standards are now in the context for that coding session. However, this is not a guarantee that they will always be followed, so continue to check Gemini's outputs.
If you ask Gemini to generate a complicated piece of code, it will usually write a plan beforehand and ask you to confirm each step as it writes the corresponding code. This allows you to redirect it to modify its plan, but sometimes it will stall and not continue on to the next step. If this happens, you can usually get it to move on to the next step by saying something like:
Continue with your plan