how to upload a text file into ipython notebook - jupyter-notebook

I want a whole file as a text file instead of just a cell in IPython notebook.
I write some codes in IPython notebook and now I want to test them ,so I tried to upload some text file into IPython notebook as the raw data.But the files' extension are always ".ipynb" and the format of the text files have changed so my code can't read it correctly.
How could I upload a text file into Ipython notebook? thanks in advance

Can you be more elaborate?
Are you trying to read data out of the text file? In that case regular python open and read functions can get you the data into variables. You can even do Unix "cat" the contents of the file in a variable:
var=!cat file
Or are you trying to get the text in the ipynb files as content in an input cell so you can edit them and run them? The ipynb files are usually created by the notebook engine and they are JSON formatted. So if you just copy paste the text from them into a cell, they won't be usable as is. Use some JSON parsing to interpret their content. They are not regular python code so "%run file" wont work to get their code executed by ipython.
Elaborating the use cases may help others to give solutions...

The following lines open a text file and display the content in a jupyter notebook cell.
text_file = open(full/path/to/text/file)
file_content = text_file.read()
print(file_content)
text_file.close()
I hope this helps.

It may be late, you can do this
%%pycat full/path/to/text/file

Related

R Studio: Opening an RMD file that contains a report, and another of code, but there is no content. Am I missing a package?

I have a report sent to me in the format of an RMD file. Another RMD that contains the code. However, all that I see are empty files when opened. Im using anaconda: Rstudio 3 version 1.1.456 (is this outdated?)
Unless there is a script to open the file?
The .Rmd format is a particular flavor of markdown so you can open the file with any plain text editor to verify that it is not empty and indeed has the code inside.
I have a solution to my problem: For anyone trying to open an Rmd file in the future with similar circumstances.. open the file, while open, Go into "file - Reopen with encoding - UTF-8".

jupiter notebook and editors

While working with ipython I used to edit an object with:
ed my_obj
And the editor opened the code defining the class of the object. I cannot find how to do the same thing in a jupyter notebook. Is it any possible?
The magic command %ed and %edit won't work in jupyter notebook.
I'm not quite sure why exactly jupyter doesn't support this edition, but probably because it can't know exactly when the user finished editing the file and the edited data should back to the cell.
If you want to edit the content of a string in a cell, you can use the following command:
a = 'jupyter'
get_ipython().set_next_input(a)
You can also use the %%writefile to save the content of the cell into a file and edit it outside jupyter.
%%writefile test.txt
[1, 2, 3]
And also use %load to bring the content to the cell:
%load test.txt

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:

How to load a notebook without the outputs?

I mistakenly printed to much to the output during a single cell's execution and now the browser tab completely freezes every time that notebook is opened. I tried restarting ipython and it didn't help (I am guessing that each time it is loaded, also all the chunk of text is loaded with it).
Is there a way to load a notebook with outputs suspended or clear?
One hack if you're desperate: open the .ipynb file, which is a text file. Scroll down to the lengthy cell output and delete it. Of course, you need to be careful that the result is still a valid .ipynb file.
nbstripout is a simple tool that removes all output from a notebook (without needing to open the notebook in your browser).
your code will be saved in the form of JSON. open it with json viewer and carefully delete the unwanted output cell and save it back.

read .sas source code on osx without installing sas?

Plain and simple: is there a way to read (not run) .sas files on osx in order to rewrite old SAS programs in another language, e.g. R? Note I do not refer to reading sas data files – I know there are several ways, I am just interested in reading old SAS code.
.sas files containing SAS code should just be a text file. You can use any text editor that you like to open and modify these files. Since the system probably doesn't have .sas files associated with any particular program you can either use the "Open with" option when "right-clicking" on the file or you could open the editor first and then open the file from within the editor.
TextEdit will work. Another editor that I like is Komodo Edit.

Resources