Category Archives: Python

Using pelican themes


Pelican is a Python based static site/webpage generator, which is famous among opensource and python lovers.

There are plenty of beautiful themes to style pelican blog site contributed by the awesome community, you can have a look at them @ pelican-themes.

Editor’s Picked themes

I have a hand picked themes for bloggers and for documenting projects

  1. Elegant
  2. Flex
  3. Iris
  4. lazystrap
  5. Pelican-clean-blog
  6. Peli-Kiera
  7. Photowall
  8. Tuxlite-tbs

When it come for blog I have Flex and Elegant in my mind. Elegant is my favorite, which is come with many features and good typography and it is good for blog, portfolio page etc.

How to get themes

You can clone the theme repository to you local machine or you can clone individual theme of your choice as follows

git clone https://github.com/alexandrevicenzi/Flex.git

How to use themes in your project

To use themes in your project, create a folder named themes , inside the site folder where you can see content folder and place your theme folder inside it.

Now go to your pelicanconfig.py / settings.py file and add the following line

THEME = 'themes/elegant'

Save changes and hit pelican content and pelican –listen

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

How to import posts from WordPress to Pelican blog


Pelican is a python powered static site generator which is perfect for creating open blog, project page etc. It is an opensource project, there are many useful plugins and themes to explore.

Compose post with markdown file and host your site with GitHub Page, that make simple.

Import WordPress Posts

You can download posts from WordPress and have it on your GitHub blog.

  • Download WordPress posts using WP Admin tools
  • fetch posts from posts.xml file using command line tool as follows
pelican-import -m markdown --wpfile wp/posts.xml

Here is a demo of GitHub Page made in Pelican

Automate PYPI upload with GitHub workflow/Action


Github workflow can be used for building and Testing and Uploading Packages to Python Package Index.

PYPI better known for the use of pip command in Python environment, is a repository of popular opensource projects. You can too contribute your packages to the community. All you need is an account with few setup tools.

Prepare You Python Package

First up all you have to prepare your Python package . Make sure package consist of

  • Setup.py
  • Readme.md file

Setup.py

Here is a sample code of setup.py

import os

