Category Archives: How to

Display random projects using short code block in WordPress


We have seen how to use short code to play with portfolio projects, I have few more posts in this category.

How about a random wise project list , in a page or post, which changes the order each time load. A customization of short code will make it possible

portfolio showposts=3 orderby=rand

You can specify number columns using columns parameter and number of posts using showposts parameter

  • display_types: display Project Types – displayed by default. (true/false)
  • display_tags: display Project Tag – displayed by default. (true/false)
  • display_content: display project content – displayed by default. (true/false)
  • display_author: display project author name – hidden by default. (true/false)
  • include_type: display specific Project Types. Defaults to all. (comma-separated list of Project Type slugs)
  • include_tag: display specific Project Tags. Defaults to all. (comma-separated list of Project Tag slugs)
  • columns: number of columns in shortcode. Defaults to 2. (number, 1-6)
  • showposts: number of projects to display. Defaults to all. (number)
  • order: display projects in ascending or descending order. Defaults to ASC for sorting in ascending order, but you can reverse the order by using DESC to display projects in descending order instead. (ASC/DESC)
  • orderby: sort projects by different criteria, including author name, project title, and even rand to display in a random order. Defaults to sorting by date. (author, date, title, rand)

How to add to post/page

  • Add a short code block
  • Add the customized portfolio code [Above]
  • Preview and Publish changes

More on WordPress and Short Code

Display specific type of portfolios using Short Code in WordPress


How to display only specific portfolio projects in a page/posts using short code block in your WordPress website ?

Portfolio short code can be customized. Following are the list of attributed you can use

  • display_types: display Project Types – displayed by default. (true/false)
  • display_tags: display Project Tag – displayed by default. (true/false)
  • display_content: display project content – displayed by default. (true/false)
  • display_author: display project author name – hidden by default. (true/false)
  • include_type: display specific Project Types. Defaults to all. (comma-separated list of Project Type slugs)
  • include_tag: display specific Project Tags. Defaults to all. (comma-separated list of Project Tag slugs)
  • columns: number of columns in shortcode. Defaults to 2. (number, 1-6)
  • showposts: number of projects to display. Defaults to all. (number)
  • order: display projects in ascending or descending order. Defaults to ASC for sorting in ascending order, but you can reverse the order by using DESC to display projects in descending order instead. (ASC/DESC)
  • orderby: sort projects by different criteria, including author name, project title, and even rand to display in a random order. Defaults to sorting by date. (author, date, title, rand)

Display only specific post types

You can specify post types as include_type and it will look like

portfolio include_type="Flutter"

You can also hide showing display type by using display_types=false false

Organize Portfolio projects in WordPress as Page


Portfolios area great ways to show case your projects in a blog. You can include them into separate menu [Projects].

How about organize the whole projects into a static page. Every time you add a project, it should update automatically, sound good ,isn’t it?

  • Create Static Page whatever block you want to use, go head. Have a clear design and description of your page with skills set.
  • Add a Short Code block after the details section and add the following short code into the block

portfolio

The page will be automatically added to the Menu section or you can add the page menu using Customize design option from your admin menu.

Now every time you add a project it will added to the portfolio project page by short code.

Here is example of portfolio page , my projects

Create a pelican blog in 5 minute


Pelican is a static website generator written in pure python. It is an opensource project and it is being used to create personal website to project webpages. So as you think a blog is dynamic with new content, how do a static generator fit for the job ?

Static generator is generating html pages every time you generate site with content, so your site can be dynamic for some extent.

Create pelican blog locally

First you need to configure Pelican blog locally, then you can publish them to webserver/GitHub Page.

Requirements

  • Python
  • Pelican package
  • Editor, Visual Studio Code/sublimText recommended.

You can install pelican with markdown support from Pypi repository.

python -m pip install "pelican[markdown]"

Create blog project

Let’s get started Create a folder for your project and step into it using CD, go it console and let pelican create a Skelton structure of your site by asking a series of questions.

mkdir -p ~/projects/yoursite
cd ~/projects/yoursite
pelican-quickstart

In your project folder you can see a site folder and inside it reach config.py which is the settings for your pelican site. Change if you need anything.

Posts and Pages

Under the content folder your can create post as markdown file and for pages create folder Pages and create About page using markdown. Your markdown can be composed with following format

Title: My First Review
Date: 2010-12-03 10:20
Category: Review

Following is a review of my favorite mechanical keyboard.

Generating content and view your site

Create few posts and pages and hit pelican content on the terminal will generate the html files for your site. Using pelican –listen command will make your site live with localhost:8000. To terminate press Ctrl+C

Hosting the site to Github Page

First up all create a repository , name should match <username.github.io> / <organization>.github.io and add the repo to your site folder. The master branch will be used to publish your page. Check your repository for page setting.

Update content

Well for updating content, you need to regenerate the content and Add,commit and push it back to the GitHub Repository and it will go live in seconds. See a demo

