WordPress-style shortcodes for Python

Overview

Python Shortcodes

WordPress-style shortcodes for Python

Create and use WordPress-style shortcodes in your Python based app.

Example

# static output
def my_function(atts = None, enclosed_content = ''):
    return "Output of my function"

# using attributes and enclosed content (pretty simple example)    
def tags_around(atts = None, enclosed_content = ''):
    tag = atts['tag']
    return '<' + tag + '>' + enclosed_content + '</' + tag + '>'

Shortcodes.add_shortcode('my-function', my_function)
Shortcodes.add_shortcode('tags-around', tags_around)

content = """Testing: [my-function]. This will be [tags_around tag="b"]bold[/tags_around]."""
print(Shortcodes.do_shortcodes(content))

# will generate output:
Testing: Output of my function. This will be <b>bold</b>.

Why Use Them?

Just have a look at how powerful WordPress shortcodes are. This simple library is giving you the most improtant functionality to use in your Python projects, in Flask or Django apps, any Python based CMS, etc.

Just think about all the possibilities you are giving to the users of your app: admins, editors, or even end users can use powerful, defined by you functions to add all kind of output to user generated content, in widgets, etc.

The shortcodes are most often used in CMS-es to allow authors or editors to include powerful functionality anywhere on the site. You can have shortcodes to create contact forms, quizzes, product lists, dynamic reports - basically everything.

Documentation

  1. You can simply copy the library in a working folder and import: from shortcodes import Shortcodes

  2. Create your custom shortcode functions. Each shortcode function or a class method should accept two optional parameters: atts and enclosed_content

Example:

# accepts name in enclosed content and capitalizes first letter
def ucfirst(atts = None, enclosed_content = ''):
s = enclosed_content
if s == '':
return '' # if no enclosed content this shortcode returns nothing
s = s[0].upper() + s[1:]
return atts['add_text'] + ' ' + s

The function should not have side effects and must return the content. The returned content will replace the shortcode inside your content after calling Shortcodes.do_shortcode() on it. See step 4.

  1. Each shortcode needs to be regusrered with add_shortcode call. Example: Shortcodes.add_shortcode('ucfirst', ucfirst)

Unlike in WordPress, in Python you need to send the function object, and not a string with the function name.

  1. Finally, call Shortcodes.do_shortcodes() on your content to have all shortcodes replaced with the output of the associated function: Shortcodes.do_shortcode(content)

  2. Shortcodes can contain named parameters with their values enclosed in double quotes. Example: [show-time format="%H:%M:%S"] In this example the shortcode function will receive parameter "format" with value %H:%M:%S

  3. Shortcodes can enclose content like this [my-shotcode param1="param 1" param2="param 2"]My content[/my-shortcode] The content will be passed a second argument to your shortcode function, this allowing you to modify it and return it.

Invalid sortcodes will be ignored and just returned in the content.

Have a look at example.py for several use cases and examples. Run it from terminal by calling python example.py

License

MIT

This is a calculator of strike price distance for options.

Calculator-of-strike-price-distance-for-options This is a calculator of strike price distance for options. Options are a type of derivative. One strat

André Luís Lopes da Silva 4 Dec 30, 2022
A GUI love Calculator which saves all the User Data in text file(sql based script will be uploaded soon). Interative GUI. Even For Admin Panel

Love-Calculator A GUI love Calculator which saves all the User Data in text file(sql based script will be uploaded soon). Interative GUI, even For Adm

Adithya Krishnan 1 Mar 22, 2022
Ergonomic option parser on top of dataclasses, inspired by structopt.

oppapī Ergonomic option parser on top of dataclasses, inspired by structopt. Usage from typing import Optional from oppapi import from_args, oppapi @

yukinarit 4 Jul 19, 2022
A Powerful Tool For Making Combo List(All possible modes)

ComboMaker A Powerful Tool For Making Combo List Introduction Check out all possible Combo list build modes with this tool =) How to Install Open the

MasterBurnt 7 Jan 07, 2023
ARA Records Ansible and makes it easier to understand and troubleshoot.

