Ipynb — кто это и с чем его есть?

Jupyter-ноутбук — это среда разработки, где сразу можно видеть результат выполнения кода и его отдельных фрагментов. Отличие от традиционной среды разработки в том, что код можно разбить на куски и выполнять их в произвольном порядке. Представьте, что вы можете написать кусочек кода на салфетке и сказать салфетке: «Выполнись».
Немного объясню по подробнее, jupyter notebook это своего рода python, только модифицированный. Представьте, что Вам нужно на основе результатов предикта построить график, или допустим у Вас есть какая-та модель, и на основе её надо построить график в matplotlib или seoborn. Если база данных большая, то что бы изменить какую-лиюо строчку, или слой в модели и заново все просчитать потребуется много времени. С ipynb же вы можете загрузить допустим датасет один раз, и дальше не делать это повторно при рестарте кода. Здорово же!)
Как открывать и где писать код .ipynb
Существует множество редакторов для формата ipynb, но зачастую подготовка к кодингу занимает много времени, я же предлагаю воспользоваться Visual Studio Code.

Я думаю многие знакомы с такой программой как Vs code. Лично я был приверженец PyCharm, и к Vs относился не очень, но когда я первый раз попробовал покодить в ней , я сразу понял, что буду делать это в ней всегда)
Кто не знает или не работал с Vs, установить ее можно по этой ссылке.
Дальше следует установить python и подключить его к vs.
Когда у нас есть vs с подключенным python мы можем приступить к скачиванию jupyter notebook. Сделать это можно перейдя к вкладке Extensions в панели слева или нажав Ctrl+Shift+X


Там будет поле поиска в которое нужно забить слово Jupyter. Из всех плагинов надо выбрать этот. И нажать Install. Собственно все) Теперь надо лишь дождаться уведомления о том, что плагин скачен.
Самое интересное. Начало кодинга
Я буду использовать датасет про уровень счастья в разных годах и странах, и уровень доллара.
Для начала нам нужно создать новый файл с расширением .ipynb. Открыть его, и мы увидим это.

Для начала пропишем подключение библиотек — мне понадобятся эти

pandas — для загрузки csv датасета
остальное для получения красивых графиков
По нажатию на зеленый треугольник мы загрузим эти библиотеки, тут и начинается магия jupyter. Мы импортируем библиотеки, и все мы больше не должны тратить на это время.
Дальше с помощью не хитрых функций seaborn, matplotlib мы получаем вот такие графики —




Но вся прелесть не только в этом, в итоге мы получаем файл с кодом и картинками в нем! Теперь если мы хотим выложить это на гитхаб у нас будут красивый код с картинками


2-я часть выйдет скоро, она будет про дополнительную библиотеку Ipython.
Jupyter notebook support
With Jupyter Notebook integration available in PyCharm , you can easily edit, execute, and debug notebook source code and examine execution outputs including stream data, images, and other media.
Notebook support in PyCharm includes:
- Coding assistance:
- Error and syntax highlighting.
- Code completion.
- Ability to create line comments Control+/ .
Quick start with the Jupyter notebook in PyCharm
To start working with Jupyter notebooks in PyCharm:
- Create a new Python project, specify a virtual environment, and install the jupyter package.
- Open or create an .ipynb file.
- Add and edit source cells.
- Execute any of the code cells to launch the Jupyter server.
Get familiar with the user interface
Mind the following user interface features when working with Jupyter notebooks in PyCharm.
Notebook editor

A Jupyter notebook opened in the editor has its specific UI elements:
- Jupyter notebook toolbar : provides quick access to the most popular actions. The rest of the notebook-specific actions are available in the Cell menu.
- Code cell : a notebook cell that contains an executable code
- Cell output : results of the code cell execution; can be presented by a text output, table, or plot.
Notebook toolbar
The Jupyter notebook toolbar provides quick access to all basic operations with notebooks:

Adds a code cell below the selected cell.
Moves the selected item or items from the current location to the clipboard. Moves the entire cell if it’s selected.
Copies the selected item or items to the clipboard. Copies the entire cell if it’s selected.
Inserts the contents of the clipboard into the selected location. If you’ve selected an entire cell, the contents are pasted to a new cell below the selected one.
Moves the current cell up.
Moves the current cell down.
Executes this cell and selects a cell below. If there is no a cell below, PyCharm will create it.
Starts debugging for this cell.
Click this icon if you want to interrupt any cell execution.
Executes all cells in the notebook.
You can select a cell type from this list and change the type for the selected cell.
Deletes the current cell.

The Jupyter Server widget that shows the currently used Jupyter server. Click the widget and select Configure Jupyter Server to setup another local or remote Jupyter server.

