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>