with open("readme.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="PACAKGE-NAME-USERNAME",  # Replace with your own username
    version="1.0.1",
    author="author name",
    author_email="email",
    description="short description",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/udername/pakg-repo",
    packages=setuptools.find_packages(),
    install_requires=[
         'pacakge1','package2'
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

Readme.md

Read me file consist of

  • Repository/package details
  • Requirements
  • How to use the repo etc

Use a markdown page for serving the readme file

Github Repository and Action

Now it is time for setup workflow for the python repository you have uploaded to the Github.com

Get PYPI account

As prerequisite for this tutorial you need to register an account in PYPi.org.

Repository secret

Go to you repository settings-Secrets and add the following secrets

PYPI_USERNAME

PYPI_PASSWORD

GitHub Secret

Add a workflow for upload a Python Package

Add a action , use manual method and copy following Upload PYPI workflow file also name file as UploadPYPI.yml

# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload PYPI

.
  release:
    types: [created]

jobs:
  deploy:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install setuptools wheel twine
    - name: Build and publish
      env:
        TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        python setup.py sdist bdist_wheel
        twine upload  dist/*

The above workflow will install dependencies on a Ubuntu cloud machine, then build the package, then upload the package to testpypi repository using twine and secret you have added to your repository, namely PYPI_USERNAME and PYPI_PASSWORD.

As we specified on the on section of the workflow, will execute the action whenever you create a new release.

To test the workflow create a release with a tag starting with v*, eg:- v1,v1.2.1 etc and the workflow will fire the actions.

You can check for result of the workflow at the actions link of your repository, also on the top of repo a notification will shown if the workflow is executed.

Tetsing The installed repo

Now check your Pypi account, under the account you can see, the uploaded projects. If the project was found on the list, can install them with pip

pip install <YOUR-PACKAGENAME>

Error

You may have caught some error while retrying publishing packages. One of them was name can’t be reused.

Pypi/TestPYPI not allowing you to reuse the package name, even if you have deleted your package from the Project list.

So Name your package with care. To change the package Name use the setup.py

Automate TestPYPI upload of python packages with GitHub workflow


Github workflow can be used for building and Testing packages on Github repositories. For Python package repository, workflow/ GitHub action can be used to automate TestPYPI upload.

(TestPYPI is a separate instance of the Python Package Index that allows you to try distribution tools and processes without affecting the real index).

Prepare You Python Package

First up all you have to prepare your Python package . Make sure package consist of

  • Setup.py
  • Readme.md file

Setup.py

Here is a sample code of setup.py

import os

with open("readme.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="PACAKGE-NAME-USERNAME",  # Replace with your own username
    version="1.0.1",
    author="author name",
    author_email="email",
    description="short description",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/udername/pakg-repo",
    packages=setuptools.find_packages(),
    install_requires=[
         'pacakge1','package2'
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

Readme.md

Read me file consist of

  • Repository/package details
  • Requirements
  • How to use the repo etc

Use a markdown page for serving the readme file

Github Repository and Action

Now it is time for setup workflow for the python repository you have uploaded to the Github.com

Get a TestPYPI account

As prerequisite for this tutorial you need to register an account in TestPYPi.org.

Repository secret

Go to you repository settings-Secrets and add the following secrets

PYPI_USERNAME

PYPI_PASSWORD

GitHub Secret

Add a workflow for upload a Python Package

Add a action , use manual method and copy following Upload TestPYPI workflow file also name file as UploadTestPYPI.yml

# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload PYPI

.
  release:
    types: [created]

jobs:
  deploy:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install setuptools wheel twine
    - name: Build and publish
      env:
        TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        python setup.py sdist bdist_wheel
        twine upload --repository testpypi dist/*

The above workflow will install dependencies on a Ubuntu cloud machine, then build the package, then upload the package to testpypi repository using twine and secret you have added to your repository, namely PYPI_USERNAME and PYPI_PASSWORD.

As we specified on the on section of the workflow, will execute the action whenever you create a new release.

You can check for result of the workflow at the actions link of your repository, also on the top of repo a notification will shown if the workflow is executed.

How to run selenium webdriver with headless mode


Selenium is one of the powerful package available in Python , used for web automation , no doubt about that. In the last we learned how to use a web driver with webdriver manager package. In this we will learn how to use a headless mode of action in selenium webdriver.

Headless mode

What is headless mode ? In head less mode ,browser you automate work in the background and the work can be completed silently. So how to run selenium web driver in headless mode .

  • You need to create browser option and add –headless.
  • Add the option to webdriver

Chrome option

Create a chrome option as follows and add attch to the web driver.

from selenium.webdriver.chrome.options import Options
chrome_opt = Options()
chrome_opt.add_argument('--headless')
browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(),chrome_options=chrome_opt)

That’s all you need to know

Fix Chrome Driver Error or Selenium webdriver error


When I started with Selenium in Python my biggest problem was the webdriver issue which I later found that there is an automatic download option in python using some special package.

The webdriver sometimes not compatible with the browser you are using. You have to manually find them from the official websites. This is messy part of selenium, I think.

We can get ride off this problem using webdriver_manager

first you need to install the Python package using pip

pip install webdriver_manager

Now you can use it in your project

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

The package will automatically check for compatible version and install it and no more worries about web driver and scrap the web with ease and peace.

flask-gentelella : A beautiful admin bootstrap for pythoners


Gentelella is a free to use bootstrap admin template for developing Python Flask web app, with custom Ajax libraries which includes variety of chart scripts like eChart, Morris Chart etc.

It packed with all must have AJAX and CSS libraries for a Python Web Geek

The bootstrap is also a ready to use flask app, all you need to do is customize the custom script. The app uses blueprint to organize flask routes, which is automatically created based on based on modules.

You can develop database powered app with less pain in few hours with this awesome bootstrap from Colorlib.

Clone and start using the project today

git clone https://github.com/afourmy/flask-gentelella.git
cd flask-gentelella

For instruction and the bootstrap project please visit GitHub page

Have a look at my demo app hosted on heroku.com which is not using any database, it depends on simple json API

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