"Saving" data in Python internally - python

I write code mainly in Python or R depending on the situation. However, one thing I REALLY like about R is that it saves your data into the IDE. So you can continue doing stuff on this data without the need to run all the code again each time. Is this even possible in Python, as elegant as R, or do I just have to save a file onto my HDD every time I compute some data that takes some time ?

I recommend changing your IDE from what ever you are using to something like Visual Studio Code and downloading the Jupyter extension or using Jupyter Notebooks. It does precisely what you are asking for, saving variables during the session so you can modify them on the fly.
Compared with other IDEs, Visual Studio Code has the best support for jupyter that I have seen without using the notebook entirely based on my experiences using various IDEs.

Related

VSCODE - PYTHON - Pandas DataFrame - Intellisense doesn't show Attributes/methods of the object

After importing Pandas, when creating a pandas dataframe, Intellisense doesn't show the available attributes/methods of the created object.(Image 2, where I try to use the .head() function).
It detects the module pd(pandas) methods without any problem (see Image 1).
I don't have this problem when running a Jupyter Notebook or Jupyter Lab on the browser.
I'm using:
Windows 7
Python 3.8.3 in a Conda environment.
VSCODE 1.46.1
Python extension 2020.6.90262
Microsoft Language Server
Visual Studio Intellicode 1.2.8
IMAGE 1: It uses intellisense to detect the module methods/attributes
IMAGE 2: Intellisense doesn't show the pandas object available attributes/methods
The detection isn't working because IntelliSense has a hard time with pandas (and pandas.read_csv() especially). It works in Jupyter because it's accessing the live data while IntelliSense has to infer everything from the source code statically.
I would advise trying out Pylance as it's the new language server from Microsoft and we have tried to support pandas appropriately. If Pylance doesn't work then
try different values for your python.languageServer setting and see which one gives you the best result.
Go to your VS Code explorer and open that folder you are currently working in. This should solve the problem. Or go to file-> Open Folder. You can also open your current working folder by hotkey ctrl + o .
Close but no cigar. In 2021, language servers still often break. I think VS code is a good idea but sometimes they just break things. I use Intellij for work and it is heavier but better in that regard. I'm sure they will get it right eventually but sadly i don't think they are taking it as serious as they should since data scientists are a big part of their costumers and if you create a pandas object you might be working with its methods for a while rather than direct methods off the modules! So it REALLY helps if we can access lets say pandas.DataFrame.groupby for example rather than just things directly after pandas alone. I keep using VS code as I like keeping my browser up and really enjoy the advantages of having an unified place to keep my python, R and notebook code :) We just need to be patient!

Change code while debugging python program in Visual Studio Code

