Monday, April 18, 2022

Matploblib does not show interactive plots on SciView in PyCharm

I encounter a problem when I plot an interactive plot using Matplotlib in PyCharm. The problem is that it does not show the interactive plot on SciView as it usually does. Although it could be due to the idiomatic way of  my creating python plot, I look for a "cheap" way to fix this since the code successfully creates interactive plots when I run it fron the console outside of PyCharm. The solution tends out to be use a different Matploblib backend. 

In PyCharm's python console, we can determine which Matploblib backend is currently using, e.g.,


>>> import matplotlib
>>> matplotlib.get_backend()
'module://backend_interagg'

>>>

In an OS console, we can determine which Matploblib backend is currently using, e.g.,


>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'

>>>

Based on the observations in the above, the solution is to set the Matplotlib backend as the one used by Matplotlib on the OS console, e.g.,



import matplotlib


if __name__ == '__main__':
    matplotlib.use('TkAgg')
    # ...
    # more code ...
    # ...



Saturday, April 16, 2022

Setting up X2Go

If you wish to have Windows Remote Desktop-like experience when we access a Linux hosts, X2GO is probably the best solution. X2GO runs on the NX or NoMachine protocol. Contrast to the solution that runs a X server on a Linux host, and run a client application on another host, we can leave a session and resume it with NX or X2GO, which offers the experience similar to Windows Remote Desktop. 

Below is a "minimal" setup for a Fedora Linux server and a Windows client. We call it a "minimal" setup because we do not install a full Linux desktop, rather, we install a graphical terminal like xterm. 

  1. Install the X2GO server on Fedora Linux
    
    sudo dnf install x2goserver x2goserver-xsessions xterm
        
  2. Install the X2GO client on Windows. For this, we download the client installation package, and run the installation package.
  3. Now we can use X2GO -- nothing needs to be done on the Linux server
    1. Start the X2GO client on the Windows host
    2. Create a new sessiono if we have not done so
    3. When creating the new session, we specify the Linux host we connect to and the user name we log on
    4. It is important to note that we should select the "Session Type" as "Single Application" and enter the command as "/usr/bin/xterm" since we did not install a full Linux Deskop and only installed xterm
    5. To connect to the Linux server and run xterm, we open the session.
    6. We can close the session. Next time when we open it, it resumes from where we close it

Friday, April 8, 2022

SSH cannot connect to any non-local SSH servers on WSL 2

Recently, I encountered a strange problem. That is, I cannot connect to any non-local SSH servers from a WSL 2 terminal using the ssh client from WSL 2. However, I would not have any problem to connect to these SSH servers using ssh.exe from Windows and PuTTY or other SSH clients on Windows. Here is what I observe when I try to connect Github.com,


ssh -vvv -T ssh@github.com
OpenSSH_7.9p1 Debian-10+deb10u2, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /home/hui/.ssh/config
debug1: /home/hui/.ssh/config line 17: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "github.com" port 22
debug2: ssh_connect_direct
debug1: Connecting to github.com [140.82.113.3] port 22.

The process hangs on "Connecting to ..." until times out. I attempted all of the methods discussed in this threads and the method of adding a Firewall rule for WSL, but to no avail in my case. The solution finally works for me is to restart the Windows Container Networking service.

Using PowerShell, we can restart the service on command line as follows,


PS C:\WINDOWS\system32> Restart-Service -Force -Name hns

The solution came to me because I wanted to restart the networking stack of WSL and to see if it would resolve this issue. The method to do it comes from this Github gist. To restart the networking stack of WSL, run the following commands,


# Restart the Host Network Service 
Restart-Service -Force -Name hns
# Restart the Windows Subsystem for Linux Manager
Restart-Service LxssManager
# Restart the WSL Network adapter
Restart-NetAdapter -Name "vEthernet (WSL)"

In my case, restarting the Windows Container Networking service (hns) is sufficient for me to resolve the problem.

Reference

  1. Reset your WSL network connection trying to fix WSL2 media disconnected error
  2. WSL2 SSH can't connect to any public SSH server
  3. SSH connections hanging from WSL2

Thursday, April 7, 2022

Failed to start Android Studio on Windows

Android Studio refuses to start due to the error,


Internal error. ...

java.util.concurrent.CompletionExecution: java.net.BindExcetion: Address already in use: bind
...

Apparently, Windows sometimes does release TCP ports completely for some reason, and the solution is to restart the winnat service. 

  1. Open Windows Command Prompt as an Administrator 
  2. Run the following to stop and restart winnat service
    net stop winnat
    net start winnat
    

Alternatively, open PowerShell as an Administrator, and run


Restart-Service -Name winnat

Thursday, March 24, 2022

Stopping Android Emulator From Command Line

To stop all Android emulator instance, we do,


adb emu kill

If we want to kill a specific Android emulator instance, we need to find out the device's serial number or identifier. For this, we do,


adb devices

The typical emulator serial number or identifier is emulator-5554. Using this as an example, we can kill the emulator instance using the command below,


adb -s emulator-5554 emu kill

Tuesday, March 15, 2022

Reinstalling disappeared ddclient

I upgraded a CentOS 8 host to CentOS Stream 8. Later I discovered the ddclient package disappeared. I attempted to install it, and this error occurred. 

$ sudo dnf install ddclient
 Problem: conflicting requests
  - nothing provides perl(Digest::SHA1) needed by ddclient-3.9.1-1.el8.noarch
(try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
$

A research on the web tells me that I need the PowerTools package. To find out the exact name of the package, 




$ dnf repolist all | grep -i tool
powertools                               CentOS Stream 8 - PowerTools   enabled
powertools-source                        CentOS Stream 8 - PowerTools - disabled
$

Following this, I enable the repo



$ sudo dnf install ddclient
CentOS Stream 8 - PowerTools                    2.5 MB/s | 4.2 MB     00:01
......
......
Complete!
$

Tuesday, February 1, 2022

Purging a directory or a list of files from a Git repository

Sometimes we want to remove a directory or a file at a given path completely from a Git repository's history. BFG Repo-Cleaner is a recommended tool for this. Typical examples examples given are to remove files matching a name or a pattern anywhere in the repository. Sometimes this is not what we wanted, for instance, we want to remove a Readme.md in a particular directory, or remove a doc directory from a repository that has multiple files or directories of those names instead.

Below we show an example where we remove a file or a directory at a particular path. This relies on the following option of BFG Repo-Cleaner,

-bi, --strip-blobs-with-ids <blob-ids-file>  
           strip blobs with the specified Git object ids

where it is noteworthy that the argument given to this option is a file. The file contains Git object hashes of the files we wish to purge. 

In addition, to purge a directory is to purge all the files in the directory; once all of the files are gone, the directory is gone; however, BFG Repo-Cleaner would yield an error if we pass the Git object hash of a directory to it. 

The question becomes how we figure out the Git object hashes of the files we wish to purge. For this, we can use git rev-list command, e.g.,

git rev-list --all --objects | \
      grep "src/Readme.md" | \
      cut -d' ' -f1 > file_hashes.txt

Following this,

bfg -bi file_hashes.txt 

git push --force