Tuesday, 31 March 2020

Error: No module named flask in python

Error: No Module named flask, for python application

(venv) C:\Python\sandbox>py -m flask run
C:\Python\sandbox\venv\Scripts\python.exe: No module named flask

When you try to run the python application using flask you may get the above error: 
root cause: There is no flask installation found in the directory you are currently trying to access the application.
Solution:
When you over come the above error first install flask using below command:
py -m pip install Flask

Then try: py -m flask run 

Now your system will recognize your command and run the flask application.

Sunday, 29 March 2020

Mat Autocomplete list in Angular 7

How to create an Autocomplete list in Angular 7:


Here I am going to show how to get and display an autocomplete list of contents based on the input given by user.

Create a Form with Input and Mat Autocomplete in the .html file:

<form [formGroup]="serviceForm" (ngSubmit)="addService()" data-automation-attribute="form-op-service">
    <mat-grid-list cols="12" rowHeight="65px">
       <mat-grid-tile [colspan]="9">
          <mat-form-field *ngIf="searchType == '1'" class="full-width" appearance="outline">
             <mat-label>Service Name</mat-label>
             <input                  
                    cdk-focus-initial
                    type="text"
                    placeholder="Service Name"
                    matInput
                    [matAutocomplete]="auto"
                    formControlName="serviceName"
                    [(ngModel)]="serviceName"
                    (input)="getserviceOnSearch()"
                    data-automation-attribute="text-service-name" />
                  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="serviceOnSelect($event.option.value)">
                    <mat-option *ngFor="let service of filteredOptions | async"
                      [value]="service.serviceName">
                      {{ service.serviceName }}
                    </mat-option>
                  </mat-autocomplete>
                </mat-form-field>
</mat-grid-tile>
</mat-grid-list>
</Form>

Explanation:

Main code in the input field:
matAutocomplete set as auto.
input: to get the input entered by the user
under mat-option set the filtered option to get the latest list based on the user key.
Once the user enter the key then using ngModel it will be captured in the method getserviceOnSearch(). The functionality of this method is given below in detail.


Add below code into .TS file:

serviceName: string = "";

//This method is used to filter using the user key and return a customized list
searchedText: string = "";
  getserviceOnSearch() {
    let splitText: any;
    if (
      this.serviceName &&
      //this.searchedText != this.serviceName &&
      (this.serviceName.length == 3 ||
        this.serviceName.length == 5 ||
        this.serviceName.length > 7)
    ) {
      this.searchedText = this.serviceName;

      this.http.get<any>('https://api.io/')
        .getResource(
          "billing-masters/unit-wise-services?searchKey=" + this.serviceName
        )
        .subscribe(res => {
          if (res) {
            this.services = res;
            this.filteredOptions = this.serviceControl.valueChanges.pipe(
              startWith(""),
              map(value => this._filter(value))
            );
          }
        });
    } else if (this.serviceName.length <= 2) {
      this.filteredOptions = new Observable<string[]>();
    }
  }

private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();
    return this.services.filter(option =>
      option.serviceName.toLowerCase().includes(filterValue)
    );
  }

The above code is very simple just need to add this method and the list will populate based on the key entered. If you want to populate the complete list during the application start then add the below code in OnInit method to load the complete API response into the list.

ngOnInit() {
this.http.get<any>('https://api.io/')
        .getResource(
          "billing-masters/unit-wise-services?searchKey=" + this.serviceName=""
        )
        .subscribe(res => {
          if (res) {
            this.services = res;
            this.filteredOptions.next(this.services.slice());
          }
        });
    } else if (this.serviceName.length <= 2) {
      this.filteredOptions = new Observable<string[]>();
    }
}

The above code will add the complete response into the list which will show user all the options that are available and once the key is entered it will filter the options and refresh the list based on the key.

Cannot read property 'nativeElement' of undefined angular 7

This error is one of the common error which we get when there is a mismatch in the typescript and html in Angular 7.

To avoid this error kindly notice the below code for reference

In example.component.ts        
@ViewChild("nameField")nameField:ElementRef;

In example.component.html
<input                      
             cdk-focus-initial 
             #nameField
             type="text" 
             placeholder="Service Name" 
             matInput
             [matAutocomplete]="auto" 
             formControlName="serviceName" 
             [(ngModel)]="serviceName"
             (input)="getserviceOnSearch()" 
  data-automation-attribute="text-service-name" />

COVID-19 - Information about corona, symptoms and how it spread

About COVID-19:

COVID-19 as Corona is a virus which now can attack humans. this virus has been infecting animals for over years which have now evolved or have been evolved to infect humans. This virus is also like similar to HIV which start attacking the immunity system first and once it has weaken the system then spread to lungs and give server damage to lunch. This is a deadly virus and a well developed human will get infected and will be developing in the host for over 1 week to 14 days. During these period your body before start recognizing the virus will get developed and it will start spreading to your lungs.

Symptoms:

During the first attack of this virus, you will get sore throat (server). And this is the first symptom which is also common when we get cold.
1. Tiredness
2. Sore throat and irritation
3. Cough
4. Fever
5. difficult in breathing (once it attack your lungs)

How it spread?

The virus can spread through below process:
1. Hand to hand contact
2. Victim sneeze
3. Hope the virus can be alive for 8 days outside
4. Even if there is a heavy moist in air the virus can be alive and spread through air

How to avoid this?

Currently we have to stay in closed place, not to roam in open space also avoid going to places where the Corona positive victims have been spotted.

Sunday, 7 May 2017

Java program to Merge two Excel sheets

This program has been taken from the below link and enhanced for easy use and easy understanding. This program is working 100% and the result is accurate.
Jars used: poi.jar

