How to write text in jupyter / ipython notebook? - jupyter-notebook

Here is an example of IPython notebook in which besides the input and output cells we have a plain text. How can I do the same in my IPython notebook? At the moment I have inly In and Out cells.

Change the cell type to Markdown in the menu bar, from Code to Markdown. Currently in Notebook 4.x, the keyboard shortcut for such an action is: Esc (for command mode), then m (for markdown).

As it is written in the documentation you have to change the cell type to a markdown.

Step 1: Click on the '+' to create a new cell, type some text in, and select 'Markdown' in the dropdown
Step 2: It should now look like this:
Step 3: Click anywhere in the cell and click on 'Run'. Now it looks how you expect it to.

Simply Enter Esc and type m it will convert to text cell.

Adding to Matt's answer above (as I don't have comment privileges yet), one mouse-free workflow would be:
Esc then m then Enter so that you gain focus again and can start typing.
Without the last Enter you would still be in Escape mode and would otherwise have to use your mouse to activate text input in the cell.
Another way would be to add a new cell, type out your markdown in "Code" mode and then change to markdown once you're done typing everything you need, thus obviating the need to refocus.
You can then move on to your next cells. :)

Related

How can I insert a line of text above the occurence of a specific text in a long R Script?

I have been looking for a solution to this problem but could not find a practical one.
I have a long R script (more than 1500 lines).
I have the following text which repeats itself several times in the Script:
df00$StartDate
I need to add the following line of text just above it (everywhere where it occurs):
df00$`MealPlan` <- sub('\n.*', '', output3$`Your choices`)
Is there an efficient way to do this instead of scrolling through the Script and manually pasting that new line of code?
CTRL+F search for df00$StartDate, then click "All", then start editing, the edit will be done for all selected rows at the same time.
See screenshot, you will see a multiline cursor at the end of each line:
2015-05-06: RStudio v0.99 Preview: More Editor Enhancements

Copy text from RStudio console as "plain text"

