Sunday 19 February 2017

Simple Shell script to install and run your flask application for first time.


Simple shell script to clone your flask app , installing requirements, activating virtual environment and running the application.
Fork me on GitHub



#!/bin/ksh

CURRENT_DIR=`pwd`
echo "please enter your repo link" #input your repo link
read input_variable  #Read input
echo $input_variable
REPOURL=$input_variable
#REPOURL=$input_variable
gitdir=`basename $input_variable .git`

if  [[ ! -d $CURRENT_DIR/$gitdir ]]
then 
git clone $REPOURL 
elif [[ ! -d $CURRENT_DIR/$gitdir/.git ]]
then
mv CURRENT_DIR/$gitdir CURRENT_DIR/$gitdir"_backup" 
git clone $REPOURL
fi
cd $CURRENT_DIR/$gitdir
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
python app.py

Python Simple Flask Redis Tutorial

A simple Flask Redis Tutorial

Install requirements : flask, redis

  • Install virtual environment:  sudo apt-get install python3-venv
  • Creating the python virtual environment:  python3 -m venv venv
  • Add the root of the repository to the python path upon virtual environment activation: echo "$(pwd)/src" > redis-venv/lib/python3.5/site-packages/MY_VENV_PYTHONPATH.pth
  • Activate your python virtual environment:  . venv/bin/activate  Note: To remove the virtual environment from your shell: deactivate
  • Installing required python modules from newly created virtual environment:  Activate your python virtual environment if not already active.  pip install -r requirement.txt
  • Run the app  python app.py

Flask Redis sample application Git Source Code

from flask import Flask, jsonify
import redis
import json

app = Flask("redis")

@app.route("/")
def index():    
    #value to save in redis 
    value_to_set = "shiva"
    #set value to key
    redis_db.set('name', value_to_set)   
    return json.dumps(redis_db.keys())



if __name__ == "__main__":
    redis_db = redis.StrictRedis(host="localhost", port=6379, db=0)
    app.run(port=15420)

PHP Form submit without java script and submit button

Submit a form without submit button and  javascript submit

We mostly prefer php form submit either using javascript or normal submit.

But we can submit a form without submit button and  javascript

have a look at the below code



<?php

if(isset($_POST['submit_x']) && isset($_POST['submit_x']))
{
      echo $_POST['first_name'];
      echo $_POST['last_name'];

      // Save logic

}
?>


<form method="post">
<lable>First Name</lable>
<input type="text" name="first_name" >

<lable>Last Name</lable>
<input type="text" name="last_name" >

<input type="image" name="submit"/>
</form>

Saturday 10 January 2015

Php for each with comma separator except last


Sometimes we write a logic to show comma separated data. But the challenge is not to append comma to the last value.
We can do this by using next() function:
 
<?php
$arr = array('tvs','hero','honda','bajaj');
$copy = $arr;
foreach ($arr as $val) {
    echo $val;
    if (next($copy )) {
        echo ','; // Add comma for all elements instead of last
    }
}
?>

Friday 7 November 2014

How to read image Using Jquery & Html5

How to read image  using jquery & html5

File reader API is used to read a file from your local hard drive

We can find Atricle  about File reader


DEMO



 HTML PART :

<input id="imageread" type="file" />
<canvas id="img"></canvas>


Jquery Code :

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">  $('input').change(function() {     var readimg = new FileReader;     readimg.onload = function() {         var img = new Image;         img.onload = function() {             var c=document.getElementById("img");             var ctx=c.getContext("2d");             ctx.drawImage(img,0,0,200,180);                 }         img.src = readimg.result;                 };      readimg.readAsDataURL(this.files[0]);    }); </script>

Monday 3 November 2014

Add Extra Field Like Google Plus

Jquery code to add extra field using jquery.




Demo




 HTML PART :

<div class='extdiv'>
<input type="text" class="addmore"  />
</div>



JQUERY CODE :

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script>
$(document).on("keyup",".extdiv .addmore",function(){
    var dynamic = $(this).attr("class");
    var countoftext = $("."+dynamic).length;  // count of entered value
    var nextcount = $(this).parent(".extdiv").nextAll(".extdiv").length; // finding next class count
    if(this.value.length >= 1 && nextcount == 0 )  // if length of textbox values greate than or equal to one and nextdiv class count is equal to zero
    {
        $(this).after("<button>remove</button>"); // appending remove button to that field
        var extrabox = "<div class='extdiv'><input type='text' class= 'addmore' /></div>" // creating required field
        $(this).parent(".extdiv").after(extrabox); // appending
    }
    });
    $(document).on("click","button",function(){
    $(this).parent("div").remove();

    });
  

</script>


Monday 21 July 2014

Search By click

Another jQuery filter by click event Usage: Here my requirement is i need to search users by using their category(ex: music director and singer) on click
As of now i don't have demo space..
Simply take a copy of this code and paste in a notepad then save as html open in browser and check
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Simple and Small Jquery Filter</title> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <ul  style="float:left">   <li>     <div class="name">Devi sri prasad</div>     <div class="ltype">Music director</div>   </li>   <li>     <div class="name">Mani Sharma</div>     <div class="ltype">Music director</div>   </li>   <li>     <div class="name">anoop rubens</div>     <div class="ltype">Music director</div>   </li>   <li>     <div class="name">yesu dasu</div>     <div class="ltype">singer</div>   </li>   <li>     <div class="name">S.P.Balu</div>     <div class="ltype">singer</div>   </li>   <li>     <div class="name">shankar mahadevan</div>     <div class="ltype">singer</div>   </li>   <li>     <div class="name">Shreya Ghoshal</div>     <div class="ltype">singer</div>   </li>   <br> </ul> <div style="float:left"><a href="javascript:void(0);">singer</a> <br> <a href="javascript:void(0);">Music director</a> </div> <script> $(document).on('click','a',function(){ var searchval = $(this).text(); $("ul li .ltype:contains("+searchval+")").parent('li').css("display","block"); $("ul li .ltype:not(:contains("+searchval+"))").parent('li').css("display","none"); }); </script> </body> </html>