How to execute code form markdown file in PyCharm? - python

Is there plugin for PyCharm that can run code from markdown file? I would like to write some text, then make some calculations using python code and add result to my text. Then continue with my text.

PyCharm professional version supports Jupyter notebooks, using the "editor only" view mode gets pretty close to what you want.

Related

how to open jupyter projects on anyother IDE like vs_code

I am working with Juypter Notebook for a long time and I want to open my projects in vs_code (or any other IDE) and my "juypter_projects" folder where I contain all my Jupyter notebook projects appears in this way: (please try to understand my handwriting 😀😁)
and inside the .ipynb_checkpoints folder which is located inside the juypter_projects folder seems in this way:
and looks like none of the files inside these folders are of .py extension
hence so I dont know which file should I choose to open when trying to edit my code in other IDEs
and so when I tried to open them in any IDE like vs_code it is not working and this limits me to only edit my code inside "Juypter Notebook" and please tell me how can I open Juptyer Notebook projects and edit them in other IDEs... Thank You
When you write code in Jupyter cells, the notebook (.ipynb) is converted into a JSON object, making it very difficult to work in an IDE.
The solution my company landed on is writing everything in external python modules and importing them into the notebook. We use Jupyter widgets extensively and they can be imported into .py files as easily as importing them into notebooks. In the end, we get a library of custom widgets and modules that can be used by everyone, and since it's just python, we can build/edit in any IDE. I use Sublime.
There may be other ways, but this approach works so well for us that we've stopped looking for alternatives.

How to make VS code's jupyter notebook's codetext bold?

I am trying to make code text on jupyter notebook on VS code be bold, but no matter how I tweak the settings.json file I can't seem to find a way to make it happen. As I have noticed, usual python code text in .py files is changing when i change settings.json file but .ipynb file is not reacting. For some reason if I use command palette to change themes jupyter notebook's code is changing color as well but the codetext itself is not becomming bold. Is there a way to solve this?
I'm a developer on the VSCode python extension. Currently our notebook editor is implemented as a webview (basically a way for extensions to put a webpage into VS Code's UI). We've tried to match things up as best as we can, but in this scenario we can't support many VS Code editor settings and features. This setting that you are changing is one of the ones that we don't support.
On the plus side, VS Code core team is currently in the process of adding build in notebook support. When we move over to this the notebook cells will be full real VS Code editor instances and will support this setting. This feature is currently in early preview (expect plenty of change and issues) if you right click on a ipynb file in VS Code Insiders and pick "open in preview Notebook Editor."
At present, the Jupyter notebook function in VSCode is provided by the python extension.
When it is used as markdown, the text output is bold. (use "# %%[markdown]").
But it does not currently support setting its code bold style. We look forward to its further improvement of this setting.

Run from and save to .py file from Jupyter Notebook

On my old computer, I was able to run .py files from Jupyter Notebook, edit them, and run them. The .py file was effectively a notebook file for all intents and purposes. I updated to the latest version of notebook, and I am no longer able to do this. How do I use .py files on my notebook?
I know there are roundabout ways to do this. I am looking for the method where, when you are in notebook, instead of opening a .ipynb file, you select a .py file which is opened, and behaves like a .ipnyb. When you save it, it writes to .py.
A text file can be loaded in a notebook cell with the magic command %load.
If you execute a cell containing:
%loadpy filename.py
The content of filename.py will be loaded in the next cell. You can edit and execute it as usual.
To save the cell content back into a file add the cell-magic
%%writefile filename.py at the beginning of the cell and run it.
To see the help for any magic command add a ?: like %loadpy? or %%writefile?.
%COMMAND-NAME?
i.e. %run?
For list of available magic function use %lsmagic.
Alternatively there is also another method magic function called %save-f but I would not recommend that, it's an indirect way of saving files.
Also see -
1. Magic Functions docs
2.this nbviewer for further explanation with examples. Hope this helps.
This is not the exact answer. At one point, I was able to open .py files using python notebook and work on it as if it were a notebook file.
However, I have been able to replicate this behavior using VScode.
https://code.visualstudio.com/docs/python/jupyter-support-py
Using VScode, you can export all your .ipynb files into .py files, then run code blocks. Code blocks are separated by # %%.
I have not used it sufficiently long enough to decide if it is better than python notebook, but this seems to be the best solution so far. I previously tried using Atom/Hydrogen and did not enjoy the experience.
You can save individual cells as files using the following code: %%writefile some_file_name.py.
You can run that code straight from the terming or from another notebook using the following code: %run some_file_name.py
Some editors (like spyder and vscode) have jupyter notebook functionality. These can be used if jupyter in installed in the python environment.
You can use it by add #%% on top of the block of code. (in vscode the button 'run cell' will automatically appear)
Also it is possible to import .ipynb as .py which can be run in to fancy decrypt above.
I just found this package p2j and tested it with a .py file with functions, comments and normal code.
I used it as indicated in this answer by doing the following:
pip install p2j
p2j -o script.py -t new_file.ipynb
You can also add -o flag to overwrite the original file.
With this, I got a working Jupiter Notebook with each block of code in a cell and the comments as markdown.
Example:
Original .py script
Converted .ipynb

What is the difference between a .py file and .ipynb file?

I have read about .py and .ipy, also the difference between python, ipython and notebook.
But the question is: what is the real difference between .py and .ipynb file?
Is .ipynb file just more convenient to be run on jupyter notebook, or anything more? I am wondering because I am thinking about which format to be used for publishing on GitHub.
Thanks
.py is a regular python file. It's plain text and contains just your code.
.ipynb is a python notebook and it contains the notebook code, the execution results and other internal settings in a specific format. You can just run .ipynb on the jupyter environment.
Better way to understand the difference: open each file using a regular text editor like notepad (on Windows) or gedit (on Linux).
Save on git the .ipynb if you want to show the results of your script for didatic purposes, for example. But if you are going to run your code on a server, just save the .py
Adding #Josir answer, the below information is very useful for open .ipynb file using PyCharm.
Create a new Python project in Pycharm
Specify a virtual environment, and install the jupyter package(pip install jupyterlab).
Run the server using the jupyter-lab command.
Browser will open the jupyter notebook like below, there you can execute the .ipynp file.
Here is documentation https://jupyterlab.readthedocs.io/en/latest/
py means PYthon
ipynb means Interactive PYthon NoteBook - which is now known as Jupyter notebook.
The latter one is merely a Python script with descriptive contents - you describe what your data is doing by means of Python script and some funny texts. That's pretty much it - and also, you need a specific editor e.g. PyCharm or Google Collab to open and run it.
I think the answer here might help you: https://stackoverflow.com/a/32029027/11924650
.ipy indicates that it's an IPython script. The only difference between IPython scripts and normal Python scripts is that IPython scripts can use IPython magics, e.g. %timeit, and run system commands as !echo Hi.

Convert Markdown to pdf with Jupyter Notebook

I'm working in a jupyter notebook, and I'm working on a markdown file. I'm using a mac with Sierra 10.12.16. I'd like to convert the markdown to a pdf and I'm wondering what the easiest way to do that would be. I've read a lot of posts about installing things like pandoc or using packages in atom. I'm wondering if there isn't a simple way with the jupyter notebook to convert the markdown file to pdf. It really seems like there should be, but I'm having trouble finding it.
I figured it out. You can just paste the markdown code in to a cell in a jupyter notebook and change the dropdown menu below "Widgets" in the notebook from "code" to "markdown". Then hit shift enter and it renders the markdown. You can then download the notebook as pdf.

Categories