Is there a simple way to copy the selected output in RStudio's console as plain text?
For example, I have this output:
And then when I select it and copy it to another application (e.g. MS Word or WPS Office) I always get this ugly format with it:
I know I can "Keep text only" in MS Word (e.g. pressing Ctrl-T right after pasting) and similar options in other applications, but I wish there were a single hotkey, or if I could just convert all output in RStudio console to plain text by default (because I have no use for its formatting anyway).
IN Addins menu, there is an option to copy value/output to clipboard.
Please see clipr
Or you can capture console output and write output to clipboard.
to_clip_board <- function(x){
message(paste(
as.character(substitute(x)), "written to clipboard."
))
write.table(
paste0(capture.output(x),collapse = "\n"),
"clipboard",
col.names = FALSE,
row.names = FALSE
)
}
> to_clip_board(iris)
> to_clip_board(data.frame)
I just found a solution that works at least on my present OS, Linux Mint (19.3): Just select text and click with middle mouse button to the place (e.g. in WPS Office or LibreOffice) where you want to copy-paste the selected text. It will instantly copy and paste the text without formatting. (If you want to copy first, click with middle mouse button on the selected text, and then click again with middle mouse button to the place where you want it pasted.)
Others have said that the Ctrl + Shift + V should paste without formatting too, but that didn't work for me. This combination is supposed to also work on Windows in many applications. Otherwise for Windows you can download PureText that makes copy-paste without formatting super easy.
(In retrospect I realize this is not necessarily an R or RStudio question, but rather a more general OS clipboard issue. Nonetheless I needed it for R only, so I'll leave the question unchanged: there might be better and more general R-specific solutions in the future, such as making all console output plain text.)

How to copy multiple input cells in Jupyter Notebook

Basically I want to copy (Ctrl+C) only the code portions from multiple cells without also copying the output or the In[1]: and Out[1]:
What is the easiest way to do so?
When you are on a cell in Command mode(blue color mode), simply press Shift + DownArrow or Shift + UpArrow to select multiple cells. Press ctrl + C. And that's it. You have copied your entire selected code at once. It doesn't affect whether you have cell outputs.
Command mode: The Jupyter Notebook has two different keyboard input modes. Edit mode allows you to type code or text into a cell and is indicated by a green cell border. Command mode binds the keyboard to notebook level commands and is indicated by a grey cell border with a blue left margin.
In jupyter you can copy several cells or the content of one cell. If you follow #BenWS comment you can copy several cells, and if you do kernel > restart & clear outputs beforehand you woult not get the [out]. Shortcut is C for copy cell and V shift + V to paste below / above.
However if you intend to copy several cells content, you should merge then before by select them and shift + M and then you can copy paste with ctrl + C.
What worked for me is the following:
update jupyter notebook within a cell using:
pip install -U jupyter notebook
go in command mode by clicking to the left of a cell. If you click inside of a cell, it will be green.
Use shift+down/up to select the cells you want to copy and use ctrl+c
Now the most important one: make sure the jupyter file you want to copy the cells into is ALSO in blue/command mode. If this is not the case, you will copy all the cells into a single cell.
Just do:
File > Export Notebook As > Export Notebook to Asciidoc
and it will be easy to copy paste.
This is what an Asciidoc file looks like:
+*In[ ]:*+
[source, ipython3]
----
import pandas as pd
df = pd.read_csv("data/survey_results_public.csv")
df.tail(10)
df.shape
pd.set_option("display.max_columns", 85)
pd.set_option("display.max_rows", 85)
schema_df = pd.read_csv("data/survey_results_schema.csv")
schema_df.head(10)
----
In the latest version of JupyterLabs:
File > Export Notebook As > Executable Script
Gives you the code as a text file.
Open notebook dir as project in PyCharm, and then open the wanted ipynb file, select and copy all the source code, past into notepad++, replace "\r\n#%%\r\n\r\n" by null with extended search mode.
For jupyterlab after Shift + UpArrow or Shift + select with mouse on multiple cells. Right click on cells for copy(C) and paste(P).

What cause format change when copy and paste in rstudio?

#sample select
sample_frac(mydata,n%)#random select n% sample
##############data review####
Just copy above code into rstudio script, you will find 2 more tab added to the last line.
What cause it?
Edit
As mentioned by #Jay in the comments, the n% in the command is treated as a function and since it is not complete it indents the next line.
To further confirm, try with df %in% in the script or df >%> and hit enter to see the cursor goes to the next line with an indent.
To avoid that just complete the function there.
sample_frac(mydata,n)
OR
sample_frac(mydata, n %% somenumber)
whatever you are trying to do and it should be fine.
Original Answer
It did add 2 tab spaces in the code when pasting in the RStudio script. I tried to paste the same text in my notes, Pycharm editor but it did not add any extra tabs there. So it was sure that this is a RStudio issue.
It turns out it is the indentation settings in RStudio which is responsible for this. To change that:
Go to Tools -> Global Options. Click on the Code option on the left. You'll see this :
Uncheck Auto-indent code after paste
and click on OK.
Now try to paste the same text. Should be resolved.

Inserting a Link to a Webpage in an IPython Notebook

How is this done? I'd like to have the link be in a markdown cell.
For visual learners:
[blue_text](url_here)
In case it is not a markdown cell, that is with what I went:
from IPython.core.display import display, HTML
display(HTML("""text"""))
Just another tip, using magic expression.
%%html
Showing Text
Improved. Thanks to the comment of calocedrus.
Here is the code I use in my python notebook when I want to insert a link to a webpage inside a markdown cell (in a python notebook).
[Clickable_visible_hyperlink](Hidden_landing_URL)
--note Here is the clickable hyperlink, you can change the value
This might help too, if you're looking to display a link programmatically.
from IPython.display import display, Markdown
display(Markdown("[google](https://www.google.com)"))
I also tried
display(HTML("""<a href="https://www.google.com>google</a>"""))
But somehow I was getting the object printed out, instead of the rendered version.
For programming in R, do the following when using Jupyter Notebook or Jupyter Lab - (using the R kernel). These steps will display a web link and an image in a Notebook markdown cell. The following shows a real-life example of some study notes using Jupyter Lab and R.
First open a markdown cell in Jupyter - can be a new markdown cell or an existing markdown cell. Then copy and paste the actual web address into a markdown cell. This will provide an active link to that website from the Notebook.
Step 2, from that website, copy the image that you want to view in the Notebook. This image should be in a standard image format (.png, .jpg, etc ). Paste this image into the same folder on the computer where the Jupyter notebook file is located. Note: if the image is later deemed too large or small, then resize using any graphics software available - and then save the changed image into this same folder. Note: it is important to know the name of this image file.
Next, paste the name of the image file between the quotation marks in the following code: . If this file in not within your existing jupyter notebook working directory, then a path to the image file will need to be placed inside the quotation marks.
Step 3, also included is an example of the line of code (also used in Notebook markdown cell) to create colored text in markdown cells. In this line of code, the double ## character results in the second largest font being used in Jupyter. Smaller text using more of these characters - with #### being the smallest. One # results in the largest font output.
Last, be sure to close and run the markdown cell to view the output. The code for the markdown cell follows, and further below shows the output from the Notebook.
Code in Markdown cell:
"https://www.tensorflow.org/images/colab_logo_32px.png" # link to website
<img src="tidyflow.png" /> # The image file (This path is the same folder as Notebook file)
## <font color = cyan> Some Colored Text in Notebook Markdown Cell </font> # colored text
Output:

Resources