Tag: 115

  • Jupyter Notebook

    Jupyter Notebook

    I have been programming with python for quite some time and been using Jupyter notebook for some data science projects, I like the UI and Jupter notebook provides with a web-based interactive development environment for python notebooks, code, and data.

    There are many blogs which will help you setup your notebook environment and itse really not difficult either and can be summarised in the following 4 commands

    $ python -m venv jupyter
    
    $ source jupyter/bin/activate
    
    $ pip install jupyter
    
    $ jupyter notebook

    Recently I tried to explore more on how I can possibly start jupyter notebook on my Android Device. The command does work on Android, but it comes with caveats. Here is somewhat quick detailed post on setting up Jupyter Notebook on Android Phone/Tablet.

    To run jupyter notebook, we require a working python development environment on Android.

    There are many ways to do this. I choose and prefer to use Termux App.

    Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically – additional packages are available using the APT package manager.

    – https://termux.com

    I have taken this install instruction from the stackoverflow answer with my amendments to it.

    1. Install the Termux app.
    2. Open Termux, then enter/run the following commands in the prompt:
      $ apt install clang python fftw libzmq freetype libpng pkg-config libcrypt
      $ LDFLAGS="-lm -lcompiler_rt" pip install jupyterFinally, test the notebook out by running:
    3. $ jupyter notebook
      

    When you run jupyter notebook, a notebook server starts up and dumps some information to stdout. When you see the line:

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:

    copy the following URL, paste it into the browser on your device, and then hit go. You should end up with something that looks like this:

    I tested these instructions out a Samsung Galaxy Tab 10 2020 (wifi) running stock Android 10 (10).

     

    I tried to provide as much details as pososble, but in case if i missed anything please let me know in the comments.

     

     

  • Using Ipython

    I started to move along with using python 3, I want my python shell for to be working properly. As many of us must be aware, Python Shell is not really a very interactive prompt and most of the time, while I am working with Python, I tend to install Ipython as an alternative to work more flexibly with Python Shell.

    When I downloaded and installed python3. I do not get ipython, this needs to be installed separately.

    The pip3 install ipython3 didn’t came of much help.

    As, I am having multiple versions of python running on to my system, one is the native python version 2.7.11 which by default came with my OS, other python version 3.5.1 which I had explicity downloaded for using python3.

    Googling around did helped much either, and the solution was pretty simple for implementation so I am documenting here in case anyone needs a quick help.

    $ which ipython
    /usr/local/bin/ipython
    $ which python3
    /usr/local/bin/python3
    
    cp /usr/local/bin/ipython /usr/local/bin/ipython3
    

    edit the ipython3 file.

    #!/usr/local/bin/python
    
    # -*- coding: utf-8 -*-
    import re
    import sys
    
    from IPython import start_ipython
    
    if __name__ == '__main__':
        sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
        sys.exit(start_ipython())

    Change the shebang which is #!/usr/local/bin/python to #!/usr/local/bin/python3

    Save the file.

    run ipython3, I am assuming /usr/local/bin is in your system path.

    $ ipython
    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 4.2.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]:

    What we basically did, was copied the original ipython file which was being used by python version 2 and ensured that to work with python3 by changing its shebang line, which tells ipython to use the desired python version. In case, if you do not already have ipython installed then pip install ipython will just work, post your python 3 installation.

    Though this is a quick hack, but really not an ideal solution.

    Try and use virtual environment in case of multiple python installations.

  • Python Virutal Environment on Windows

    I am an Unix SA and had been from quite a some time and used to work with my Windows Desktop in office and Linux Laptop at home, but during this lockdown I had been left with no choice but to work primarily on a Windows Laptop all the time, I really did not get much time to start my working and development environment, which also begs me to write an ansible playbook, which is for some later day blog. But here is the guide on which I would possible write my Playbook to install python and set thigs up.

    Prerequisites:

    • A Windows machine
    • Python installer (if you are the admin of the laptop then all good, otherwise installing this without admin account is another level challenge)
    • Knowledge of working with Windows Command Prompt. (Yes this is going to be totally different that working with Linux Command Prompt)

    The setup is quite simple, follow the steps below or just copy and paste all even that is all good.

    I am planning to use Python 3.8.3 installed on to my system.

    To check if python is installed on your laptop/workstation or not just type on the cmd prompt from the start menu or press “Win + R” -> “cmd”.

    Once the command prompt is open, type python on the c: prompt

    Display python version on Windows

    you should see something like in the screenshot .

    Type exit () to come out of the prompt, usual Linux “Ctrl +d” will not work in here. Windows has its own limitations.

    Next is to create a virtalenv, where we can do your project work.

    I have plans to make a video of this, which I will do sometime sooner for setting up python and virtualenv in windows. #TODO

    c:\>mkdir projects
    
    c:\>cd projects
    
    c:\projects>python -m venv my_first_python_program
    
    c:\projects>cd my_first_python_program
    
    c:\projects\my_first_python_program>dir
    
    c:\projects\my_first_python_program>cd Scripts
    
    c:\projects\my_first_python_program\Scripts>activate
    
    (my_first_python_program) c:\projects\my_first_python_program\Scripts>
    How to setup a virtual env in windows 10
    Steps to steps virtual env in windows

    Once you setup and activated the environment, the environment name is added as the prefix of the prompt.

    Till the time, you do not “deactivate” your virtual environment. you are not going make any changes to your core operating system libraries.

    Now, go download the pip libraries and modules and try out new things.

    (my_first_python_program) c:\projects\my_first_python_program\Scripts>pip install jupyter

    Finally, Coming out of the virtual enviromnet

    (my_first_python_program) c:\projects\my_first_python_program\Scripts>deactivate
    
    c:\projects\my_first_python_program\Scripts>

    Thank you for reading and bearing my adventours. 🙂

  • Simple http server

    python -m SimpleHTTPServer