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

A demo Piccolo app - a movie database!

PyMDb Welcome to the Python Movie Database! Built using Piccolo, Piccolo Admin, and FastAPI. Created for a presentation given at PyData Global 2021. R

11 Oct 16, 2022
A repository containing an introduction to Panel made to be support videos and talks.

👍 Awesome Panel - Introduction to Panel THIS REPO IS WORK IN PROGRESS. PRE-ALPHA Panel is a very powerful framework for exploratory data analysis and

Marc Skov Madsen 51 Nov 17, 2022
Cool little Python scripts & projects I've made.

Little Python Projects A repository for neat little Python scripts I've made! How to run a script: *NOTE: You'll need to install Python v3 or higher.

dood 1 Jan 19, 2022
A python package to adjust the bias of probabilistic forecasts/hindcasts using "Mean and Variance Adjustment" method.

Documentation A python package to adjust the bias of probabilistic forecasts/hindcasts using "Mean and Variance Adjustment" method. Read documentation

1 Feb 02, 2022
This is a simple web interface for SimplyTranslate

SimplyTranslate Web This is a simple web interface for SimplyTranslate List of Instances You can find a list of instances here: SimplyTranslate Projec

4 Dec 14, 2022
Self sustained producer-consumer(prosumer) policy study using Python and Gurobi

Prosumer Policy This project aims to model the optimum dispatch behaviour of households with PV and battery systems under different policy instrument

Tom Xu 3 Aug 31, 2022
Taxonomy addition for complete trees

TACT: Taxonomic Addition for Complete Trees TACT is a Python app for stochastic polytomy resolution. It uses birth-death-sampling estimators across an

Jonathan Chang 3 Jun 07, 2022
A script to generate NFT art living on the Solana blockchain.

NFT Generator This script generates NFT art based on its desired traits with their specific rarities. It has been used to generate the full collection

Rude Golems 24 Oct 08, 2022
Powerful Assistant

Delta-Assistant Hi I'm Phoenix This project is a smart assistant This is the 1.0 version of this project I am currently working on the third version o

1 Nov 17, 2021
msgqywx 使用企业微信的应用消息推送实时信息

msgqywx 使用企业微信的应用消息推送实时信息

Demon Finch 8 Dec 18, 2022
GDIT: Geometry Dash Info Tool

GDIT: Geometry Dash Info Tool This is the first large script that allows you to quickly get information from the Geometry Dash server

dezz0xY 2 Jan 09, 2022
Sailwind Mod Manager

Sailwind Mod Manager The Sailwind Mod Manager is an open source mod manager for the Sailwind community. It currently allows you to browse and download

Max 3 Jul 15, 2022
flake8 plugin which checks that there is no use of sleep in the code.

flake8-sleep flake8 plugin which checks for use of sleep function. installation Using Pypi: pip install flake8-sleep flake8 codes Code Description SLP

1 Nov 26, 2021
An open source server for Super Mario Bros. 35

SMB35 A custom server for Super Mario Bros. 35 This server is highly experimental. Do not expect it to work without flaws.

Yannik Marchand 162 Dec 07, 2022
Un Assistente Vocale scritto in Python e altamente personalizzabile

Un Assistente Vocale scritto in Python e altamente personalizzabile

Marco 2 May 06, 2022
Run-Your-Own Firefox Sync Server

Run-Your-Own Firefox Sync Server This is an all-in-one package for running a self-hosted Firefox Sync server. It bundles the "tokenserver" project for

Mozilla Services 1.7k Dec 30, 2022
Dungeon Dice Rolls is an aplication that the user can roll dices (d4, d6, d8, d10, d12, d20 and d100) and store the results in one of the 6 arrays.

Dungeon Dice Rolls is an aplication that the user can roll dices (d4, d6, d8, d10, d12, d20 and d100) and store the results in one of the 6 arrays.

Bracero 1 Dec 31, 2021
Spyware baseado em Python para Windows que registra como atividades da janela em primeiro plano, entradas do teclado.

Spyware baseado em Python para Windows que registra como atividades da janela em primeiro plano, entradas do teclado. Além disso, é capaz de fazer capturas de tela e executar comandos do shell em seg

Tavares 1 Oct 29, 2021
This is a simple python script for checking A/L Examination results of srilankan students

AL-Result-Checker This is a simple python script for checking A/L Examination results of srilankan students INSTALLATION [Termux] [Linux] : apt-get up

Razor Kenway 8 Oct 24, 2022
Notifies server owners of mod updates, also notifies of player deaths and player joins through Discord.

ProjectZomboid-ServerAssistant Notifies server owners of mod updates, also notifies of player deaths and player joins through Discord. A Python based

3 Sep 30, 2022