Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally display blocks of code #11

Open
dkirkby opened this issue Jul 12, 2016 · 2 comments
Open

Optionally display blocks of code #11

dkirkby opened this issue Jul 12, 2016 · 2 comments

Comments

@dkirkby
Copy link
Collaborator

dkirkby commented Jul 12, 2016

@drphilmarshall suggested using %load file.py as a way to move large blocks of dense code into external modules and then optionally display them. Try this out and look for opportunities to improve the flow of a live tutorial with this technique. This method can also be usesd to reveal the solution to an exercise.

@fjaviersanchez
Copy link
Owner

I used the function show_notebook defined here: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/rstversions/Importing%20Notebooks.html

The good thing is that it allows you to use Jupyter magic commands, the bad things are that I have to define this function somewhere, and that it just appears as text, not as a executable (there also exists the possibility of importing the whole notebook using the link above.

@dkirkby
Copy link
Collaborator Author

dkirkby commented Jul 13, 2016

The way I would use %load is to create a file helpers.py that defines functions used to make plots, etc, where the code details are distracting to someone seeing the tutorial for the first time:

def plot1():
   ...

def example2():
   ...

I don't think this file should have any if __name__ == "__main__" boilerplate and any imports it needs should be imported in the notebook instead.

Next, import helpers at the top of the notebook and call these functions throughout. Somewhere near the top, explain that the code of any helper can be displayed (and modified and re-run) in the notebook using, e.g.

%load -s example2 helpers

When you run this cell, its contents are replaced with:

# %load -s example2 helpers
def example2():
   ...

The same technique could be used for an external file solutions.py to reveal solutions to exercises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants