<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmad Abdullah Tariq </title>
    <description>The latest articles on DEV Community by Ahmad Abdullah Tariq  (@ahmadabdullah247).</description>
    <link>https://dev.to/ahmadabdullah247</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F553383%2Fa330baf5-31ae-4813-b49b-5f4883cb5656.png</url>
      <title>DEV Community: Ahmad Abdullah Tariq </title>
      <link>https://dev.to/ahmadabdullah247</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmadabdullah247"/>
    <language>en</language>
    <item>
      <title>Configure Jupyter Global Imports</title>
      <dc:creator>Ahmad Abdullah Tariq </dc:creator>
      <pubDate>Mon, 18 Jan 2021 08:40:17 +0000</pubDate>
      <link>https://dev.to/ahmadabdullah247/configure-jupyter-global-imports-342e</link>
      <guid>https://dev.to/ahmadabdullah247/configure-jupyter-global-imports-342e</guid>
      <description>&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open a terminal and go to the following directory&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    cd ~/.ipython/profile_default/startup
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now create a python file. You can name it anything you want:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    touch start.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now open the file you can do inside the terminal or open up your favorite IDE.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    nano start.py
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AbPymBkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jho1jk2bwsvmshw6aab7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AbPymBkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jho1jk2bwsvmshw6aab7.png" alt="Screenshot 2021-01-18 at 09.32.07"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Place all the libraries that you want to get imported by default.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rnk7kJo6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sk7qy8lfm8b7v6fukcto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rnk7kJo6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sk7qy8lfm8b7v6fukcto.png" alt="Screenshot 2021-01-18 at 09.29.23"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No press &lt;code&gt;CMD+X&lt;/code&gt; and press &lt;code&gt;Y&lt;/code&gt; to save.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have jupyter instance running you need to restart them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To check if you successfully included those libraries you can check them in &lt;code&gt;globals()&lt;/code&gt; in a notebook:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lKCFbP9D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q929uf9i3aak4lduwaeu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lKCFbP9D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q929uf9i3aak4lduwaeu.png" alt="Screenshot 2021-01-18 at 09.35.18"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ref: &lt;a href="https://towardsdatascience.com/how-to-automatically-import-your-favorite-libraries-into-ipython-or-a-jupyter-notebook-9c69d89aa343"&gt;How to Automatically Import Your Favorite Libraries into IPython or a Jupyter Notebook&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jupyter</category>
      <category>config</category>
    </item>
    <item>
      <title>Dataframe to Google Sheet</title>
      <dc:creator>Ahmad Abdullah Tariq </dc:creator>
      <pubDate>Fri, 15 Jan 2021 13:43:03 +0000</pubDate>
      <link>https://dev.to/ahmadabdullah247/dataframe-to-google-sheet-553d</link>
      <guid>https://dev.to/ahmadabdullah247/dataframe-to-google-sheet-553d</guid>
      <description>&lt;h2&gt;
  
  
  Setup in Google Console
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a new project in &lt;a href="http://console.developers.google.com"&gt;Google Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You have to enable google services in order to get access to google sheet. Under &lt;strong&gt;API &amp;amp; Services&lt;/strong&gt; Click on &lt;code&gt;Enable API and Services&lt;/code&gt;
&lt;ul&gt;
    &lt;li&gt;Enable &lt;code&gt;Google Drive API&lt;/code&gt;
&lt;/li&gt;
    &lt;li&gt;Enable &lt;code&gt;Google Sheets API&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Now go to &lt;strong&gt;Credentials&lt;/strong&gt; menu and click on &lt;code&gt;Manage service accounts&lt;/code&gt;.

&lt;ul&gt;
    &lt;li&gt;Click on &lt;code&gt;Create Service Account&lt;/code&gt;
&lt;/li&gt;
    &lt;li&gt;Optionally you can also add roles but we are going to skip that since we need to access files for different users.&lt;/li&gt;
     &lt;li&gt;Inside &lt;strong&gt;Service Account Details&lt;/strong&gt; click on &lt;code&gt;Add key&lt;/code&gt;. Download it and store it we will need it later while setting up functionality in jupyter.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup in Jupyter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Place the service key in the root of your jupyter folder. (You can change the path too, but you have to modify it inside the function.)&lt;/li&gt;
