Thursday, September 29, 2016

Python Connect Database Using peewee


First install peewee using pip

pip3 install peewee
Now we have to install MySQLdb or PyMySQL

To install PyMySQL run following command:
sudo pip3 install PyMySQL
Now our basic Configuration done.
Lets create a sample program to connect database and retrieve information from an existing table
import peewee
from peewee import *
db = MySQLDatabase('Word', user='root' , passwd='csecu')


class Words(peewee.Model):
    name = peewee.CharField()
    engdes = peewee.CharField()
    pages = peewee.IntegerField()

    class Meta:
        database = db


for words in Words.filter(name="bappu"):
    print(words.pages)
Lets breakdown :

db = MySQLDatabase('Word', user='root' , passwd='csecu')
This line is used to establish a database connection.

class Words(peewee.Model):
    name = peewee.CharField()
    engdes = peewee.CharField()
    pages = peewee.IntegerField()
Here class Words, represents that, we have a table name words in Word database and it has 3 columns. Which are name, engdes and pages 

class Meta:
        database = db

This means that Words model uses the "Word" database.
for words in Words.filter(name="bappu"):
    print(words.pages)
This is used to select all data where name = bappu and showing number of pages

Thursday, September 1, 2016

Up & Running With Gulp

Check Older version if installed

$ npm rm --global gulp
  Install Gulp Globally
$ npm install --global gulp-cli
Initializing Project directory
npm init
Installing Dependencies
npm install --save-dev gulp
Creatng gulpfile.js in root directory
var gulp = require('gulp');

gulp.task('default', function() {
  // place code for your default task here
});
Preprocessing with Gulp 

Compile Sass to CSS with the help of gulp-sass PKG

$ npm install gulp-sass --save-dev

 --save-dev flag to ensure that gulp-sass gets added to devDependencies in package.json Now add following line to gulpfile.js
var sass = require('gulp-sass');
  Now add following line in gulpfile.js
gulp.task('sass', function(){
    return gulp.src('css/sass/shuvrow.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
Now add watch to change dynamically.
 Add following line on gulofile.js
gulp.task('watch', function(){
    gulp.watch('css/sass/shuvrow.scss', ['sass']);
    // Other watchers
})
Now run following command in terminal
gulp watch
Now you can see shuvrow.css file changes automatically.
Live Reloading => Auto sync 
Run Following command:

npm install browser-sync --save-dev
Add following line on gulpfile.js
var browserSync = require('browser-sync').create();
Now add following lines
gulp.task('browserSync', function() {
  browserSync.init({
    server: {
      baseDir: ''
    },
  })
})
Now update following code
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
to
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
        .pipe(browserSync.reload({
            stream: true
        }))
});
Now, we have to run both the watch and browserSync tasks at the same time for live-reloading
To do this update following codes
gulp.task('watch',function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
to
gulp.task('watch',['browserSync','sass'], function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
Now run gulp watch command on terminal to see all new changes on browser automatically.