https://coderanch.com/t/614715/java/merge-excel-files - Thanks for the program

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;


@SuppressWarnings("deprecation")
public class MSExcel {
 public static void main(String args[]) throws FileNotFoundException{
  MSExcel mslobj = new MSExcel();
  mslobj.readExcel();
 }

 private void readExcel() {
  // TODO Auto-generated method stub
  try {
   String FilePath = "C:\\Users\\test.xls";
   String FilePath1 = "C:\\Users\\test1.xls";
   FileInputStream fis = new FileInputStream(FilePath);
   FileInputStream fis1 = new FileInputStream(FilePath1);
   HSSFWorkbook workbook = new HSSFWorkbook(fis);
   HSSFSheet sheet = workbook.getSheet(“sheet”);
 
   HSSFWorkbook wb = new HSSFWorkbook(fis1);
   //Workbook wb = new XSSFWorkbook();
   HSSFSheet sheet1 = wb.getSheet(“sheet1”);
   copySheets(sheet1, sheet);
 
   FileOutputStream outfile = new FileOutputStream("C:\\Users\\test1.xls");
   wb.write(outfile);
   outfile.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
  public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet){
    copySheets(newSheet, sheet, true);
  }
     
       public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle){
           int maxColumnNum = 0;
           Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
           for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
               HSSFRow srcRow = sheet.getRow(i);
               HSSFRow destRow = newSheet.createRow(i);
               if (srcRow != null) {
                   MSExcel.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
                   if (srcRow.getLastCellNum() > maxColumnNum) {
                       maxColumnNum = srcRow.getLastCellNum();
                   }
               }
           }
           for (int i = 0; i <= maxColumnNum; i++) {
               newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
           }
       }
 
       public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, Map<Integer, HSSFCellStyle> styleMap) {
           @SuppressWarnings("deprecation")
     Set<CellRangeAddress> mergedRegions = new TreeSet<CellRangeAddress>();
           destRow.setHeight(srcRow.getHeight());
           for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
               HSSFCell oldCell = srcRow.getCell(j);
               HSSFCell newCell = destRow.getCell(j);
               if (oldCell != null) {
                   if (newCell == null) {
                       newCell = destRow.createCell(j);
                   }
                   copyCell(oldCell, newCell, styleMap);
                   CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(), (short)oldCell.getColumnIndex());
                   if (mergedRegion != null) {
                       CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn());
                       if (isNewMergedRegion(newMergedRegion, mergedRegions)) {
                           mergedRegions.add(newMergedRegion);
                           destSheet.addMergedRegion(newMergedRegion);
                       }
                   }
               }
           }
         
       }
     
       public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
           if(styleMap != null) {
               if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){
                   newCell.setCellStyle(oldCell.getCellStyle());
               } else{
                   int stHashCode = oldCell.getCellStyle().hashCode();
                   HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
                   if(newCellStyle == null){
                       newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                       newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                       styleMap.put(stHashCode, newCellStyle);
                   }
                   newCell.setCellStyle(newCellStyle);
               }
           }
           switch(oldCell.getCellType()) {
               case HSSFCell.CELL_TYPE_STRING:
                   newCell.setCellValue(oldCell.getStringCellValue());
                   break;
               case HSSFCell.CELL_TYPE_NUMERIC:
                   newCell.setCellValue(oldCell.getNumericCellValue());
                   break;
               case HSSFCell.CELL_TYPE_BLANK:
                   newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                   break;
               case HSSFCell.CELL_TYPE_BOOLEAN:
                   newCell.setCellValue(oldCell.getBooleanCellValue());
                   break;
               case HSSFCell.CELL_TYPE_ERROR:
                   newCell.setCellErrorValue(oldCell.getErrorCellValue());
                   break;
               case HSSFCell.CELL_TYPE_FORMULA:
                   newCell.setCellFormula(oldCell.getCellFormula());
                   break;
               default:
                   break;
           }
         
       }
     
       public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {
           for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
               CellRangeAddress merged = (CellRangeAddress) sheet.getMergedRegion(i);
               if (merged.isInRange(rowNum, cellNum)) {
                   return merged;
               }
           }
           return null;
       }
 
       private static boolean isNewMergedRegion(CellRangeAddress newMergedRegion, Collection<CellRangeAddress> mergedRegions) {
           return !mergedRegions.contains(newMergedRegion);
       }
     
 }

Saturday, 1 April 2017

Simple Java program to find Prime number

This program has made simple for better understanding of beginners.

import java.util.Scanner;


public class PrimeNumber {
public static void main(String args[]){
System.out.println("Enter the number: ");
//To receive input from users
Scanner s = new Scanner(System.in);
int num = s.nextInt();
s.close();
//Logic to detect the prime number
for(int i=2; i<num; i++){
int temp = num%i;
if (temp == 0){
System.out.println("This is not a prime number.");
break;
else{
if (i == (num-1)){
System.out.println("This is a prime number.");
}
}
}


}

Output:
Enter the number: 
21
This is not a prime number.

Enter the number: 
3
This is a prime number.

Simple Java program for Odd or Even

This program has been simplified for beginners to find the given number is Odd or Even.

import java.util.Scanner;


public class OddrEven {
public static void main(String args[]){
//To get the input number from user
System.out.println("Enter the number: ");
Scanner sc = new Scanner(System.in);
int inp = sc.nextInt();
sc.close();
//Simple logic to check the number is Odd or Even
int temp = inp % 2;
if (temp == 0){
System.out.println("This is a even number.");
}else{
System.out.println("This is a odd number.");
}
}

}

Output:
Enter the number: 
45
This is a odd number.

Enter the number: 
22
This is a even number.

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...