Is it possible to change the code while debugging in VSCode and that the change will take effect immediately without rerun the code?
I'm using Microsoft Python extension.
It depends. There's no such thing as a hot reload in Python. The closest you can come is importlib.reload(), but realize that only reloads the module and not the objects which already exist in memory. IOW it typically doesn't do what you want in code (it's usually used in the REPL).
So, apparently that the closest way of doing it is to use Jupyter capabilities (that can be use via VSCode).
With Jupyter you can split your code to multiple cells and run every each one of them separately without run all from beginning.

What is the difference between Spyder and Jupyter?

I am learning Python for data science, but my problem is that I still don't understand the difference between Spyder and Jupyter!
I would like you guys to help me to understand the difference, please; I would appreciate that.
Here's just a basic summary of the two tools.
Jupyter is a very popular application used for data analysis. It's an IPython notebook ("interactive python"). You can run each block of code separately. For example, I can print a graph using matplotlib. Create a new block of code and print another graph. There are also cool functions like %timeit that test the speed of your code.
Spyder is an Integrated Development Environment (IDE) for Python like Atom, Visual Studio, etc. I use VS Code and I suggest you install it as well. It's easier to learn and get running. There's also tons of helpful youtube videos due to its popularity.
I prefer to use Jupyter notebook to analyze data whether it be in pandas dataframes or plots. When I'm developing a program or implementing new code on data I already analyzed, I use a text editor like VS Code.
There's a lot more to it, but I think that's all you need to know for now. As you gain more experience you'll learn more about the tools and find your preferences. If you want to know more, there a ton of information about them online with people who can probably explain this much better than I can.
I hope your journey into data science goes well! Just be patient and remember struggling is part of learning. Good luck!
Spyder Pros:
Code completion
Code cells: You can create code cells using Spyder.
Scientific libraries
PDB debugger
Help feature
cons:
Limited to python only.
Bad layout not customizable
Jupyter pros:
Easy to learn
Secure and free server - The Jupyter server can be utilized free of charge.
Keyboard shortcuts makes it easy and fast
Share Notebook
cons:
Not recommended for running long, nonconcurrent errands.
No IDE integration, no linting, and no code-style adjustment.
Read more in detail https://ssiddique.info/pycharm-vs-spyder-vs-jupyter.html

IPython Notebook: code reuse

Is it possible to plug IPython notebook into existing Python project and to be able to reuse some of the existing code w/o copy-pasting it into a notebook?
I am looking for a way to use IPython Notebooks as a part of a large Python project to quickly test hypothesis and to analyze data on the spot.
P.S. It would also be nice to be able to import Python files into a Notebook. Is it possible?
I see this is an old question, but I want to answer it if someone still looks it up.
P.S. It would also be nice to be able to import Python files into a Notebook. Is it possible?
You can import any python script (filexy.py) from the same folder as your notebook by simply stating import filexy.
Relating to that, I'd suggest that you define functions for your most reused code bits and gather them in a library (filexy.py) that you import in your notebook. Use the notebook as a short, clean, "working-desk" and your filexy.py library as the "toolbox".
That way you can also solve:
I am looking for a way to use IPython Notebooks as a part of a large Python project to quickly test hypothesis and to analyze data on the spot.

Scientific Reporting in Python

I am working on a scientific python project performing a bunch of computations generating a lot of data.
The problem comes when reports have to be generated from these data, with images embedded (mostly computed with matplotlib). I'd like to use a python module or tool to be able to describe the reports and "build" HTML pages for these reports (or any format supported by a browser).
I was thinking about generating an ipython notebook but I was unable to find if there is a way to do so (except creating the json but I'm doubtful about this approach).
The other way is using Sphinx a bit like the matplotlib but I am not sure how I could really fine-tune the layouts of my various pages.
The last option is to use jinja2 templates (or django-templates or any template engine working) and embed matplotlib code inside.
I know it's vague but was unable to find any kind of reference.
nbconvert has been merged into IPython itself, so please do not use the standalone version anymore. It is now fully template base so you can change things from just tweeking the css, fully re-wrote your templates, or just overwrite the current part of templates you want.
Notebook format is a pure json file, is takes ~20 lines to write a program that loop through it and re-run the codecell. That plus command line argument it is not hard to write a notebook, make it a 'template' notebook and run it on multiple dataset without opening a browser.
Some resources :
programatically run nbconvert, and run a notebook headless (first link)
I think you want to work in ipython notebook and then use nbconvert
Currently, this is it's own utility, that already works (albeit with some installation hurdles, but working) but it is currently being implemented directly into the ipython notebook machinery, which I believe should be released in autumn, or so.
The goal is (and Fernando Perez has demonstrated that this works), that a notebook becomes a fully documented, image containing pdf-document after the conversion.
Using the inline-modus of ipython notebook,
ipython notebook --pylab inline
you can execute your matplotlib-scripts in a browser interactively (thus generating your plots). Then go to
File -> Print View (in the notebook-menu, NOT the browser menu)
and save the generated html-File (via the browser menu). This will include all the plots you generated before as well as the python code. Of course, you cannot modify these html-Files anymore without the notebook-server in the background.
Is this what you mean?
I just found this old question and want to add PWeave to the list, which is perfectly suited to generate reports from python code / jupyter notebooks. I use it to share my work with colleagues that aren't invested with programming alot.
It also integrates into Spyder, THE scientific IDE for python, using the spyder-reports module.

Categories

Resources