ARA Records Ansible ARA Records Ansible and makes it easier to understand and troubleshoot. It's another recursive acronym. What it does Simple to ins

Community managed Ansible repositories 1.6k Dec 25, 2022
Anki for desktop computers

Anki This repo contains the source code for the computer version of Anki. If you'd like to try development builds of Anki but don't feel comfortable b

Ankitects 12.9k Jan 09, 2023
Программа для практической работы №12 по дисциплине

Информатика: программа для практической работы №12 Код и блок-схема программы для практической работы №12 по дисциплине "Информатика" (I семестр). Сут

Vladislav 1 Dec 07, 2021
Python Repository for Bachelor Ski Sign.

BachelorSkiSign Python Repository for Bachelor Ski Sign. This application reads data from https://bachelorapi.azurewebsites.net/ It is written in Ciru

Winston 1 Jan 04, 2022
Multitrack exporter for OP-Z

Underbridge for OP-Z Multitrack exporter Description Exports patterns and projects individual audio tracks to seperate folders for use in your DAW. Py

Thomas Herrmann 71 Dec 25, 2022
This repo is related to Google Coding Challenge, given to Bright Network Internship Experience 2021.

BrightNetworkUK-GCC-2021 This repo is related to Google Coding Challenge, given to Bright Network Internship Experience 2021. Language used here is py

Dareer Ahmad Mufti 28 May 23, 2022
Custom Python code for calculating the Probability of Profit (POP) for options trading strategies using Monte Carlo Simulations.

Custom Python code for calculating the Probability of Profit (POP) for options trading strategies using Monte Carlo Simulations.

35 Nov 22, 2022
Simple application that does transformation with HPF and LPFs.

Simple application that applies Butterworth, Gaussian & Ideal kernels on HPF and LPFs -aka Frequency Domain Filtering- Upload image from sidebar, set

Merve Noyan 3 Jul 06, 2022
🛠️ Plugin to integrate Chuy with Poetry

Archived This is bundled with Chuy since v1.3.0. Poetry Chuy Plugin This plugin integrates Chuy with Poetry. Note: This only works in Poetry 1.2.0 or

Eliaz Bobadilla 4 Sep 24, 2021
Organize seu linux - organize your linux

OrganizeLinux Organize seu linux - organize your linux Organize seu linux Uma forma rápida de separar arquivos dispersos em pastas. formatos a serem c

Marcus Vinícius Ribeiro Andrade 1 Nov 30, 2021
本仓库整理了腾讯视频、爱奇艺、优酷、哔哩哔哩等视频网站中,能够观看的「豆瓣电影 Top250 榜单」影片。

Where is top 250 movie ? 本仓库整理了腾讯视频、爱奇艺、优酷、哔哩哔哩等视频网站中,能够观看的「豆瓣电影 Top250 榜单」影片,点击 Badge 可跳转至相应的电影首页。

MayanDev 123 Dec 22, 2022
Basic infrastructure for writing scripts in Python

Base Script Python is an excellent language that makes writing scripts very straightforward. Over the course of writing many scripts, we realized that

Deep Compute, LLC 9 Jan 07, 2023
A collection of online resources to help you on your Tech journey.

Everything Tech Resources & Projects About The Project Coming from an engineering background and looking to up skill yourself on a new field can be di

Mohamed A 396 Dec 31, 2022
A project for the Qvault Hackathon, 2022-01-17

musical-octo-engine Steps to run brew install python-tk brew install portaudio

Erik Kristofer Anderson 2 May 17, 2022
tox-gh is a tox plugin which helps running tox on GitHub Actions with multiple different Python versions on multiple workers in parallel

tox-gh is a tox plugin which helps running tox on GitHub Actions with multiple different Python versions on multiple workers in parallel. This project is inspired by tox-travis.

tox development team 19 Dec 26, 2022
MuMMI Core is the underlying infrastructure and generalizable component of the MuMMI framework

MuMMI Core is the underlying infrastructure and generalizable component of the MuMMI framework, which facilitates the coordination of massively parallel multiscale simulations.

4 Aug 17, 2022