List of the available Jupyter kernels.
Select this checkbox to allow executing JavaScript in your Jupyter notebook.
This actions selects the cell above.
This actions selects the cell blow.
You can preview the notebook in a browser.
Tool windows
The Server Log tab of the Jupyter tool window appears when you have any of the Jupyter server launched. The Server log tab of this window shows the current state of the Jupyter server and the link to the notebook in a browser.

It also provides controls to stop the running server () and launch the stopped server ().
The Jupyter Variables tool window the detailed report about variable values of the executed cell.
Create and edit Jupyter notebooks
To open an existing .ipynb file, follow the same steps as for the files of the other types. If needed, you can create a notebook file.
Create a notebook file
- Do one of the following:
- Right-click the target directory in the Project tool window, and select New from the context menu.
- Press Alt+Insert
- Select Jupyter Notebook .
- In the dialog that opens, type a filename.
A notebook document has the *.ipynb extension and is marked with the corresponding icon.
Editing Jupyter notebooks
You can apply various editing actions to one cell or to the entire notebook. Press the Control+A once to select a cell at caret, and press Control+A twice to select all cells in the notebook.
When editing notebook files, mind that PyCharm updates the source code and the preview of the notebook if it has been changed externally.
The editor for Jupyter notebooks has two modes: the edit mode and the command mode . Depending on the mode you can either edit code in notebook cells or use keyboard shortcuts to perform specific actions with cells.
Edit mode

- To toggle the edit mode, press Enter or click any cell.
- When a cell is in the edit mode, it has a green border on the left and a highlighted line with a caret.
- When in the edit mode, you can navigate through all cells line by line using Up / Down keys.
Command mode

- To toggle the command mode, press Esc or click the gutter.
- When a cell is in the command mode, it has a blue border on the left.
- When in the command mode, you can navigate the notebook cell by cell using Up / Down keys, as well as use keyboard shortcuts to select, copy, paste, and delete cells.
Edit cells

- A newly created notebook contains one code cell. You can change its type with the cell type selector in the notebook toolbar:
- To edit a code cell, just click it.
- To edit a Markdown cell, double-click it and start typing. To preview the output, press Shift + Enter .
Working with notebook cells
Add cells

- To add a code cell above the selected cell, do one of the following:
- In the edit mode, press Alt+Shift+A .
- In the command mode, press A .
- Select Cell | Add Code Cell Above in the main menu.
- To add a code cell below the selected cell, do one of the following:
- In the edit mode, press Alt+Shift+B .
- In the command mode, press B .
- Select Cell | Add Code Cell Below in the main menu.
- Click in the notebook toolbar.
- You can add code or Markdown cells by using the popup between cells:
Select cells
- To select a cell, click the gutter next to the cell.
- To select several cells:
- Click the gutter next to cells while holding Shift for a series of consecutive cells, or Control for non-consecutive cells.
- In command mode, press Shift and Up / Down keys.
You can execute, copy, merge, and delete the selected cells.
Copy and paste cells
- To copy a cell in the command mode, press Control+C , C , or click on the notebook toolbar.
- To paste the copied cell below, press Control+V , V , or click .
- To paste it above the current cell, press Shift with Control+V / Shift+V .
Split and merge cells
- To merge a current cell with the cell below, right-click the cell and select Merge Cell Below command from the context menu. Similarly, you can merge a cell above the selected cell with the corresponding command.
Delete cells
- In the command mode, press D, D or Delete .
- Click on the notebook editor toolbar.
- Right-click the cell and select Delete Cell from the context menu.
Use coding assistance
You can edit code cells with the help of Python code insights, such as syntax highlighting, code completion, and so on.
-
PyCharm enables code completion for the names of classes, functions, and variables. Start typing the name of the code construct, and the suggestion list appears.





The auto-completion of DataFrame columns names is available in runtime. PyCharm will suggest the names for columns only if the cell where the DataFrame is created has already run in the current kernel session.

- Intention actions and quick fixes. You can add the missing imports on-the-fly by using the intention actions. Note that you can add an import statement to the current cell or to the first cell of the notebook.
Jupyter notebooks in PyCharm
Today we’re going on a Python adventure using Jupyter notebooks and PyCharm! First, let’s talk about what these are. Jupyter Notebook is a web application where you can create interactive coding documents, supporting many programming languages including both Python and R as well as Markdown. PyCharm is an amazing IDE (interactive development environment) for Python that has tools and plugins to help you code more efficiently. You can develop your code both locally and remotely using PyCharm. First, let’s get set up!
Getting started with PyCharm
- Install PyCharm.I highly recommend installing PyCharm Professional because you get more features like SciView that are awesome for data science. Plus PyCharm Professional is free for students!
- Install Python if you don’t already have it — you’ll need an interpreter in order to use PyCharm! Be aware that Python 2 and Python 3 are different in terms of syntax — don’t worry, you can load either version in PyCharm when you start a new project. If this is your first time developing in Python, I recommend going with the latest version 3.8.3.
- Create a new project in PyCharm and select your local Python interpreter. Here, I created a new project called ‘pycharm’ in a directory on my desktop and selected Python 3.7 as my interpreter.

