Skip to content

Instantly share code, notes, and snippets.

@tschm
Created February 7, 2019 08:58
Show Gist options
  • Save tschm/3614fcdea306e3a1ba4c15754f5d32f2 to your computer and use it in GitHub Desktop.
Save tschm/3614fcdea306e3a1ba4c15754f5d32f2 to your computer and use it in GitHub Desktop.
Send production scripts to Jupyter
#!/usr/bin/env python3
import os
import nbformat as nbf
from config.config import logger
from pyutil.parent import Production
from pyutil.sql.interfaces.symbols.strategy import Strategy
from pyutil.sql.session import session
def cell_dict(cells):
return {a.metadata.name : a for a in cells if hasattr(a.metadata, 'name')}
if __name__ == '__main__':
with Production(log=logger) as p:
with session(connection_str=os.environ["reader"]) as session:
# loop over all strategies
for strategy in session.query(Strategy):
p.logger.debug("{s}".format(s=strategy))
# load the notebook template
json_doc = nbf.read("/notebooks/StrategyTemplate.ipynb", as_version=4)
# modify the cell named source
cell_dict(json_doc["cells"])["source"].source = strategy.source
# Look over all code cells
for cell in json_doc["cells"]:
if cell["cell_type"] == "code":
cell["outputs"] = []
cell["execution_count"] = None
# create a new notebook...
nbf.write(nbf.from_dict(json_doc), "/notebooks/strategies/{name}.ipynb".format(name=strategy.name), version=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment