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

Add option for hiding input #135

Merged
merged 1 commit into from
Jul 25, 2018
Merged

Add option for hiding input #135

merged 1 commit into from
Jul 25, 2018

Conversation

cloud-princess
Copy link
Contributor

Have made an attempt. Please tell me what to change/add. Thanks!

@cloud-princess
Copy link
Contributor Author

#130

@@ -147,7 +147,8 @@ def execute_notebook(notebook,
kernel_name=None,
progress_bar=True,
log_output=False,
start_timeout=60):
start_timeout=60,
report-mode=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an underscore, otherwise this is a subtraction.


Returns:
nb (NotebookNode): executed notebook object
"""
print("Input Notebook: %s" % get_pretty_path(notebook), file=sys.stderr)
print("Output Notebook: %s" % get_pretty_path(output), file=sys.stderr)
nb = load_notebook_node(notebook)

# Hide input if report-mode is set to True.
if report-mode:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

report_mode here too.

@cloud-princess
Copy link
Contributor Author

ah right! Will fix. Thanks!

@cloud-princess
Copy link
Contributor Author

hopefully it is better now

@codecov
Copy link

codecov bot commented May 10, 2018

Codecov Report

Merging #135 into master will increase coverage by 0.08%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #135      +/-   ##
==========================================
+ Coverage   73.58%   73.66%   +0.08%     
==========================================
  Files           8        8              
  Lines         935      938       +3     
==========================================
+ Hits          688      691       +3     
  Misses        247      247

@cloud-princess
Copy link
Contributor Author

hm... what else is wrong?

@rgbkrk
Copy link
Member

rgbkrk commented May 10, 2018

It's only reporting an error because coverage has gone down (no tests on this new command). I'll have to defer to one of the primary pythonistas for what they'd want next as well as style on the command. Thanks for submitting this!

@cloud-princess
Copy link
Contributor Author

Alright! Looking forward to seeing what is done next!

@MSeal
Copy link
Member

MSeal commented May 11, 2018

Two things to add now I think:

If we're adding an option to the execute command it should also be on CLI. The cli.py file should be straight forward to manipulate and add a new flag (you can copy one of the adjacent flags properties).

The second item is tests. We will want a test in the test_execute.py and test_cli.py to test that we can set the option and that it will change the metadata of an output notebook when it executes that notebook.


# Hide input if report-mode is set to True.
if report_mode:
nb.metadata.inputHidden = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpacer I saw some of the back-and-forth on jupyter threads about hidden inputs, is this the metadata field we want papermill to populate for other tools going forward?

@cloud-princess
Copy link
Contributor Author

will look into the error tmr!

papermill/cli.py Outdated
@@ -36,6 +36,8 @@
help="Flag for writing notebook output to stderr.")
@click.option('--start_timeout', type=int, default=60,
help="Time in seconds to wait for kernel to start.")
@click.option('--report-mode/--not-report-mode', default=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want this to be default=False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!

@@ -147,7 +147,8 @@ def execute_notebook(notebook,
kernel_name=None,
progress_bar=True,
log_output=False,
start_timeout=60):
start_timeout=60,
report_mode=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do report_mode=False here too as it's a flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks!

@cloud-princess
Copy link
Contributor Author

cloud-princess commented May 13, 2018

Have attempted writing tests in both test_execute.py and test_cli.py.

@cloud-princess
Copy link
Contributor Author

cloud-princess commented May 13, 2018

I have read the errors(EDIT: this was written before squashing), and the following is my understanding:

  1. test_cli.py
    it says that the arguments in the call aren't consistent with the expected arguments but I see no difference - what am I missing?

  2. test_execute.py
    the test notebook 'report_mode_test.ipynb' is not found in the path because I've only added the notebooks in my branch instead of nteract/papermill/... - presumably that's an error I can't overcome?

The input and output notebooks I've made may or may not make a very good test case...

Thanks!

@MSeal
Copy link
Member

MSeal commented May 21, 2018

Try pasting the delta into a diff tool, should pop up quickly. Also try adding a debug point (or print statements) in the execute call to make sure you're getting expected values. You can isolate to a single test with https://stackoverflow.com/questions/36456920/is-there-a-way-to-specify-which-pytest-tests-to-run-from-a-file to narrow down the issue. I don't see the problem from a peripheral scan of the code.

@MSeal
Copy link
Member

MSeal commented May 21, 2018

For test_execute you do not need to have the notebooks pushed anywhere, just make sure they're in the repo branch and added. Check that self.notebook_path is getting set to something correct, but I don't see any obvious issue there either so it's probably a silly typo error someplace.

@cloud-princess
Copy link
Contributor Author

Yes, it was a typo. Thanks!!

@MSeal
Copy link
Member

MSeal commented May 22, 2018

Awesome, two things left. First, let's squash these commits down. Second is we're discussing this week with a few nteract team members what the best metadata / library contract should be to respect this report mode. Specifically what should the metadata field look like and how do we want nteract exporters to respect metadata contracts moving forward. Today I don't believe any tools will respect nb.metadata.inputHidden = True, and I'm assuming you want nteract html exporters to respect this field?

@rgbkrk
Copy link
Member

rgbkrk commented May 22, 2018

The original jupyter discussion about hidden input was here: jupyter/notebook#534

nteract frontends all support both inputHidden and hide_input https://github.com/nteract/nteract/pull/1697/files

I totally forgot that at the last dev meetings we ended up on a nested path:

https://github.com/jupyter/nbformat/pull/94/files

metadata.jupyter.source_hidden and metadata.jupyter.outputs_hidden

Let's go with that officially merged stance on nbformat and I'll make the necessary changes to nteract to also set those. 🙃

@cloud-princess
Copy link
Contributor Author

cloud-princess commented May 23, 2018

Yes I do want html exporters to respect the field. Ok! Thanks. I'll squash and go through the links!

@mpacer
Copy link
Member

mpacer commented May 24, 2018

@cldssty As long as we're namespacing the metadata to provide this functionality — I'm cool with that. With such a change I'm ok with this PR.

Note that that spec that @rgbkrk linked to is a spec on a per cell basis, and so there is no one field to set for the whole notebook.

More generally though, think we should consider the long-term plan for additional customisation (like report-mode) for a couple of reasons. First, the proposed solution will be respected by the nteract front-end and if you want a static html page (which is what I thought you wanted) you'd need to use commuter (which I think supports this?).

As of right now, source_hidden is not respected in nbconvert as there was some disagreement on whether that hidden state was actually intended to be the mechanism for specifying this in static exports as opposed to only in the interactive view of the notebook. Right now there is no --no-input flag in nbconvert, but I plan to make a PR for that later today (as I mentioned in the issue associated with this PR… there is already input removal enabled via the TemplateExporter.exclude_input). See spatialaudio/nbsphinx#65 (comment) for more context.

Part of the reason why there was disagreement around the term hide_input is whether that is intended to remove input altogether or make it so that its visibility can be toggled back and forth (see
https://mpacer.github.io/hiding_tags_nbconvert/hide_cells_based_on_tags.html for an example, for the code see https://github.com/mpacer/hiding_tags_nbconvert ).

@MSeal
Copy link
Member

MSeal commented May 30, 2018

@cldssty Let us know if you get stuck in the final PR updates

mpacer added a commit to mpacer/nbconvert that referenced this pull request Jun 5, 2018
@MSeal
Copy link
Member

MSeal commented Jul 18, 2018

@cldssty Do you think you'll have some time to complete this PR or should we make final touches to get it mergable?

@cloud-princess
Copy link
Contributor Author

I'm really sorry for the delay! I'll be looking at this this weekend and will ask about any problems I'm encountering. Again really sorry about disappearing and thank you so much for your patience. Apologies for any inconvenience caused!!

@MSeal
Copy link
Member

MSeal commented Jul 19, 2018

No problem, and no need to apologize. We wanted to get this nice capability added before the nteract sprints begin in a week so we'll give special attention to this PR and help with getting it finished.

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 23, 2018

I've skimmed through the links and these are my thoughts:

I don't know why I assumed metadata.inputHidden it was a field for notebooks 2 months ago - but yes, it is a field for code cells.

If I stick to using a field for the whole notebook(instead of for each cell), then we'd need to

1. add the field in the nbformat schema?

2. then we should probably make it so that if this field is set to True, then for each new code cell created, the metadata.jupyter.source_hidden field is set to True?

Alternatively, I could just make this flag toggle input visibility on a cell-by-cell basis. In that case, I would need to change my code but won't need to add a field to nbformat schema?

EDIT: I'm going to go ahead and look into using metadata.jupyter.source_hidden and change my code so that I'm calling this field with cells, not notebooks. Thanks for all the tips.

In any case, since @mpacer says that source_hidden is not currently respected by nbconvert, we'll need to additionally somehow make this flag set TemplateExporter.exclude_input in nbconvert to True as well so that input is hidden for html exports? I need to look into the PR by @mpacer.

I have yet to find out how to do all of this. If there are any misunderstandings or things that cannot be done, please let me know!

Thanks :D

@cloud-princess
Copy link
Contributor Author

Currently have something like this in execute.py:

# Hide input if report-mode is set to True.
if report_mode: 
    for cell in nb.cells:
        if cell.cell_type == 'code':
            cell.metadata.jupyter.source_hidden = True

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 24, 2018

It's saying that it doesn't have the 'jupyter' key... should the key be added somewhere or...?

@rgbkrk
Copy link
Member

rgbkrk commented Jul 24, 2018

If it doesn't have the jupyter key, go ahead and add it in, then apply source_hidden. We formalized on what the spec would be yet haven't started adopting it. If you set it here, we'll be extra motivated to follow through on the nteract frontend at least. 😄

We can leave it to nbconvert to implement it without bogging this PR down.

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 24, 2018

ok shall I just squash the commits? Going to look into making this work with html exports in a bit more detail.

Oh wait, the field is now metadata.jupyter['source_hidden'] instead of metadata.jupyter.source_hidden so I should probably fix that.

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 24, 2018

I'll keep working on this tmr.

This is what I've done so far:

  1. changed field to metadata.jupyter['source_hidden'] for cells and it passed travis build.
  2. currently trying to turn this into metadata.jupyter.source_hidden but so far my ideas aren't working.

What I've tried so far:

  1. setting metadata.jupyter = lambda: None and then setting attributes source_hidden and outputs_hidden for that. The travis-ci errors didn't seem to show any direct cause of the failed build.

  2. creating a custom object to set metadata.jupyter to. First, I'm not sure whether to put the class definition of this object in iorw.py or not, and secondly, I'm still trying to implement a JSON serialiser for it, but idk if this is the right route to go down.

Please advise! I will look into the exportation part of this and also if it gets too close to the nteract sprint and I still can't find a good solution, I would be happy to hand this over to someone who can wrap this up before the start of the sprint. You could specify a deadline for this if you like. Until then I'm going to keep trying!

Thanks!

if report_mode:
for cell in nb.cells:
if cell.cell_type == 'code':
cell.metadata.jupyter.source_hidden = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cell.metadata['jupyter'] = cell.get('jupyter', {})
cell.metadata.['jupyter']['source_hidden'] = True

self.source_hidden = False
self.outputs_hidden = False

class Dumper(Jupyter):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need these two classes

@@ -167,6 +190,9 @@ def load_notebook_node(notebook_path):

if not hasattr(cell.metadata, 'papermill'):
cell.metadata['papermill'] = dict()

if not hasattr(cell.metadata, 'jupyter'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given the snippet I posted you can skip this check/assignment too

nb = execute_notebook(self.notebook_path, self.nb_test_executed_fname, report_mode = True)
for cell in nb.cells:
if cell.cell_type == 'code':
self.assertEqual(cell.metadata.jupyter.source_hidden, True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.assertEqual(cell.metadata.get('jupyter', {}).get('source_hidden'), True)

@MSeal
Copy link
Member

MSeal commented Jul 25, 2018

Thanks for coming back to wrap up the PR!

I think leaving metadata with primitive objects (dict/str) will uncomplicated the part you're stuck on and posted come comments to help with making that change. Give that change a whirl and I think you'll be good :)

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 25, 2018

Changes applied and squashed. Thanks for the tips!!

@rgbkrk
Copy link
Member

rgbkrk commented Jul 25, 2018

This is looking pretty good, I think you can take the WIP title off if you think it's ready. :)

@cloud-princess
Copy link
Contributor Author

cloud-princess commented Jul 25, 2018

So is source_hidden going to be respected by html exporters if it gets adopted by nteract frontends and nbconvert, i.e. something that is probably going to be out of the scope of this PR?

I was going to do a bit more research later today to see if there is anything else I could do regarding html exports but if there isn't then yes I'm happy to take the WIP title off at this stage!

Thanks!

@MSeal
Copy link
Member

MSeal commented Jul 25, 2018

The intention is yes. We'll need to make a PR to nbconvert to respect the same field as nteract. @mpacer has a PR (jupyter/nbconvert#825) which we can help get completed and merged.

@cloud-princess cloud-princess changed the title [WIP] Add option for hiding input Add option for hiding input Jul 25, 2018
@cloud-princess
Copy link
Contributor Author

Thanks for all the tips and being helpful + patient! I'll be looking out for further development in related PRs!

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

Successfully merging this pull request may close these issues.

None yet

5 participants