Friday, September 13, 2013

Using Python to Create a Small Dynamic Blog

A example of a small dynamic blog.

Small Code example of a small dynamic blog that uses MySQL. Much more could be
added, but here are the smaller working pieces.
*note, posting it kills the python indentation which will prevent it from working, you can download each piece from here Blog Files
_________________________________________________________________________________
For this to work, you will need a mysql database with a table for each element. One for the template, one for your css, another for the body context, or your blog itself with an addition column for the date field. This makes a request to each and pulls the most current request. This allows you to update and rollback each element as you insert a row, or delete a row if you don't wish to keep your changes.

The Template

<!DOCTYPE html> 
<html> 
<head> 
<title> </title>
     {S} 
</head> 
      <body>
       <h1> My Blog </h1> 
       <h3> <nav> <div class="lx"> </div> 
          <div class="lx"> </div> < div class="lx">  <a href="home"> Home </a> </div> 
           <div class="lx"> <a href="about"> About </a> </div> <div class="lx"> </div> 
            <div class="lx"> </div> </nav> </h3> 
            <div class="top"> </div> <div class="left"> {L} </div> <div class="right"> {R} </div> 
            <br> <br> <br> {B} 
        </body> 
</html>

To insert this into your MySQL table.

INSERT INTO template (template, date)
VALUES
('<!DOCTYPE html> <html> <head> <title> </title> {S} </head> <body> <h1> Cogs and Wrenches </h1> <h3> <nav> <div class="lx"> </div> <div class="lx"> </div> <div class="lx"> <a href="home"> Home </a> </div> <div class="lx"> <a href="about"> About </a> </div> <div class="lx"> </div> <div class="lx"> </div> </nav> </h3> <div class="top"> </div> <div class="left"> {L} </div> <div class="right"> {R} </div> <br> <br> <br> {B} </body> </html>', now());
----------------------------------------------------------------------------------------------------------------------------------
The CSS
<style> 
body { background-color: #d0e4fe; text-align:center;} 
h1 { color: #5F9EA0; text-align:center;} 
h2 { color: #8FBC8F; text-align:center;} 
h3 {background-color: #5F9EA0;}
p {font-family: "Times New Roman", Times, serif;}
div.lx { display:inline; width:240px; padding:10px; border:2px solid #6495ED; margin:1px; background: #8FBC8F; border-radius: 15px;}
div.lr {width:200px; padding:10px; border:5px solid #6495ED; margin:1px; background: #8FBC8F; border-radius: 15px;}
div.left {width:240px; margin:0px; background: #5F9EA0; float:left; height:800px;}
div.right {width:240px; margin:0px; background: #5F9EA0;float:right; height:800px;}
</style>

To insert this into your MySQL database use

insert into style (style, date) values 
('<style> body { background-color: #d0e4fe; text-align:center;} 
h1 { color: #5F9EA0; text-align:center;} 
h2 { color: #8FBC8F; text-align:center;} 
h3 {background-color: #5F9EA0;}
p {font-family: "Times New Roman", Times, serif;}
div.lx { display:inline; width:240px; padding:10px; border:2px solid #6495ED; margin:1px; background: #8FBC8F; border-radius: 15px;}
div.lr {width:200px; padding:10px; border:5px solid #6495ED; margin:1px; background: #8FBC8F; border-radius: 15px;}
div.left {width:240px; margin:0px; background: #5F9EA0; float:left; height:800px;}
div.right {width:240px; margin:0px; background: #5F9EA0;float:right; height:800px;}
</style>', now());
____________________________________________________________________________

Now  create a table for your blog with the columns title, body, date. So that you have some thing to look at. 

INSERT INTO blog (title, body, date)
VALUES
("My New Blog", "Some neat things happened", now());

________________________________________________________________________________
The Code
This is were the magic happens, this is where it's all put together and served up for the browser.
Be sure you have python 2.7, and the bottle framework installed. You can get these from.

The code, on a windows mac, or linux box, just save this as blog.py, and for mac and linux just type in "blog.py" for windows if you have python installed just type in "blog.py"


#! /usr/bin/env python
###########################################
#Program:Blog
#Date Created: 9-9-2013
#Author: Daniel Frost
###########################################
import bottle
from bottle import *
import MySQLdb


def styles():
db = MySQLdb.connect(host="ip address" ,user="username",passwd="password",db="blog")
c = db.cursor()
slq_inject = "SELECT style from style ORDER by date DESC limit 1;"
c.execute(slq_inject)
db.store_result()
db.use_result()
result = c.fetchall()
styles = '''
%s
'''% result[0]
return styles

def body():
db = MySQLdb.connect(host="ip address" ,user="username",passwd="password",db="blog")
c = db.cursor()
slq_inject = " SELECT title, date ,blog from body ORDER by date DESC limit 3;"
c.execute(slq_inject)
db.store_result()
db.use_result()
result = c.fetchall()
body = ''
for i in xrange(len(result)):
time = result[i][1]
title = "<h2>%s   %s</h2>"% (result[i][0],time[:10])
body_temp = '''
%s
<p>%s</p>
'''% (title, result[i][2])
body = body + body_temp
return body

def about():
db = MySQLdb.connect(host="ip address" ,user="username",passwd="password",db="blog")
c = db.cursor()
slq_inject = " SELECT about from about ORDER by date DESC limit 1;"
c.execute(slq_inject)
db.store_result()
db.use_result()
result = c.fetchall()
title = "<h2>About Me</h2>"
body = '''
%s
<p>%s</p>
'''% (title, result[0][0])
return body

def template(*arg, **kwarg):
''' This joins the items fetched from the style() and body() methods'''
db = MySQLdb.connect(host="ip address" ,user="username",passwd="password",db="blog")
c = db.cursor()
slq_inject = "SELECT template from template ORDER by date DESC limit 1;"
c.execute(slq_inject)
db.store_result()
db.use_result()
result = c.fetchall()
styles = arg[0]
body = arg[1]
parse = '%s'% (result[0])
template = parse.split(' ')
for i in xrange(len(template)):
if template[i] == '{S}':
template[i] = styles
if template[i] == '{B}':
template[i] = body
output = ''
for i in template:
output = output + i + ' '
return output

@bottle.route('/home')
def home():
p_style ='%s'% styles()
p_body = body()
page = template(p_style,p_body)
return page

@bottle.route('/about/me')
def about_me():
p_style ='%s'% styles()
p_about = about()
page = template(p_style,p_about)
return page

bottle.run(host='127.0.0.1', port=8080)