Interactive Python for Incremental Programming

Learn a much more efficient way to program in Python. Move a step closer to the legendary concept of Incremental Programming.

Sometimes I just can't avoid work that requires obtaining a large amount of data from a fairly slow API, process that data, and then pushing it somewhere else.

I find it impossible to just write the correct code from scratch in one linear pass. At least, impossible in the traditional approach of: write script covering the entire procedure, run script, check errors, improve script.

If the script takes a long time to initialize, we end up waiting that time out multiple times. What if we could wait that time out only once, and make subsequent test calls immediate?

Interactive Python allows exactly that. Create a frame with the initialization. Run the frame. Proceed to writing new code in a separate frame. Run the new frame as many times as you want - data already waits in the memory.

VSCode already has plugins for this kind of Python usage. Give your source file a .py extension, and separate frames with "#%%".

To make things clear: There's no need to use Jupyter notebook for this - just VSCode is enough.

An alternative would be setting a breakpoint in the program and defining new behaviors from there.

However, it's much easier to take code from an interactive Python notebook and put that into your target program, than to write functions in the debugger console and later copy, paste and edit those.

Especially the editing part seems nearly impossible in the REPL - while very convenient in cells of interactive Python.

To get started, create a source file with name ending with a .py extension, add an initial frame to it by writing "#%%", and write `print("Hello, world!")` in the next line. Open the file in VSCode and click "Run cell".

Write "#%%" to start another cell.

Subscribe to SBCTricks.com

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe