Using not uploaded file in jupyter notebook - python

Is it possible to use not uploaded files for example images in your jupyter notebook or is there a script for uploading files into your jupyter notebook? I have done some research but didn't find anything.

For sure! Like an usual python script. Let's say you want to open a file named "file.txt" and read its content.
with open("the/path/of/file.txt", "r") as file:
text = file.read()
Now an image using cv2 named picture.jpg
image = cv2.imread("the/path/of/the/image/picture.png)

Related

How to solve, file not found error from script, but works in notebook?

I have a code to load a json file.
with open("data/movie_data.json", "r") as j:
word_map = json.load(j)
The data folder is in current directory. However, this code works in the jupyter notebook, but while running from a script, it says, file not found error. Both the script and notebook are in same folder, that contains 'data' folder. What am I missing?
Try
"./data/movie_data.json"
instead of
"data/movie_data.json"

view the details of a netcdf file in spyder using xarray

I open a NetCDF file using xarray xr.open_dataset in Spyder.
I would like to view the contents of the file, so I click on the file at the Variable Explorer of Spyder. It looks ugly and non-comprehensible like in this screenshot:
How could I view the contents of the file in a nice and clear way in Spyder, same as for example the Google Colab would do? Here is a screenshot of how Google Colab would show the contents of the same netcdf file:

File not found when running a file in JupiterLab console

Every time when I try to run a file in the JupiterLab console I get the following message:
ERROR:root:File 'thing.py' not found.
In this case, my file is called thing.py and I try to run it with the trivial run thing.py command in the console. The code is running and it gives me correct results when executed in the console, but I wanted to have it saved, so I put it in a JupiterLab text file and changed the extension to .py instead of .txt. But I get the aforementioned message regardless of which file I try to run. I am new to JupiterLab and admit that I might have missed something important. Every help is much appreciated.
If you're running Jupyterlab you should be able:
to create a new file & paste in your commands
Rename that file to "thing.py"
And then open a console in the same Jupyterlab instance and run that file. Notice that you can see "thing.py" in the file explorer on the left:
Alternatively, you can use the %load magic command in a notebook to dynamically load the code into a notebook's cell.
You might want to understand exactly what a Jupyter Lab file is, and what a Jupyter Lab file is not. The Jupyter Notebooks have the extension, .ipynb.
So anyway, the Jupyter Notebooks are not saved or formatted with python extensions. There are no Jupyter Notebooks or Jupyter Labs ending with the .py extension. That means Jupyter will not recognize an extension having .py, or .txt or .R or etc.... Jupyter opens, reads, and saves files having the .ipynb extension.
Jupyter Notebooks are an open document format based on JSON.
Jupyter can export in a number of different formats. Under the File tab, is the Export feature. The last time I looked there were about 20 different export formats. But there isn't a python or .py export format. A Jupyter file can also be Downloaded. Also under the File tab is the Download feature. This will download a standard text formatted JSON file. JSON files are mostly unreadable unless you've spent years coding JSON.
So there's not much purpose in downloading the Jupyter file unless you are working on a remote server and cannot save your work at that site. And it makes much more sense to save and copy the Jupyter file in its native Jupyter format - that means having the extension, .ipynb . Then just open and use that file on another PC.
Hopefully this should clarify why Jupyter won't open any .py or .txt files.

How to Upload a File Using Pandas in Python

I'm having a problem uploading a file ( the file is called "kickstarter1.csv" it is in the image that I attached) using Pandas in python. In the bottom of the picture I attached, it is saying that the file does not exist. I found out a way to view the full path of my file which is located at the bottom of the finder window ( it is in the image I attached). How do I code the full path into my pandas code? I'm using Anaconda Navigator ( I'm not sure if that is relevant).
provide the complete path of your CSV file while reading it through pandas as
name_you_want = pd.read_csv('path/file_name.csv')
or go to the specific folder using cd command on notebook and then read the CSV file.

Opening a .ipynb.txt File

I have got downloaded a file that got downloaded in a format .pynb.txt extension. Can anyone help me to figure how to make it in a readable format?
Attaching a screenshot of the file when i tried opening in python notebook.
What you have on your hands is an IPython Notebook file. (Now renamed to Jupyter Notebook
you can open it using the command ipython notebook filename.ipynb from the directory it is downloaded on to.
If you are on a newer machine, open the file as jupyter notebook filename.ipynb.
do not forget to remove the .txt extension.
the file has a series of python code/statements and markdown text that you can run/inspect/save/share. read more about ipython notebook from the website.
if you do not have IPython installed, you can do
pip install ipython
or check out installation instructions at the ipython website
If you have a unix/linux system I'd just rename the file via command line
mv file_name.pynb.txt file_name.ipynb
worked like a charm for me!
These steps work for me:
Open the file in Jupyter Notebook.
Rename the file: Click File > Rename, change the name so that it ends with '.ipynb' behind, and click OK
Close the file.
From the Jupyter Notebook's directory tree, click the filename to open it.
Try the following steps:
Download the file open it in the Juypter Notebook.
Go to File -> Rename and remove the .txt extension from the end; so now the file name has just .ipynb extension.
Now reopen it from the Juypter Notebook.
I used to read jupiter nb files with this code:
import codecs
import json
f = codecs.open("JupFileName.ipynb", 'r')
source = f.read()
y = json.loads(source)
pySource = '##Python code from jpynb:\n'
for x in y['cells']:
for x2 in x['source']:
pySource = pySource + x2
if x2[-1] != '\n':
pySource = pySource + '\n'
print(pySource)
Below is the easiest way in case if Anaconda is already installed.
1) Under "Files", there is an option called,"Upload".
2) Click on "Upload" button and it asks for the path of the file and select
the file and click on upload button present beside the file.
The trick that works is this:
Open the file with jupyter notebook. It is not going to display properly, don't work.
Click on file > rename and remove the ".txt" attached to the file's name immediately after ".ipynb"
Close the file and reopen it.
Enjoy it.
go to cmd get into file directory and type
jupyter notebook filename.ipynb in my case it open code editor and provide local host connection string copy that string and paste in any browser!done
I faced a similar situation what I did is created a blank .ipynb file via Jupyter and replacted the code in the blank file.
CMD in Windows machine type jupyter notebook
Then Opened new IPY kernal
In the new IPY Kernal went to file>>Download as>> Notebook(.ipynb)
This will create a blank .ipynb file
opened that file in notepad and replaced the code.
I faced a similar issue recently. (Using Linux)
What I did is-
searched for jupyter support in VsCodium(Visual Studio Code)
the package named "ms-python" is the support for jupyter which opens ".ipynb" file.
Click on the second ext in the image
Another way of opening a ".ipynb" file is simply opening it on the jupyter online Jupyter online you can upload your files and continue
Upload the file on Jupyter Notebook with (.ipynb.txt) format.
Single click on the file name.
Rename it.
Click Save.
Works for me as of 9/17/2022

Categories

Resources