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 subfunction for prep course #6

Closed
cimendes opened this issue Oct 28, 2021 · 5 comments · Fixed by #9
Closed

Add subfunction for prep course #6

cimendes opened this issue Oct 28, 2021 · 5 comments · Fixed by #9
Assignees

Comments

@cimendes
Copy link
Member

TBD

Containing only the necessary functions to grade the notebook and return a grade.

@buedaswag
Copy link
Contributor

buedaswag commented Nov 1, 2021

i'm trying to grade a notebook locally with this code, but im getting an error.
am i missing something?
@hcastilho do you have any suggestions?

from nbgrader import utils

def grade(nb):
    total_score = 0
    max_total_score = 0
    for cell in nb.cells:
        if utils.is_grade(cell):
            score, max_score = utils.determine_grade(cell)
            total_score += score
            max_total_score += max_score

    return total_score, max_total_score

from traitlets.config import Config
import nbconvert

#####

def execute(notebook, timeout=None, allow_errors=True):
    c = Config()
    c.NotebookExporter.preprocessors = [
        "nbconvert.preprocessors.ClearOutputPreprocessor",
        "nbconvert.preprocessors.ExecutePreprocessor",
    ]
    c.ExecutePreprocessor.allow_errors = allow_errors
    if timeout:
        c.ExecutePreprocessor.timeout = timeout

    exporter = nbconvert.NotebookExporter(config=c)
    notebook, _ = exporter.from_notebook_node(notebook)

    return nbformat.reads(notebook, as_version=nbformat.NO_CONVERT)

####

import nbformat

notebook_path = 'Exercise notebook.ipynb'
notebook = nbformat.read(notebook_path, as_version=nbformat.NO_CONVERT)
notebook = execute(notebook, timeout=60) # utils.execute(notebook, timeout)
total_score, max_score = grade(notebook)
Traceback (most recent call last):
  File "grade.py", line 40, in <module>
    notebook = execute(notebook, timeout=60) # utils.execute(notebook, timeout)
  File "grade.py", line 30, in execute
    notebook, _ = exporter.from_notebook_node(notebook)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/exporters/notebook.py", line 32, in from_notebook_node
    nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/exporters/exporter.py", line 139, in from_notebook_node
    nb_copy, resources = self._preprocess(nb_copy, resources)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/exporters/exporter.py", line 316, in _preprocess
    nbc, resc = preprocessor(nbc, resc)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/base.py", line 47, in __call__
    return self.preprocess(nb, resources)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/execute.py", line 405, in preprocess
    nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/base.py", line 69, in preprocess
    nb.cells[index], resources = self.preprocess_cell(cell, resources, index)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/execute.py", line 438, in preprocess_cell
    reply, outputs = self.run_cell(cell, cell_index, store_history)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/execute.py", line 578, in run_cell
    exec_reply = self._poll_for_reply(parent_msg_id, cell, timeout)
  File "/home/mig/.virtualenvs/grading-notebooks-locally/lib/python3.7/site-packages/nbconvert/preprocessors/execute.py", line 479, in _poll_for_reply
    if msg['parent_header'].get('msg_id') == msg_id:
TypeError: 'coroutine' object is not subscriptable
sys:1: RuntimeWarning: coroutine 'ZMQSocketChannel.get_msg' was never awaited

@hcastilho
Copy link
Member

This is due to problems with lib versions, jupyter-client I think.

@buedaswag
Copy link
Contributor

managed to fix it with jupyter_client==6.1.12, thanks @hcastilho !

@buedaswag
Copy link
Contributor

found the answer here jupyter/jupyter_client#637

@cimendes
Copy link
Member Author

cimendes commented Nov 2, 2021

🕵🏻

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 a pull request may close this issue.

3 participants