&lt;li&gt;To get access of google drive and google sheet we need to install some libraries. Install the following libraries:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    pip install pandas gspread google-api-python-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The following function takes &lt;code&gt;dataframe&lt;/code&gt; and two optional arguments &lt;code&gt;sheet_name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;. &lt;code&gt;sheet_name&lt;/code&gt; will rename the sheet to a given name and &lt;code&gt;email&lt;/code&gt; will add permission to add the created file into the given account's google drvive. Copy the following function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import pandas as pd
    from numpy.random import randint
    import gspread
    from google.oauth2 import service_account
    from googleapiclient.discovery import build

    def df_to_sheet(df, sheet_name, email_address):
        """
        Connects with google drive. Creates a sheet and copy data from dataframe, stores and  create a link.

            :param df: dataframe you want to store.
            :param sheet_name: Name of the sheet you want to save in google drive.
            :param email_address: of the user of google drive.
            :return: returns URL of the file saved 
        """

        scopes = [ "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
        credentials = service_account.Credentials.from_service_account_file("./credentials.json",scopes = scopes)
        permission = { "role": 'writer', "type": 'user', "emailAddress": email_address}

        try:
            # connecting to sheets and google drive
            sheet_service = gspread.authorize(credentials) 
            drive_service = build("drive", "v3", credentials=credentials)

            # creating sheet and copying data in worksheet
            sh = sheet_service.create(sheet_name)
            worksheet = sh.get_worksheet(0)
            worksheet.update([df.columns.values.tolist()]+df.values.tolist())
            drive_service.permissions().create(fileId=sh.id, body=permission).execute()

        except Exception as e: 
            print('Error creating google sheet: ',e)
            return None
        return "https://docs.google.com/spreadsheets/d/%s" % sh.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you can create and store any dataframe you want. &lt;strong&gt;Note:&lt;/strong&gt; make sure there are no null values inside dataframe &lt;code&gt;df.fillna('',inplace=True)&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ## creating a dataframe
    df = pd.DataFrame({'x':randint(5, size=10),'y':randint(5, size=10)})

    ## storing dataframe to google sheet 
    df_to_sheet(df, sheet_name='New Sheet', email_address='ahmadabdullah247@gmail.com')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Repurpose Old Laptop to Host Jupyter Lab</title>
      <dc:creator>Ahmad Abdullah Tariq </dc:creator>
      <pubDate>Sun, 10 Jan 2021 11:03:47 +0000</pubDate>
      <link>https://dev.to/ahmadabdullah247/repurpose-old-laptop-to-host-jupyter-lab-3mia</link>
      <guid>https://dev.to/ahmadabdullah247/repurpose-old-laptop-to-host-jupyter-lab-3mia</guid>
      <description>&lt;p&gt;If you have an old laptop sitting around in your home, instead of giving it away it is a great idea to make it a remote server to practice your development skills. You can use it to deploy the host website (PS: it is advised to use cloud service to productionize. Since it provides 100% uptime and will save you the hassle of securing the server). Neither the less it provides a really great opportunity to learn setting up a remote server. &lt;/p&gt;

&lt;p&gt;Theoretically, you can use any operating system you prefer. In this tutorial, we will be setting up remote access on an Ubuntu OS (version 20.04 precisely). Hosting a Jupyter Lab instance on a remote laptop and accessing it via SSH.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup on Remote Laptop
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open a terminal and make sure everything is up to date on your remote laptop:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    sudo apt-get install update
    sudo apt-get install upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now to enable your laptop as a remote server you need to install ssh server tools via following command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    sudo apt-get install OpenSSL-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you just need to copy the IP address from this machine which we will need to access the remote laptop. You can find that by running the following command.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ip addr  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;(Optional) you can install &lt;strong&gt;htop&lt;/strong&gt; which gives you information about your server resources. &lt;code&gt;free&lt;/code&gt; command gives you the stats about memory.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    sudo apt-get install htop
    free 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Connecting from Laptop
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fire up the terminal and connect via  following command. On entering the following command you'll be prompted to enter the password of your remote laptop. Type your password and hit enter. Wola you are now connected to your remote desktop.
Note: change &lt;code&gt;remote&lt;/code&gt; to your remote laptop name and &lt;code&gt;127.0.0.1&lt;/code&gt; to IP that you copied from the other system.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ssh remote@127.0.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Once you are on your remote desktop you can use it for any application you like. We are going to start a &lt;strong&gt;Jupyter lab&lt;/strong&gt; host. The following command will start jupyter lab on your remote laptop on the default port which is 8888. Copy the URL.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    jupyter lab -no-browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open another terminal and write following command. This will again prompt you to enter the password and if there is no error this means that you have successfully connected to your remote machine.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ssh -N -f -L localhost:8888:localhost:8888 remote@127.0.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now paste the copied URL from the other terminal in your browser and you have jupyter lab connected from your remote laptop. To understand what all the flags in the above command mean you can visit &lt;a href="https://explainshell.com/explain?cmd=ssh++-N+-f+-L"&gt;this&lt;/a&gt; website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt; If you want to operate your remote laptop with the lid down you can take following steps&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     sudo nano /etc/systemd/logind.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uncomment &lt;code&gt;HandleLidSwitch&lt;/code&gt; and change it to 'ignore' if it isn't already. (&lt;code&gt;ctrl+x&lt;/code&gt; to save and exit)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    HandleLidSwitch=ignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to restart the system daemon and you can use your remote laptop with the lid down.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     sudo systemctl restart systemd-logind
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>machinelearning</category>
      <category>jupyter</category>
      <category>ubuntu</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