- Install Jupyter by selecting PyCharm >> Preferences >> Project Interpreter, then click the “+” button to add new packages.

Then type ‘jupyter’ and select jupyter from the packages list. Then click the ‘Install Package’ button at the bottom of the window.

- Create a new Jupyter notebook by navigating to File >> New… and selecting ‘Jupyter Notebook’. Alternatively, if you just want to create a Python script you can select ‘Python File’. Here, I created a new notebook called pycharm.ipynb.

- Let’s start editing our notebook by adding a Markdown cell. In the editor next to the ‘#%’, add ‘md’ to set the cell type to Markdown. Then use Markdown formatting, such as ‘### Header’ like in my example here. Notice that the right side panel displays a preview of your notebook.

- Now let’s add a coding cell. Click the ‘+’ button just below the Markdown cell to add a new cell. Add your code and click the green arrow to run the code in the cell.

Here I created and printed a variable called my_string.

- Let’s check out a cool feature of PyCharm Scientific View. Add the following to a code cell and run it:
import pandas as pd #read in data from my github repo data = pd.read_csv("https://raw.githubusercontent.com/CaitlinCasar/Casar2020_DeMMO_MineralHostedBiofilms/master/orig_data/site_geochem.csv") data.head()- Next, open your Jupyter tab at the bottom of your PyCharm window, click on the variables tab, and right click on the new variable you just created called ‘data’. Select ‘View as Dataframe’, then click on the ‘SciView’ tab on the right panel.

This is a great feature for viewing your stored variables. You can view both dataframes and arrays with SciView.

- Let’s try launching this notebook in our browser. Click on the text at the bottom of your PyCharm window that says “Jupyter Server started at http://localhost:8888// Open in Browser”. You should see an Event Log window pop up in the bottom right panel. Click on “Open in Browser” to launch your notebook in your web browser.

You should now see something like this in your browser:

Click on the notebook to run your code in the browser window. You can run cells by clicking the Run button in the top tool bar, or by clicking Ctrl + Enter.

Sync files with a remote server
- Let’s say you want to sync files on a remote server. You’ll need to set up your file transfer protocol. Select Tools >> Deployment >> Browse Remote Host. Then select your protocol — here I chose SFTP (secure file transfer protocol).


- Next, configure your connection to the remote host by adding your IP address and log in credentials. Optionally, set your root path to the path on the remote server where you want to access files.

- Click on the Mapping tab and set your local path to your project directory in PyCharm. Set the deployment path to the directory on the remote server where you want to access or upload files.

- Sync your local directory with the remote directory by right-clicking on your project in the left panel, then select Deployment >> Sync with Deployed To…

Then click the green double arrow button “Synchronize All” to sync your files. You can use the ‘Remote Host’ tab on the right panel to view your remote file tree.
Run a remote Jupyter server kernel
If you want to run an interactive Jupyter notebook on a remote server in PyCharm, you’ll need to set up your Jupyter server configuration and remote Python interpreter.
- In the terminal, SSH into your remote server, navigate to the directory where your data is, then launch a Jupyter notebook.

- Navigate to the directory where your data is located. Then launch a jupyter notebook by running the following:
#replace port number with whatever port you want jupyter notebook --port=8899 --no-browserThis will return a url and token, similar to:
https://my-notebook/tree/?token=abcdef.Copy this entire url and token. Make sure you copy it all from one line — I have to make my terminal full screen for this. Otherwise you might get a weird line break and your url/token might not work.
- Open a new Jupyter notebook file and select ‘Configure jupyter server…’.

Paste your url and token in the field for ‘Configured Server’. Then click ‘Apply’.

- You should now be able to access data on the remote server in your Jupyter notebook. Here, I cloned my repo on the remote server, and I’m accessing a csv file with the pandas library.

- If you want to use an interpreter on the remote server, you can figure the interpreter by naviating to Pycharm >> Preferences >> Project Interpreter…, then select the wheel button next to your current interpreter and select ‘Add…’. Then select ‘SSH interpreter’ and choose ‘Existing server configuration’. From the dropdown menu, select the SSH configuration that you set up in step 2 of the ‘Sync files with a remote server’ above.

Note:Unfortunately, you cannot view your variables while using a remote Jupyter server kernel as documented here.
Congrats on developing in Jupyter Notebooks and PyCharm! I hope you enjoyed this tutorial, feel free to comment below with any comments/questions!