How to Create Your Own Taplink Using Python and GitHub Pages
I accidentally came across the author's article Lucas Neves Pereira under the title “Build your own LinkTree with Go and GitHub Pages“. The article describes how to create a LinkTree-like (analogous to Taplink) in Go and GitHub Pages. As a Python fan, I decided to implement the project in this language.
Step 1: Preparing the project file structure
The first thing we'll do is create a file structure for our project. We'll organize our project in a way that makes it easy to maintain and deploy to GitHub Pages.
File structure:
/ (root)
|-- /docs
| |-- index.html
| |-- /assets
| |-- (файлы стилей, скриптов, иконок и т.д.)
|-- config.yml
|-- generate_site.py
|-- /themes
/docs: This folder will contain generated HTML files and all necessary assets (images, styles, scripts). This folder will be used to deploy the site to GitHub Pages.
config.yml: Configuration file that contains all the data for personalizing the site.
generate_site.py: Python script that will generate a site based on data from config.yml.
/themes: Folder with themes for the site. In our case, only one theme is stored here customwhich includes an HTML template, styles, scripts and images.
Step 2: Setting up the configuration file (config.yml)
File config.yml contains user data and links that will be displayed on the site. Here is its content:
name: "King Triton"
picture: "assets/img/picture.jpg"
bio: "Programmer python and php/laravel"
meta:
lang: "en"
description: "Programmer python and php/laravel"
title: "King Triton"
author: "King Triton"
siteUrl: "https://king-tri-ton.github.io/pythonpagelink/"
links:
- name: "Github"
url: "https://github.com/king-tri-ton"
- name: "Dev.to"
url: "https://dev.to/king_triton"
- name: "Patreon"
url: "https://www.patreon.com/king_triton"
- name: "Telegram"
url: "https://t.me/king_triton"
- name: "Instagram"
url: "https://www.instagram.com/king_tri_ton"
theme: "custom"
name: The username that will be displayed on the site.
picture: Path to the user's image.
bio: Brief biography of the user.
meta: Site meta information (language, description, title, author, site URL).
links: List of links that will be displayed on the site. Each element contains a title and URL.
theme: The website theme to use.
Step 3: Develop a Python script to generate the site (generate_site.py)
Next we will write a Python script that will use the template from the theme, data from config.yml and generate a ready-made HTML file.
import os
import shutil
from jinja2 import Environment, FileSystemLoader
import yaml
# Загрузка конфигурации
with open('config.yml', 'r') as config_file:
config = yaml.safe_load(config_file)
# Создание выходной директории
output_dir="docs"
os.makedirs(output_dir, exist_ok=True)
# Настройка Jinja2
env = Environment(loader=FileSystemLoader('themes/custom'))
template = env.get_template('index.html')
# Генерация HTML файла
output_html = template.render(config=config)
with open(os.path.join(output_dir, 'index.html'), 'w') as fh:
fh.write(output_html)
# Копирование папки assets в выходной каталог
assets_source = os.path.join('themes', config['theme'], 'assets')
assets_dest = os.path.join(output_dir, 'assets')
if os.path.exists(assets_source):
shutil.copytree(assets_source, assets_dest, dirs_exist_ok=True)
print("Site generated successfully.")
Loading configuration: Script loads data from file config.yml.
Creating an output directory: Folder docs created automatically if it does not exist.
Setting up Jinja2: Uses Jinja2 to load the HTML template and render the content.
Generating HTML File: The script generates a file index.html using the data from the configuration and saves it in the folder docs.
Copying assets: All assets (CSS, images, scripts) are copied to the folder docs/assets.
Step 4: Create Theme and Assets
Now let's create a theme that will be used for our site. In the folder themes/custom/ The following files should be present:
themes/custom/index.html
This is the main HTML template of the site. It uses variables from the configuration file.
<!DOCTYPE html>
<html lang="{{ config.meta.lang }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{{ config.meta.description }}">
<title>{{ config.meta.title }}</title>
<meta name="author" content="{{ config.meta.author }}">
<link rel="canonical" href="https://habr.com/ru/articles/839574/{{ config.meta.siteUrl }}">
<link rel="icon" type="image/x-icon" href="assets/icons/favicon.ico">
<link rel="stylesheet" href="assets/css/styles.css">
<meta property="og:title" content="{{ config.meta.title }}">
<meta property="og:site_name" content="{{ config.meta.title }}">
<meta property="og:description" content="{{ config.meta.description }}">
<meta property="og:locale" content="{{ config.meta.lang }}">
<meta name="twitter:title" content="{{ config.meta.title }}">
<meta name="twitter:description" content="{{ config.meta.description }}">
</head>
<body>
<header>
<img src="{{ config.picture }}" alt="Picture" class="avatar">
<h1>{{ config.name }}</h1>
<small class="bio">{{ config.bio }}</small>
</header>
<main>
<section class="links">
{% for link in config.links %}
<a class="link-item" href="{{ link.url }}" target="_blank" rel="noopener noreferrer">
<p>{{ link.name }}</p>
</a>
{% endfor %}
</section>
</main>
<footer>
<small>© <span class="year"></span> {{ config.meta.author }}</small>
</footer>
<script src="assets/js/script.js"></script>
</body>
</html>
themes/custom/assets/styles.css
CSS file for styling the page.
/* CSS Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Variables */
:root {
--max-width: 600px;
--font-family: 'Inter', sans-serif;
--padding: 1rem;
--header-margin-bottom: 1rem;
--line-height: 2;
--font-size: 16px;
--primary-color-light: #ffffff;
--background-color-light: #f0f0f0;
--text-color-light: #333;
--link-color-light: #1a73e8;
--bio-color-light: #666;
--primary-color-dark: #1e1e1e;
--background-color-dark: #121212;
--text-color-dark: #e0e0e0;
--link-color-dark: #8ab4f8;
--bio-color-dark: #aaa;
}
/* Light Theme */
@media (prefers-color-scheme: light) {
:root {
--primary-color: var(--primary-color-light);
--background-color: var(--background-color-light);
--text-color: var(--text-color-light);
--link-color: var(--link-color-light);
--bio-color: var(--bio-color-light);
}
}
/* Dark Theme */
@media (prefers-color-scheme: dark) {
:root {
--primary-color: var(--primary-color-dark);
--background-color: var(--background-color-dark);
--text-color: var(--text-color-dark);
--link-color: var(--link-color-dark);
--bio-color: var(--bio-color-dark);
}
}
/* Global Styles */
html {
font-family: var(--font-family);
font-size: var(--font-size);
line-height: var(--line-height);
}
body {
max-width: var(--max-width);
min-height: 100vh;
margin: 0 auto;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--background-color);
color: var(--text-color);
padding: var(--padding);
}
/* Header Styles */
header {
padding: var(--padding) 0;
margin-bottom: var(--header-margin-bottom);
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
.avatar {
width: 100px;
height: 100px;
border-radius: 50%;
object-fit: cover;
border: 2px solid var(--primary-color);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 24px;
margin-bottom: 0.5rem;
}
.bio {
font-size: 14px;
color: var(--bio-color);
margin-bottom: 1rem;
}
/* Main Content Styles */
main {
width: 100%;
flex: 1;
}
.links {
display: flex;
flex-direction: column;
gap: 1rem;
text-align: center;
overflow-y: auto;
max-height: 400px;
}
.link-item {
display: block;
padding: 16px 20px;
text-decoration: none;
color: var(--link-color);
background: var(--primary-color);
border-radius: 12px;
border: 1px solid var(--link-color);
transition: background-color 0.25s, color 0.25s;
}
.link-item:hover,
.link-item:focus {
background-color: var(--link-color);
color: var(--primary-color);
}
.link-item p {
line-height: 1.5;
font-weight: 500;
}
/* Footer Styles */
footer {
width: 100%;
text-align: center;
padding: 1rem 0;
font-size: 14px;
gap: 1rem;
display: flex;
justify-content: center;
align-items: center;
}
/* ScrollBar */
::-webkit-scrollbar {
width: 5px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: transparent;
}
::-webkit-scrollbar-thumb:hover {
background: transparent;
}
themes/custom/assets/js/script.js
JavaScript file for basic functionality.
console.log("scripts loaded");
const yearDate = new Date().getFullYear().toString();
document.querySelector(".year").innerText = yearDate;
themes/custom/assets/img/picture.jpg
The photo that will be used as an avatar.
Step 5: Generate a site
Once all files are created, run the script generate_site.pyto generate a website:
python generate_site.py
The site will be generated in the docs folder.
Step 6: Deploy to GitHub Pages
Create a new repository on GitHub.
Upload all files including the folder docsto the repository.
Go to the Settings section of the repository.
In the Pages section, select a branch master and the folder /docs as a source.
Save your changes and wait for GitHub Pages to deploy your site.
Now your site will be available at https://[username].github.io/[repository-name]/
That's it! You now have your own Taplink-style website, built in Python and deployed to GitHub Pages. You can see my finished product at https://king-tri-ton.github.io/pythonpagelink/.
Thank you for your attention!