# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Intro
# In this document I want to give an overview of magic commands, short cuts and code snippets I find extremely helpful in my day-2-day work with jupyter-notebooks. 

# # Command Mode
# I don't know if it is just me or if there are some other people as well, who came across the command mode quite late. So this would have saved me really a lot of time!
#
# To enter the command mode, just press ```esc```.
#
# - Now you can move across the cells with the arrow keys.
# - Enable editing of a specific cell, just by hitting ```enter```.
# - Insert new cells before or after the current cell with ```a```, ```b```.
# - Change between ```code```and ```markdown```with ```y```and ```m```.
# - Deleting a cell is as easy as pressing ```x```.
# - ...
#
# Check out https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ for more functionalities.

# # Toggling code
# Sometimes you like to use the notebook as a nice and clean documentation for sharing your results and maybe you don't want to disturb the viewer with long and maybe messy codecells. Then you can insert one of the following code snippets at the beginning of your notebook.
#
# The first one is for toggling all code in the notebook # https://stackoverflow.com/questions/27934885/how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer
#
# The second one is for toggling one specific cell by adding the command ```hide_toggle()``` at the end https://stackoverflow.com/questions/31517194/how-to-hide-one-specific-cell-input-or-output-in-ipython-notebook/48084050

# +
from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')

# +
from IPython.display import HTML
import random

def hide_toggle(for_next=False):
    this_cell = """$('div.cell.code_cell.rendered.selected')"""
    next_cell = this_cell + '.next()'

    toggle_text = 'Toggle show/hide'  # text shown on toggle link
    target_cell = this_cell  # target cell to control with toggle
    js_hide_current = ''  # bit of JS to permanently hide code in current cell (only when toggling next cell)

    if for_next:
        target_cell = next_cell
        toggle_text += ' next cell'
        js_hide_current = this_cell + '.find("div.input").hide();'

    js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))

    html = """
        <script>
            function {f_name}() {{
                {cell_selector}.find('div.input').toggle();
            }}

            {js_hide_current}
        </script>

        <a href="javascript:{f_name}()">{toggle_text}</a>
    """.format(
        f_name=js_f_name,
        cell_selector=target_cell,
        js_hide_current=js_hide_current, 
        toggle_text=toggle_text
    )

    return HTML(html)

hide_toggle()
# -

# # Toggle output
# Sometimes you also want to toggle the output, especially if you have created some long print statements in a loop and you don't want to scroll down so much.
#
# Therefore you have to enter the command mode again, press ```esc``` and use the shortcut ```esc + o```. The same shortcut is used to untoggle the output.

# # Variable output
# One great thing about jupyter-notebook is the output of all variables, including arrays and dataframes. But by default only the last statement will produce a printed output. So, in the end you will end up with lots of cells or un/commenting lines for printing several variables. But there is an easy solution - check out the following code snippet. Found on https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ - there is also the description how to enable this permanently for all your notebooks.

# +
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

hide_toggle()

# +
a=10
b=20
c=a+b

a
b
c
# -

# # Help - man pages
# By adding a ```?``` in front of a function, you will get the documentation for this function, so no need to google it while coding.
#
# --> And if you want to close the documentation, just hit ```esc```.

# ?print

# # Execute shell commands
# For executing shell commands, e.g. creating folders, getting filenames, you just have to preset a ```!```

# !ls -al

# # Magic commands
# Ipython has so called magic commands built-in, from which I find some extremely helpful!
#
# Some more examples can also be found here:
# https://towardsdatascience.com/top-8-magic-commands-in-jupyter-notebook-c1582e813560
#
# To get an overview of all available commands, type ```%lsmagic```.

# %lsmagic

# In the following list I describe my favourite commands
# - %%time: Measure cell execution time
# - %env: set and get system environment variables
# - %who: show all variables of a specific type, e.g. str / int
# - %load: load code into your notebook (does not work with .ipynb, but with .py)
# - %run: execute external code (does work with .ipynb)

# %%time
for i in range(1,10000):
    i+=1

# %env NEW_ENV=MAGIC
# %env

# %who int

# %load ./hello_world.py 

# %run ./hello_world.ipynb

# # Virtual Environments w/ Jupyter
# Python and PYthon packages are quite volatile and dynamic. So depending on versions packages might be or migt not be compatible. Virtual environemnts are very common to ensure a stable python env for your current project with the needed packages without breaking your systemwide python installation.
#
# Setting up the venv is well known
#
# ```python
# python3 -m venv --system-site-packages NAME_ENV
# ```
#
# But if you use jupyter-notebooks a lot for development, you want to use the same environemnts also in your notebooks:
#
# ```python
# pip install --user ipykernel
# python -m ipykernel install --user --name=myenv
# source env/bin/activate
# ```



StarcoderdataJupyterScriptsDedupFiltered