Find a way to extent the visual appearance and functions of your blog by using Themes and Plugins. Visit the GitHub Repo of Pelican Project

For detailed documentation visit Official Quicks tart Guide

Return table row as string in SQL Server


You can return table rows as single row using a COALESCE trick. This can be possible by appending each row value to a variable.

The SQL Coalesce are used to handle NULL values. During the expression evaluation process the NULL values are replaced with the user-defined value.

declare @ret varchar(4000)
select @ret=coalesce(@ret+', ','') + l_name from accounts
print(@ret)

Here in my SQL script, created a variable and append all row values in the column of l_name to sql vaiable @ret and then I print the result

Even though the script can return lengthy string, it may depends on the number of rows and the capacity of varchar variable declared.

How to pass python list to JavaScript in Flask app


You can’t use Python list directly into JavaScript, JavaScript lislt/array is not compatible with Python or wise versa. So what to do.

Solution

I have a list of names, which I want pass to a JavaScript in a Flask ninja template. Ninja template allow use to safely convert the python list to Json string then we can pass it to the JavaScript function / just JavaScript

<script type="text/javascript">

        dn = {{ distabr|tojson }}
</script>

You can’t use same route variable in multiple places in same template, it may not serve the purpose

In JavaScript Functions

Suppose you have separate JavaScript function files for some functionality like drawing analytic plots and pie diagram, or some donut chart. So how do we pass value to those functions ?

You can create a JavaScript as above, in your current template, then in function create a script variable to use it as follows.

function init_echarts() {
    var distNames=dn;
}

That’s all you need to know to embed some morris diagram to your flask app.

You should look at Morris JS , it is free and easy to use JavaScript charts library.

Pass variables to other templates in Flask


Flask is one of my Python frame work in which I build simple web application with ease and peace. Today we learn how to pass a variables to other templates in a Flask application.

Suppose your web app have a base template and a top nav bar template which included using

{% include "site_template/top_navigation.html" %}

and you want to pass a title or something else from a Index page to navigation bar where you want show up some text.

Using {% set %} you can create global variables.

The solution

We can use a set block in top of the index page as follows ,to pass variables to other sub templates

{% set mytitle=title %}

In the route it will look like

return render_template('index.html',  title=title)

and in the nav bar page we can use as mytitle as variable using

<div id="site_title"><h2  >{{ mytitle}} </h2></div>

If you had doubt and suggestion please leave a comment

Python Library Tips: timedelta


Timedelta

Timedelta class represent not any particular date or time, but it represents a span of time. This class will be helpful when you need to run some math on date and time. Say what will be this day on after few weeks or what will be this day in future year.

Following example will make it clear

from datetime import date
from datetime import timedelta

print(f'todays date {date.today()} after 1 year will be {date.today()+ timedelta(days=365)}')

Output 
todays date 2020-06-27 after 1 year will be 2021-06-27

Here I used the f.string to construct the message , the timedelta represent one year, 365 days. You can find what will be the date before few weeks back using date -timedelta(weeks=4)

Python Library Tips: strip off letter from list


f.strings

In this series of posts , learn how to use Python Library functions

How to strip off no of letters from a list of strings

The split function of string class can be used to split words from a string. do you know how to creating a abbreviated set of strings from a list. I

With a comprehension statement in Python this can’t be hard

list=['PYTHON','CPYTHON','IPYTHON']
stripped=[ k[:3] for k in list]
print(stripped)
['PYT', 'CPY', 'IPY']

Here we strip of the each list element using : operator in the list and specify the number of characters want to keep.

You can also have the reverse order using k[3:] or can use the starting and ending position as follows

stripped=[ k[0:4] for k in list]
print(stripped)
#['PYTH', 'CPYT', 'IPYT']

stripped=[ k[1:4] for k in list]
print(stripped)
#['YTH', 'PYT', 'PYT']

Python Library Tips: f.strings


f.strings

In Python you can format string in a variety of ways.We learned how to use a %format technique in last post. In Python version 3.7 introduced f.string .

f/F strings are literals begins with letter f/F and It uses { }, curly braces for replacement of expressions with their values

spyder_version=3
python_version=2.7
print(f'I am using Spyder ID {spyder_version} with Python {python_version}')

#Output
I am using Spyder ID 3 with Python 2.7

Arbitrary expression

F"{10*10}"

#Output
100

Function call

We can also call function inside f.strings.

def multi(x,n):
    return x*n
f'{multi(2,2)}'

Multipleline

f-strings are faster than % formatting because they ares constants while f.string expressions are evaluated at runtime.

Also it is possible compose f.strings in multiple line. All strings must begin with ‘f/F’.

>>topic='panda frames'
>>lang='python'
>>version =3.8
>>say=(f'I want to use {topic}'
  f' in {lang}'
  f' version {version}')

>>say
Out[20]: 'I want to use panda frames in python version 3.8'

It can also be rewritten using ‘\’

>>say=f'I want to use {topic}' \
f' in {lang}' \
f' version {version}'