In a julia notebook opened with vscode the dataframe output format is latex but the result is not correctly rendered:
> using DataFrames
> df = DataFrame(a=1:3)
\begin{tabular}{r|c}
& a\\
\hline
& Int64\\
\hline
1 & 1 \\
2 & 2 \\
3 & 3 \\
\end{tabular}
Is there a way to configure jupyter extension to get a well formatted output for julia dataframes?
I'm actually using vscode 1.53.2 and julia 1.6.0-rc1.
I update the show method like this:
using DataFrames
Base.show(io::IO, ::MIME"text/latex", df::AbstractDataFrame) = show(df, backend=:html, show_row_number=true, highlighters=:tf_html_default )
Then we can see the well formatted output from this line:
df = DataFrame(a=1:3)
Try VSCodeServer.vscodedisplay(df), for an example:
using DataFrames
df = DataFrame(x=1:4,y=rand(4),z='a':'d')
VSCodeServer.vscodedisplay(df)
Here is how it works:
Unfortunately, Przemyslaw's answer doesn't actually display the table inline in the Jupyter notebook. It seems that the VSCode implementation requests LaTeX output which it asks MathJax to display. This seems to be broken for DataFrames.
An alternative which does display the table inline is the PrettyTables package.
> using PrettyTables
> pretty_table(df, backend=:html)
Related
I am editing a .ipynb file in Visual Studio Code, with the file open in VS code's Jupyter Notebook editor.
When I edit this notebook in the Jupyter Notebook App (i.e. if I were not using VS code, instead using the interface described here), I could add tags to cells by clicking View > Cell Toolbar > Tags, and then entering tags into the UI that comes up.
Is there an equivalent way to do this in VS Code?
I am aware I can reopen the file in a text editor view and edit the JSON directly. But I am looking for something a bit more user friendly than this.
Try installing this jupyter on vs code.
On my old windows 7 I used to use this. It has the same interface as when you open Jupiter in your browser but you don't need to load a bunch of stuff to open jupyter. You can open it in one single click like changing text file while writing code inside vc code.
Also you need to install one library to use it but unfortunately I can't remember the library as I don't use python or Jupiter anymore.
In the documentation is a python code cell which enables to change all the cell tags.
import nbformat as nbf
from glob import glob
# Collect a list of all notebooks in the content folder
notebooks = glob("**/*.ipynb", recursive=True)
notebooks_windows = []
for i in notebooks:
j = i.replace("\\", "/")
notebooks_windows.append(j)
notebooks_windows
# Text to look for in adding tags
text_search_dict = {
"# HIDDEN": "remove-cell", # Remove the whole cell
"# NO CODE": "remove-input", # Remove only the input
"# HIDE CODE": "hide-input" # Hide the input w/ a button to show
}
# Search through each notebook and look for the text, add a tag if necessary
for ipath in notebooks_windows:
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
for cell in ntbk.cells:
cell_tags = cell.get('metadata', {}).get('tags', [])
for key, val in text_search_dict.items():
if key in cell['source']:
if val not in cell_tags:
cell_tags.append(val)
if len(cell_tags) > 0:
cell['metadata']['tags'] = cell_tags
Microsoft have finally released an extra extension to support this.
ms-toolsai.vscode-jupyter-cell-tags
See this long running github.com/microsoft/vscode-jupyter issue #1182 and comment recently closing the issue.
Be ware however, that while you can set tags, e.g. raises-exception, which is meant to make the runtime expect and ignore the exception and keep on running more cells, but the vscode-jupyter extension that runs the cells does not always honor tag features the same way that the standard Jupyter notebook runtime does, and so, disappointingly, it's still difficult to demo common mistakes or what not to do (exceptions).
Just copy everything in .py file with the unremovable tags, paste that into a plaintext file. The tags are now just editable text. Remove them, Save As or copy/paste back to the .py file.
In org-mode, try to push LaTex code to generate matrix like this
The code is like this:
#+BEGIN_SRC latex
\[ G_{x} = \left[ \begin{array}{ccc}
-1 & 0 & 1 \\
-2 & 0 & 2 \\
-1 & 0 & 1 \end{array} \right] {\hspace{2em}} G_{y} = \left[ \begin{array}{ccc}
-1 & -2 & -1 \\
0 & 0 & 0 \\
1 & 2 & 1 \end{array} \right] \]
#+END_SRC
But when I try to export ORG file to ODT document, the matrix are not presented. Do I need any options or configuration for my org-mode?
Thanks
Untested, but there are a couple of answers to this in the Org-mode manual, specifically they involve wrapping the snippet in \begin{equation}and \end{equation} rather than #+begin_src (which I think will result in embedding the source in the ODT), and setting an option (such as #+OPTIONS: tex:imagemagick) which will ask Org-mode to embed LaTeX as images.
http://orgmode.org/manual/Working-with-LaTeX-math-snippets.html#Working-with-LaTeX-math-snippets
Note: this may be specific to your version of Org-mode -- the exporter framework was significantly overhauled for 8.0 and I believe this documentation applies to Org-mode 8.0.
I'm aware of the %load function (formerly %loadpy) which loads the contents of a file (or URL, ...) into a new input cell (which can be executed afterwards).
I'm also aware of %less, %more and %pycat, which show the contents of a file in a pager (which means in the notebook it's shown in the split-window at the bottom of the screen).
Is there a (magic) command to load a file and show its content (with syntax highlighting) in an output cell?
I.e. something like the following but with syntax highlighting of the result:
with open('my_file.py', 'r') as f:
print(f.read())
I want the file content to be stored with the .ipynb file but I don't want it to be executed when I do Cell -> Run All.
Is there a command similar to %psource which shows the source code in an output cell instead of a pager?
Example code based on answer by #Matt:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import IPython
with open('my_file.py') as f:
code = f.read()
formatter = HtmlFormatter()
IPython.display.HTML('<style type="text/css">{}</style>{}'.format(
formatter.get_style_defs('.highlight'),
highlight(code, PythonLexer(), formatter)))
No there is not way to do that with current magics, but it is pretty easy using pygments and returning IPython.display.HTML(...).
10 years later, and there's now a much simpler solution:
https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.Code
from IPython.display import Code
Code(filename='my_file.py', language='python')
I'm producing a document using org-mode which has quite a few tables in it, constructed using the built in table functionality. I've added captions to the tables, but when I export them to LaTeX, rather than the caption appearing below the table, it appears above it. In the manual section on tables and the latex export documentation, there is no mention of any method of changing this, other than fiddling with the LaTeX code manually. As an illustration, the following code snippets show what is generated by the export on an example table with a caption.
#+CAPTION: Results using two methods with different parameter settings.
#+LABEL: tbl:rescomp
| Parameter | Result 1 | Result 2 |
|-----------+----------+----------|
| 0.5 | 0.1 | 0.8 |
| 1 | 0.8 | 0.1 |
Exported:
\begin{table}[htb]
\caption{Results using two methods with different parameter settings.}
\label{tbl:rescomp}
\begin{center}
\begin{tabular}{rrr}
Parameter & Result 1 & Result 2 \\
\hline
0.5 & 0.1 & 0.8 \\
1 & 0.8 & 0.1 \\
\end{tabular}
\end{center}
\end{table}
The problem could be fixed very simply. The caption appears above the table in the document because it is above the table in the code. Moving the caption definition below the tabular section fixes the issue:
\begin{table}[htb]
\begin{center}
\begin{tabular}{rrr}
Parameter & Result 1 & Result 2 \\
\hline
0.5 & 0.1 & 0.8 \\
1 & 0.8 & 0.1 \\
\end{tabular}
\end{center}
\caption{Results using two methods with different parameter settings.}
\label{tbl:rescomp}
\end{table}
Placing the caption definition below the table in the org file is not possible, as it defines the caption for the next table, as described in the manual. Is there any way that I can get org-mode to export the caption below the table produced?
Just updating the answer for Org-mode version 8.3.2 because setting org-export-latex-table-caption-above to nil does nothing now.
I added the next line to my .emacs or init.el file:
(setq org-latex-caption-above nil)
Just as a side note, that variable contains the value (table) by default, which is the one that we are overriding to nil.
In the link posted by N.N., a patch to implement functionality to allow captions to be placed above or below the float was applied. Looking at the org-mode code on github, the default behaviour of Emacs 24.1 is to place captions above the table. To place captions below tables instead, set the org-export-latex-table-caption-above variable to nil:
M-x customize-variable RET org-export-latex-table-caption-above RET nil
or
M-x set-variable RET org-export-latex-table-caption-above RET nil
Is there a way to get a Markdown cell in iPython Notebooks to highlight syntax in code blocks?
For instance, in GitHub, one can get the desired effect via the following.
```python
>>>print('hello')
```
The GitHub Flavored Markdown-style of denoting code using the triple-backtick is now supported in IPython master branch on GitHub, and so will be included in the 1.0 release.
As Jakob noted, even prior to this, you could use regular markdown for code, in which you just need to indent your code by four spaces, and this continues to be a valid way of displaying code in your IPython notebook.
using IPython 0.13.1 syntax highlighting is as easy as (in a markdown cell):
some text
def foo():
print 'bar'
return 0
some text
Just, use a blank line before and indent the code (see example notebooks shipped with Ipython). This works for Python and some other languages.
In IPython 7.2.0 notebooks you can use:
Text `code` text
in a markdown cell to print highlighted code inline.