Jupyter Notebook - run cell and save as .py file - python

I know of the %%writefile .py cell-magic command but it causes the cell to not run. What I want is for the cell to run and create the .py as well.
Edit 1: I only want the original code in the cell to be saved as a .py file. I don't need the results

I would try to:
Use the %%writefile magic in the next cell, referencing the last one
or
Use the %save line magic instead.

There are some ways to deal with it.
Follow two ideas:
%%writefile in the first cell followed by %run yourscript or !python 'yourscript.py'
Convert your code into a string, run it with exec and save it.
The first option is the most common, and you can pass arguments. The second option can be attractive because you don't depend on a Jupyter Notebook to use it.

Related

Use command prompt from python

I write a python scripts that after execute some db queries, save the result of that queries on different csv files.
Now, it's mandatory to rename this file with the production's timestamps and so every hour i got new file with new name.
The script run with a task scheduler every hour and after save my csv files I need to run automatically the command prompt and execute some command that includes my csv files name in the path....
Is it possible to run the cmd and paste him the path of csv file like a variable? in python I save the file in this way:
date_time_str_csv1 = now.strftime("%Y%m%d_%H%M_csv1")
I don't know how to write automatically the different file name when i call the cmd
If I understand your question correctly, one solution would be to simply execute the command-line command directly from the Python script.
You can use the subprocess module from Python (as also explained here: How do I execute a program or call a system command?).
This could look like this for example:
csv_file_name = date_time_str_csv1 +".csv"
subprocess.run(["cat", csv_file_name)
You can run a system cmd from within Python using os.system:
import os
os.system('command filename.csv')
Since the argument to os.system is a string, you can build it with your created filename above.
you can try using the subprocess library, and get a list of the files in the folder in an array. This example is using the linux shell:
import subprocess
str = subprocess.check_output('ls', shell=True)
arr = str.decode('utf-8').split('\n')
print(arr)
After this you can iterate to find the newest file and use that one as the variable.

Call all files from a menu

I've got a task to do that is crushing my head. I have five .py documents and I want to make a menu in another .py so I can run any of them by introducing a string inside an input() but don't really see the way to do that and I don't know if there is somehow I can.
I have tried import every file to the 6th file but I don't even know how to start.
I would like it just to be seen as simple as it can sound, but yet I find it really hard.
If you just want to run them, then try this:-
import os
file_path = input("Enter the path of your file = ")
os.system(file_path)
If the file that you are trying to execute is not in the current
directory, i.e. doesn't exist in the same folder as the currently
executing python file, then you have to provide it's full path.
Path Format:-. C:\Users\lmYoona\OneDrive\Desktop\example.py
If the python file you are trying to execute is in the same directory as
the currently executing python file, then abstract name will also
work
Path Format:- example.py
P.S.:- I would only recommend this method if all you want is just to execute the other python file, rather then importing stuff from it.

pandas to_csv doesn't output the file

I'm using jupyter notebook pandas to_csv doesn't output the dataframe to a file.
I tried to use to_csv to output a dataframe to a csv file by setting the working directory or specify the directory, and it didn't create any file. The code ran and didn't produce any error message, but when I opened the folder, there was no such file.
I tried a different IO, and it did show the result had been output.
from io import StringIO
output = StringIO()
a.to_csv(output)
print(output.getvalue())
I got the following output:
,a
0,1
1,2
2,3
but again to_csv('filepath/filename.csv') doesn't output any file.
PS: I can read any file in any directory using read_csv().
Update
If I save the file df.to_csv('testfile.csv') then do pd.read_csv('testfile.csv')
I can read the file but cannot see it in the directory.
Also, doing [x for x in os.listdir() if x == 'testfile.csv'] will list the file.
I think the issue is that you're running a Jupyter Notebook so the "current directory" for the notebook is probably somewhere in "C:\Users\user_name\AppData...".
Try running os.getcwd() on its own in your notebook. It probably won't be the same folder as where the *.ipynb file is saved. So as #Chris suggested in comments, this:
df.to_csv(os.getcwd()+'\\file.csv')
... will send your csv into the AppData folder.
You could either change the working directory for the Jupyter notebook, or you could use a fully specified filename like:
df.to_csv('C:\\Users\\<user_name>\\Desktop\\file.csv')
(Note: this also tripped me up in VS-Code while using the interactive iPython execution that happens when you press shift+enter. Interestingly, in VS-Code, if you use ctrl+shift+p and select "Python: Run selection..." it executes in your default terminal, which doesn't have this problem.)
you probably forgot to add the name of the file after your path, so it will named your file as the last character of your path, which you can see on the home page of jupyter.
should be:
df.to_csv('path/filename.csv', ....)
rather than
df.to_csv('path.csv'......)
I had the same problem using spyder. In my case it was caused by the internet security tool (COMODO) I used, which somehow executed spyder in a sandbox or so and not allowed it to write to the "normal" directories. Instead it saved the result to a folder named C:VTRoot\HarddiskVolume2\users\. You can try to save a file with a quite unique name a.to_csv('very_unique_filename.csv') and then search in the windows explorer for that filename to find the folder it is stored in. If the reason is some tool like this, changing it's settings may help.
Maybe you do not have access to your output folder.
First try the current dir, like to_csv('tmp.csv').
Then check the directory's ownership by using ls -l.
This will give you a normal document even though you have used to_csv:
df.to_csv(**'df',** index = False)
Make sure to use 'df.csv' this will ensure the output CSV file.
df.to_csv(**'df.csv'**, index = False)
in my case the files were saved to my apps root folder (as expected).
but i needed to restart Jupyter even though i had auto reload enabled:
%load_ext autoreload
%autoreload 2
meaning, reload works for most changes, but it did not work when i added df.to_csv until restarting jupyter notebook

How to run .py from Jupyter notebook when paths are stored as strings in list?

I see that %run filepath.py lets me execute the code in a .py file. However, it appears to only accept unquoted paths or paths quoted with ", not expressions that would evaluate to a path. For example these work:
%run ../some/dir/script.py
%run "../some/dir/script.py"
But these do not:
%run '../some/dir/script.py'
ERROR:root:File `"'../some/dir/script.py'.py"` not found.
%run "../some/dir/" + "script.py"
ERROR:root:File `'../some/dir/.py'` not found.
paths[1]
'../some/dir/script.py'
%run paths[1]
ERROR:root:File `'paths[1].py'` not found.
I am inferring that this is something to do with what a magic command is or does in IPython. However, I don't understand paths or order of evaluation in Python well enough to understand where in the documentation this behaviour would be described or figure out that this wouldn't work beforehand.
So the question: What is the best way to run many .py files from a Jupyter notebook, given that I know or can construct their paths with os.listdir or something else? Is there an alternative way to run a bunch of code? And what am I misunderstanding about how magic commands and paths work that led me to expect that I could do things like %run paths[1]?
All advice appreciated, new to Python ways.
EDIT: I am not trying to have personal methods or functions available in a notebook, which it seems like creating modules is for. These scripts are each creating particular large list objects (Earth Engine FeatureCollections) and are not really supposed to be reusable code, they are just big chunks of text. Example of first few lines of each one:
CLEAR_20160129T072832 = ee.FeatureCollection(
[ee.Feature(
ee.Geometry.Polygon(
[[[43.29505920410156, 7.921873929606968],
[43.29814910888672, 7.8878678877391515],
[43.335227966308594, 7.883106818446446],
[43.341064453125, 7.9072516750710635],
[43.33179473876953, 7.925614422980302],
[43.297462463378906, 7.920853789152566]]]),
{
ad nauseam for hundreds of lines.

What does it mean to .py into python program?

So I have an assignment, and For a specific section, we are supposed to import a .py file into our program."You will need to import histogram.py into your program."
Does that simply mean to create a new python file and just copy and past whatever is in the histogram.py into the file?
This part of my assignment is to create a graphical display with the contents in the .py file (which confuses me too) I was reading the chapters from the tb and it states how to create a window, but I havent seen anything about importing.. Sorry if this is a dumb question
You have to create your own python file and simply add
import histogram
at the top of the file (or lets say in the "import" section).
Yout don't need the .py extension. Make sure the histogram.py file is in the same folder like your created py file.
You need to make sure you have the files you need before you try and import them, either downloading them or getting them through pyget. Once you have your files then you need to call them (2.7 docs)
from .foo import Foo
from .bar import Bar
import Fizz
All you have to do is paste the file named histogram.py to your working directory and to use the file on your code just type import histogram on the top most line of your main code file then you can use anything(functions and variables) from histogram.py.

Categories

Resources