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

firstDraft #1

Merged
merged 8 commits into from
Jul 8, 2017
Merged

firstDraft #1

merged 8 commits into from
Jul 8, 2017

Conversation

ashutoshbondre
Copy link
Contributor

Added 3 files:

  1. spec.json (The json data)
  2. script.py (The python script which generated the notebook)
  3. firstDraft.ipynb (The actual notebook)

Copy link
Contributor

@ellisonbg ellisonbg left a comment

Choose a reason for hiding this comment

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

Talked to Ashutosh today in person. Summary of the discussion:

  1. Add %%html cells with the converted HTML from the spec file so we can compare our generated HTML.
  2. Split the notebook into a series of smaller ones, each with maybe 100 cells (the full one is quite large).
  3. Add a section header for each example such as "Example 1" as a Markdown cell.

@rgbkrk
Copy link
Member

rgbkrk commented Jun 14, 2017

Oooh, I'm liking this spec file and script. Great approach for testing our own compliance.

@ashutoshbondre
Copy link
Contributor Author

Hey quick question, how can I run the code cell using python command? Like I know in the notebook you can do shift+enter, but if I don't want to manually do that, and just run the code cells using my script & get the output of the code cells in the notebook, what is the command / syntax for that?
If at all that's possible.
Thanks.

@mpacer
Copy link
Member

mpacer commented Jun 14, 2017

@ashutoshbondre Where are you running it from? If you can use nbconvert the Execute preprocessor probably does what you want: http://nbconvert.readthedocs.io/en/latest/execute_api.html

And if you want to do on individual cells directly, I'd recommend looking at the internals of the Execute preprocessor, specifically the preprocess_cell or the run_cell commands. I wouldn't use that code verbatim (it makes more sense in the nbconvert context) but it should get you a good chunk of the way there.

@mpacer
Copy link
Member

mpacer commented Jun 14, 2017

Btw, just looked and was there a reason you used a python2 kernel? If not please switch to using python3? I anticipate that we'll eventually run into unicode bugs in this spec code otherwise.

@ashutoshbondre
Copy link
Contributor Author

ashutoshbondre commented Jun 14, 2017 via email

@mpacer
Copy link
Member

mpacer commented Jun 14, 2017

Looking at what you have, what I'd do is something like:

# ⋮ your imports
from nbconvert import ExecutePreprocessor

kernel_name = 'python3' # set it programmatically so that you only need to change it in one place
display_name = 'Python 3'
output_path = '/Users/ashu/github/ans.ipynb' # probably want to use a relative path? 
timeout_amount = 60 # none of these should take too long
metadata = {'kernelspec': {
    'name': your_kernel_name
    'display_name': display_name
    }} # this might need to be populated with more valid metadata, but I'm not sure which metadata you'll actually need.
format_version = 4.3
# ⋮ this is where your spec reading and cell writing would happen
executor = ExecutePreprocessor(timeout=60, kernel_name=your_kernel_name)
executor.preprocess(nb, metadata)
nbformat.write(nb,"/Users/ashu/github/ans.ipynb", format_version)

Hope that helps!

Copy link
Member

@mpacer mpacer left a comment

Choose a reason for hiding this comment

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

Tried to find all of the little things that will make this more portable and configurable in the long run. Some of my example code would address these issues (at least by making them top level variables instead of hard-coded values).

script.py Outdated
for i in range(len(data)):
cells +=[new_markdown_cell(data[i]["markdown"])]
nb=new_notebook(cells=cells,metadata={'kernelspec': {
'name': 'python2',
Copy link
Member

Choose a reason for hiding this comment

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

probably shouldn't hard code this.

script.py Outdated
cells +=[new_markdown_cell(data[i]["markdown"])]
nb=new_notebook(cells=cells,metadata={'kernelspec': {
'name': 'python2',
'display_name': 'Python 2'
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't hard code this as you want it to be easily changeable.

script.py Outdated
}})


nbformat.write(nb,"/Users/ashu/github/ans.ipynb",4.0)
Copy link
Member

Choose a reason for hiding this comment

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

probably don't want to hard code the path or the version number as it's reasonable to expect that we want to do this on any number of potential versions.

Copy link
Member

Choose a reason for hiding this comment

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

Probably want the path to be relative, because this makes it so you're the only one who can easily run this without directly editing the file.

script.py Outdated
import nbformat
from pprint import pprint
from nbformat.v4 import new_code_cell, new_markdown_cell, new_notebook
with open('spec.json') as data_file:
Copy link
Member

Choose a reason for hiding this comment

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

You should either use absolute paths or relative paths consistently (here you're implicitly using a relative path, later you use an absolute path).

@ashutoshbondre
Copy link
Contributor Author

@mpacer Thanks for the review, did most of the changes. Also ExecutePreprocessor did what I wanted.

One reason I used python 2 kernel was because that was the default one I had, I am having problems with executing the code in python3.
I set up a different environment in python 3 using "conda create" command, but when I execute the code there,it doesn't even recognize nbformat. Says "no module named nbformat" .

Currently I am working on Python 2.7.13. Can you tell me, what can I do here, to switch to the right environment. I am pretty sure, I am just missing some minute detail.
Thanks

@ellisonbg
Copy link
Contributor

ellisonbg commented Jun 15, 2017 via email

@mpacer
Copy link
Member

mpacer commented Jun 15, 2017 via email

The script creates as many notebook as possible, each with 100 entries.
@ashutoshbondre
Copy link
Contributor Author

ashutoshbondre commented Jun 15, 2017

Ready for review:
Hi, I did all the necessary changes, added relative paths as well.
The script now takes json data of any size & creates a number of notebooks, each with 100 examples. So in our case we have 643 entires, so it creates 7 notebooks, six with size 100 & seventh with 43 entries. Notebooks are named nb1, nb2, nb3, etc. on creation.
Each notebook has 3 cells for each entry, which are:

  1. Markdown cell with example number, eg. "Example 1"
  2. Actual markdown code in a markdown cell.
  3. Code cell, with html code for each markdown, each code cell already executed in place.

Thanks,
Ashutosh

@ashutoshbondre
Copy link
Contributor Author

All made with python3 kernel.

@rgbkrk
Copy link
Member

rgbkrk commented Jul 8, 2017

I'd like to go ahead and assume that the requested changes were made and this is ready to go.

@ellisonbg ellisonbg merged commit ede1605 into jupyter:master Jul 8, 2017
Copy link

@Mhtpyurt Mhtpyurt left a comment

Choose a reason for hiding this comment

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

Sevdiğim

firstDraft.ipynb Outdated

Choose a reason for hiding this comment

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

H

firstDraft.ipynb Outdated

Choose a reason for hiding this comment

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

Aşk

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