question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert Jupyter notebooks to Hydrogen / VScode / Spyder compatible scripts, and back

See original GitHub issue

Many editors recognize code cells in python scripts as blocks that start with # %%, including

  • Hydrogen
  • VS code
  • Spyder.

I found no specific marker for markdown () or raw cell yet. So let’s be inventive and imagine a few specifications for that format: any cell starts with the above prefix. An optional cell type (among: code, markdown, raw) can be specified. An optional cell name can be stated (it cannot be any of code, markdown, raw). Then, if required, we add the cell metadata, in JSON format, like in the following:

# %% optional_cell_type optional_cell_name {"metadata_key1": "value1", "key2": "value2"}
import pandas as pd
pd.DataFrame({'A':[5]}).plot(kind='bar')

# %% markdown
# This is a markdown cell

# %%
# # This is a commented code cell
# import pandas

Hydrogen and VS code execute python code with Jupyter kernels, so there’s no need to escape Jupyter magics here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mwoutscommented, Oct 7, 2018

@danieltomasz , thanks for your example. I just changed the format parser to make sure your example will open as a ‘percent’ script. The identification of spyder scripts now requires just one cell in the script, that’s a simpler criterion!

1reaction
mwoutscommented, Oct 4, 2018

Well, yes. You should add a least a second cell… this is briefly documented in the next README. Let me explain a bit more…

When no format information is given, jupytext classifies percent scripts as such if they contain at least two explicit cells. What’s happening here is that jupytext opens your script as a standard Python script, using the light format.

Note that if you generate the percent script using Jupytext, a YAML header with an explicit information on the file format will be added, and Jupyter will open it with the right format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Jupyter Notebooks in Visual Studio Code
Export your Jupyter Notebook​​ You can export a Jupyter Notebook as a Python file ( . py ), a PDF, or an HTML...
Read more >
Jupytext documentation
Jupytext is a plugin for Jupyter that can save Jupyter notebooks as ... write notebooks as Hydrogen/VScode/Spyder/PyCharm compatible scripts ...
Read more >
jupytext - PyPI
jupytext converts Jupyter notebooks to their text representation, and back. The command line tool can act on notebooks in many ways. It can ......
Read more >
jupyter kernels
The Jupyter Notebook integrates code and its output into the single document ... assignments. with shell kernels you can turn your scripts into...
Read more >
How to structure your Python scripts for Sphinx-Gallery
This will be rendered in-line with the Python code and its outputs, similar to how Jupyter Notebooks are structured (in fact, Sphinx-Gallery also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found