Python/Jupyter: Difference between revisions

From Wiki
No edit summary
m (Marcluer verschob die Seite Python/Jupyter Notebook nach Python/Jupyter, ohne dabei eine Weiterleitung anzulegen)
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Installation ==
= Magic Functions =
* apt install python3-pip
== Timing ==
* pip3 install --upgrade pip
* pip3 install jupyter
 
== Magic Functions ==
* %timeit (time line)
* %timeit (time line)
* %%timeit (time block in lines below, same line = not timed setup)
* %%timeit (time block in lines below, same line = not timed setup)


== Config ==
== importing other jupyter notebook ==
* full width:  
* run.ipynb:
**~/.jupyter/custom/custom.css
<blockquote>
<blockquote>
<pre>
<pre>
.container { width:100% !important; }
% run 'lib_testprog.ipynb'
testfunct()
</pre>
</pre>
</blockquote>
</blockquote>


* autostart from rc.local
* lib_testprog.ipynb:
<blockquote>
<blockquote>
<pre>
<pre>
sudo -u pi /usr/local/bin/jupyter-notebook --config=/home/pi/.jupyter/jupyter_notebook_config.py
def testfunct()
    return 1
 
if __name__ == '__main__' and '__file__' not in globals():
    print(testfunct())
</pre>
</pre>
</blockquote>
</blockquote>


 
== sharing variables between notebooks ==
* insecure access without token/password (/home/pi/.jupyter/jupyter_notebook_config.py)
* test1.ipynb:
<blockquote>
<blockquote>
<pre>
<pre>
c.NotebookApp.ip = '*'
variable = 'testdata'
c.NotebookApp.notebook_dir = '/srv/Python'
%store variable
c.NotebookApp.open_browser = False
c.NotebookApp.password = ''
c.NotebookApp.token = ''
</pre>
</pre>
</blockquote>
</blockquote>


== nbextensions ==
* test2.ipynb:
<blockquote>
<blockquote>
<pre>
<pre>
pip3 install jupyter_contrib_nbextensions
%store -r variable
jupyter contrib nbextension install --user
print(variable)
</pre>
</pre>
</blockquote>
</blockquote>


* codefolding
* hints:
* Codefolding in editor
** use "global" in functions
* Collapsible Headings
* Table of contents
* Variable inspector
* Toggle all line numbers


== importing other jupyter notebook ==
= Autorun notebooks on startup =
* run.ipynb
== simple, but no start/stop control later ==
* /home/user/.ipython/profile_default/startup/startup.ipy
<blockquote>
<blockquote>
<pre>
<pre>
% run 'lib_testprog.ipynb'
#!/usr/bin/ipython
testfunct()
 
%run /srv/python/script_a.ipynb
</pre>
</pre>
</blockquote>
</blockquote>


* lib_testprog.ipynb
== complex, with full control ==
* /home/user/.ipython/profile_default/startup/startup.ipy
<blockquote>
<blockquote>
<pre>
<pre>
def testfunct()
#!/usr/bin/ipython
     return 1
 
%run /srv/python/startup.ipynb
</pre>
</blockquote>
* /srv/python/startup.ipynb
<blockquote>
<pre>
from selenium import webdriver 
from selenium.webdriver.common.by import By
 
urls = ["http://192.168.111.11:8888/notebooks/projects/test.ipynb"]
 
def start_notebook(url):
    browser = webdriver.PhantomJS(service_args=['--ssl-protocol=any'])
     browser.set_window_size(1280, 1400)
    browser.implicitly_wait(10) 
    browser.get(url)
   
    browser.find_element(By.LINK_TEXT, 'Cell').click()
    browser.find_element(By.LINK_TEXT, 'Run All').click()
    browser.quit()


if __name__ == '__main__' and '__file__' not in globals():
for url in urls:
     print(testfunct())
     print ("starting: " + url)
    start_notebook(url)
</pre>
</pre>
</blockquote>
</blockquote>


== Links ==
 
= Links =
* https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
* https://github.com/jupyter/jupyter/wiki/a-gallery-of-interesting-jupyter-notebooks
* https://stackoverflow.com/questions/26126853/verifying-pep8-in-ipython-notebook-code
* https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
* https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

Latest revision as of 11:13, 24 March 2021

Magic Functions

Timing

  • %timeit (time line)
  • %%timeit (time block in lines below, same line = not timed setup)

importing other jupyter notebook

  • run.ipynb:
% run 'lib_testprog.ipynb'
testfunct()
  • lib_testprog.ipynb:
def testfunct()
    return 1

if __name__ == '__main__' and '__file__' not in globals():
    print(testfunct())

sharing variables between notebooks

  • test1.ipynb:
variable = 'testdata'
%store variable
  • test2.ipynb:
%store -r variable
print(variable)
  • hints:
    • use "global" in functions

Autorun notebooks on startup

simple, but no start/stop control later

  • /home/user/.ipython/profile_default/startup/startup.ipy
#!/usr/bin/ipython

%run /srv/python/script_a.ipynb

complex, with full control

  • /home/user/.ipython/profile_default/startup/startup.ipy
#!/usr/bin/ipython

%run /srv/python/startup.ipynb
  • /srv/python/startup.ipynb
from selenium import webdriver  
from selenium.webdriver.common.by import By

urls = ["http://192.168.111.11:8888/notebooks/projects/test.ipynb"]

def start_notebook(url):
    browser = webdriver.PhantomJS(service_args=['--ssl-protocol=any'])  
    browser.set_window_size(1280, 1400)
    browser.implicitly_wait(10)  
    browser.get(url) 
    
    browser.find_element(By.LINK_TEXT, 'Cell').click()
    browser.find_element(By.LINK_TEXT, 'Run All').click()
    browser.quit()

for url in urls:
    print ("starting: " + url)
    start_notebook(url)


Links