most folks are familiar with notebooks in their interactive forms.
in IPython, we are working in python and __name__ == "__main__".
for python programs, this condition is the last stop for our program,
but in notebooks it is just the beginning.
i like to thing we development programs and applications in reverse in notebooks.
# three states of python in a notebook
most folks are familiar with notebooks in their interactive forms.
in `IPython`, we are working in python and `__name__ == "__main__"`.
for python programs, this condition is the last stop for our program,
but in notebooks it is just the beginning.
i like to thing we development programs and applications in reverse in notebooks.
with experience, it is possible to author notebooks that operate as programs/scripts. this document demonstrates some advanced usage patterns to apply to get the most out of future notebooks.
most folks are familiar with notebooks in their interactive forms.
in
IPython
, we are working in python and
__name__ == "__main__"
.
for python programs, this condition is the last stop for our program,
but in notebooks it is just the beginning.
i like to thing we development programs and applications in reverse in notebooks.
with experience, it is possible to author notebooks that operate as programs/scripts. this document demonstrates some advanced usage patterns to apply to get the most out of future notebooks.
thethreepythoncompilationstatesofnotebooks:*an__interactive__statewhenweareauthoringnotebooks.*a__script__statewhenanotebookisusedasproceduralcode.*a__module__whenitisimportedlikeanypythonresourceweneedtoconferwiththe`__name__`and`__file__`propertiesofamodule's runtime to know which state we are in.|state|`__name__`|`__file__`||--------|------------|------------||interactive|`__name__=="__main__"`|`"__file__"notinlocals()`||module|`__name__!="__main__"`|`"__file__"inlocals()`||script|`__name__=="__main__"`|`"__file__"inlocals()`|
## python state logicthethreeconditionsareexpressedinpythoncodebelowMAIN,FILE=__name__=="__main__","__file__"inlocals()INTERACTIVE=MAINandnotFILESCRIPT=MAINandFILEMODULE=FILEandnotMAINthesestatesletuswritemultipurposedocumentsthatcanbeusedinmultiplecontextsandsharingthebenefitsofallofthem.
#### the `SCRIPT` conditinifSCRIPT:weimportthenotebookandrunthecommandlineprogram.belowisa`typer`applicationthatisonlyexecutedwhenthenotebookisusedasascriptimporttypertyper.run(a_bad_ass_function)#### the `INTERACTIVE` conditionifINTERACTIVE:datafordemonstrationiscomputedanddisplayed!midgy2023-01-09-notebooks-states.ipynb--help!midgy2023-01-09-notebooks-states.ipynb--who"y'alls"
## the benefits of knowing the stateknowingthestateorcontextofanotebook's execution allows us to write logic that makes notebooks a swiss army knife.someadvancedpatternsareinclude[testingnotebooks]or[writingcommandlinescripts].[testingnotebooks]:https://nbviewer.org/gist/tonyfast/b617e4f87337a50e1361ab043b53c2c7[writingcommandlinescripts]:https://tonyfast.github.io/tonyfast/xxii/2022-12-19-integrating-typer.html