jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.59k stars 4.86k forks source link

When calling %%writefile cell magic disable syntax highlighting #1447

Open SimonBiggs opened 8 years ago

SimonBiggs commented 8 years ago

When calling the %%writefile cell magic python syntax highlighting continues. This can be quite confusing for people who are being shown the notebook and reading the code for the first time.

An example of what I mean can be seen here: http://mybinder.org/repo/Simonbiggs/electroninserts_bundle/electroninserts_notebook.ipynb#Recording-and-loading-the-data

Carreau commented 8 years ago

Good point, we should do that for most %%whatever magics.

blink1073 commented 8 years ago

In this case you could use Codemirror.findModeByFileName() to find the right mode.

Carreau commented 8 years ago

In this case you could use Codemirror.findModeByFileName() to find the right mode.

Oh, is this function as recent addition to CM ?

blink1073 commented 8 years ago

I'm not sure when it was added, but we're using it in JupyterLab: https://github.com/jupyter/jupyter-js-ui/blob/master/src/codemirror/index.ts#L24

SimonBiggs commented 8 years ago

As a question, how difficult would it be to make it so that should ever the file type of %%writefile be a CSV, then the text is justified to align to commas?

This would visually present CSV files very well, and double as an intuitive way to input data.

takluyver commented 8 years ago

I'm not sure exactly what options codemirror provides, but I'd guess that would be pretty hard.

Have a look at handsontablejs for a (prototype) way to edit data frames in the notebook - it works with pandas data frames, but they can easily load from and save to CSV.

SimonBiggs commented 8 years ago

Yeah, I thought it would be non trivial. Do you think it would be worth me attempting a plugin that simply adds spaces before commas to pad out the csv for alignment? Or would that also be non trivial? I haven't played around with plugins before.

I'll give handsontablesjs a try. Thanks for the tip.

SimonBiggs commented 8 years ago

Unfortunately the handsontablesjs method won't work for my purpose. I have been specifically requested to have a permanent record of everything used to make the model within the notebook. Hence writing the CSV file directly into the notebook.

takluyver commented 8 years ago

We have an idea we're planning to develop that we call 'editor plugins' - i.e. alternative interfaces to write/edit the contents of a cell. A data entry table would make perfect sense in that system, so you might want to keep an eye out for that functionality. It should, I hope, get implemented in the next few weeks.

SimonBiggs commented 8 years ago

Ahh brilliant. Thank you. Will do. One great feature currently available is that I can drag and drop a csv file from the file browser directly into the cell and it fills it out as expected.

Would a custom data entry table plugin stop something like that from working?

takluyver commented 8 years ago

I'm surprised that works at present, actually :confused: . A custom editor plugin should be able to handle drop events, though, so it should work with that.

SimonBiggs commented 8 years ago

Brilliant. Would the custom editor plugin be able to activate only if certain text is within the cell?

And would this maybe work like "tabs" above a cell? Essentially if the plugin is active, and then "%%writefile (*).csv" gets written at the top of a cell, then a data entry tab would appear.

Might the set up be something like that?

SimonBiggs commented 8 years ago

I just realised you would probably use the metadata dropdown box...

takluyver commented 8 years ago

The UI hasn't been worked out, but that's roughly the idea - there'll be some way to switch between the table view that the plugin provides and the raw code that it translates the table into.

SimonBiggs commented 8 years ago

Wonderful. Do you know anyone else who is looking to make a data table type editor with the new plugin method? See if I can work together with them.

takluyver commented 8 years ago

Not yet, but I imagine that once support for editor plugins is implemented, there will be other people who'd want to work on a data table editor with you.

JamiesHQ commented 7 years ago

@takluyver @blink1073 @gnestor : is this functionality available now? thanks!

gnestor commented 7 years ago

@takluyver What's the status of this? Is this something that we should revisit at the team meeting this week?

takluyver commented 7 years ago

The original issue of disabling syntax highlighting for %%writefile or the larger question about editor plugins that we discussed in this thread? I think the former should be fairly easy to fix (just add a pattern in the JS that recognises cell magics).

gnestor commented 7 years ago

Ya, it sounds like editor plugins require a much larger discussion. Ok, I will take a crack at disabling syntax highlighting for %%writefile and potentially other magics...

jeremyf21 commented 6 years ago

This might be worthy of opening another issue, but i've noticed that when using %%writefile filename , the filename argument is taken as a literal i.e. if I have variable filename = '/path/to/some/file.txt/' defined in my notebook, running %%writefile filename will write the content of the cell to my current path in a file called filename instead of /path/to/some/file.txt.

takluyver commented 6 years ago

That's expected. You might be able to use a Python variable like %%writefile {filename} or %%writefile $filename.

connectdotz commented 5 years ago

I bump into the same issue, using a variable doesn't really help...

for example setting variable f f="ab c", followed by %%writefile {f}, which yielded UsageError: unrecognized arguments: c; clearly didn't recognize "ab c" as a whole string.

Is there any reason why the filename has to be litteral rather than using standard string syntax?

Charan7221 commented 1 year ago

UsageError: Cell magic %%Writefile not found. it was my error in jupyter notebook