Author: ken

  • Building a Chatbot for Your Website

    Building a Chatbot for Your Website

    In today’s fast-paced digital world, instant communication is no longer a luxury but an expectation. Websites are evolving beyond static pages, aiming to provide dynamic, personalized experiences. One of the most powerful tools to achieve this is the chatbot – an AI-powered assistant designed to interact with users, answer queries, and guide them through your site. Integrating a chatbot can significantly enhance user experience, streamline operations, and boost engagement.

    Why Integrate a Chatbot?

    Adding a chatbot to your website brings a multitude of benefits, transforming how users interact with your brand:

    • 24/7 Availability: Chatbots never sleep. They provide round-the-clock support, ensuring your visitors always have access to information and assistance, regardless of time zones or business hours.
    • Improved User Experience: Instant answers to common questions reduce frustration and wait times. Users can quickly find the information they need, leading to a more positive experience.
    • Lead Generation & Qualification: Chatbots can engage potential customers, gather vital information, qualify leads, and direct them to the appropriate sales channels, often even scheduling appointments.
    • Automated Customer Support: Handle frequently asked questions (FAQs), troubleshoot common issues, and guide users through processes, freeing up human agents to focus on more complex problems.
    • Data Collection & Insights: Chatbot conversations provide valuable data about user queries, pain points, and interests, which can be used to improve products, services, and website content.

    Core Components of a Chatbot

    A functional chatbot is typically built on several key components working in harmony:

    User Interface (UI)

    This is where users interact with the chatbot, often a widget embedded on a website, or an integration with popular messaging platforms like Facebook Messenger or WhatsApp.

    Natural Language Understanding (NLU) / Processing (NLP)

    The brain of the chatbot. NLU allows the chatbot to interpret and understand user input, identifying the intent (what the user wants to do) and entities (key pieces of information) within a message.

    Dialog Management

    Manages the flow of conversation. It keeps track of the conversation state, decides the next appropriate response, and ensures a coherent dialogue. This often involves predefined rules, decision trees, or more advanced machine learning models.

    Backend Integration

    For chatbots to be truly useful, they often need to connect with other systems – databases for product information, CRM systems for customer data, booking systems, or other APIs to fetch real-time data or trigger actions.

    Choosing Your Chatbot Development Approach

    There are several paths you can take to build a chatbot, depending on your technical expertise, budget, and desired level of customization:

    Low-Code/No-Code Platforms

    These platforms provide a visual interface to design conversation flows, manage intents, and integrate with various services, requiring minimal to no coding.
    * Examples: Google Dialogflow, AWS Lex, Microsoft Azure Bot Service, ManyChat.
    * Pros: Rapid development, easy to use for non-developers, good for common use cases.
    * Cons: Vendor lock-in, limited customization, may become expensive at scale.

    Open-Source Frameworks

    For developers who want more control and flexibility, open-source frameworks provide the building blocks for creating custom chatbots.
    * Examples: Rasa (Python), ChatterBot (Python).
    * Pros: Full control over logic and data, no vendor lock-in, highly customizable.
    * Cons: Requires coding skills, more complex setup and infrastructure management.

    Custom Development

    Building a chatbot from scratch using programming languages and libraries for NLU/NLP.
    * Examples: Python (with NLTK, spaCy, Flask/Django), Node.js (with natural, Express.js).
    * Pros: Ultimate flexibility, tailor-made solutions, complete control over every aspect.
    * Cons: Most time-consuming and resource-intensive, requires advanced coding and NLP knowledge.

    A Simple Chatbot Logic Example

    Here’s a very basic Python example demonstrating how you might handle simple intents based on keywords. In a real-world scenario, you’d use sophisticated NLP libraries or cloud services for intent recognition.

    # A simple function to handle basic chatbot messages
    def handle_chatbot_message(user_input):
        user_input = user_input.lower() # Convert to lowercase for easier matching
    
        if "hello" in user_input or "hi" in user_input:
            return "Hello there! How can I assist you today?"
        elif "product" in user_input or "catalog" in user_input:
            return "You can view our full product catalog at example.com/products."
        elif "support" in user_input or "help" in user_input:
            return "Visit our support page at example.com/support or call us at 1-800-TECH-BOT."
        elif "hours" in user_input or "open" in user_input:
            return "Our business hours are Monday-Friday, 9 AM to 5 PM EST."
        elif "thank you" in user_input or "thanks" in user_input:
            return "You're most welcome! Is there anything else you need?"
        else:
            return "I'm sorry, I didn't quite understand that. Could you please rephrase?"
    
    # Example of how you might use this function:
    # print(handle_chatbot_message("Hi, what are your products?"))
    # print(handle_chatbot_message("I need some help."))
    # print(handle_chatbot_message("When are you open?"))
    

    Integrating the Chatbot into Your Website

    Once your chatbot’s logic is defined and developed, the next step is to integrate it into your website.

    • JavaScript Widget: Most platform-based chatbots provide a JavaScript snippet to embed directly into your website’s HTML. This widget handles the UI and communication with the chatbot service.
    • API Calls: For custom chatbots, your website’s frontend (using JavaScript) would make API calls to your backend server, which in turn communicates with your chatbot’s core logic or an external chatbot service.

    Best Practices for Chatbot Development

    To ensure your chatbot is effective and provides a positive user experience:

    • Define Clear Goals: What do you want your chatbot to achieve? (e.g., answer FAQs, qualify leads, provide specific information).
    • Start Simple, Iterate: Begin with a narrow scope of capabilities and expand over time based on user feedback and data.
    • User-Friendly Language: Design conversations using natural, clear, and concise language. Avoid jargon.
    • Handle Edge Cases & Errors: Plan for scenarios where the chatbot doesn’t understand the user or encounters an error. Provide fallback responses.
    • Seamless Human Handover: Always provide an option for users to escalate to a human agent if the chatbot can’t help or if the user prefers human interaction.
    • Monitor & Analyze: Regularly review chatbot conversations, performance metrics, and user feedback to identify areas for improvement.

    Conclusion

    Building a chatbot for your website is a powerful step towards enhancing your digital presence. By providing instant support, automating tasks, and gathering valuable insights, chatbots can significantly improve user experience, boost customer satisfaction, and streamline your operations. Whether you opt for a low-code platform or a custom-built solution, embracing chatbot technology can make your website more interactive, efficient, and responsive to the needs of your visitors.


  • Visualizing Stock Market Data with Matplotlib

    Visualizing Stock Market Data with Matplotlib

    Stock market data, in its raw numerical form, can be overwhelming and difficult to interpret. Visualizations, however, transform this complexity into understandable charts, making it easier to identify trends, patterns, and potential insights. Matplotlib, a powerful plotting library in Python, combined with Pandas for data manipulation, provides an excellent toolkit for this task.

    In this blog post, we’ll walk through the process of acquiring stock data and creating informative visualizations using Matplotlib.

    Setting Up Your Environment

    Before we begin, ensure you have the necessary libraries installed. If not, you can easily install them using pip:

    pip install pandas matplotlib yfinance
    

    Note: yfinance is a convenient library for fetching historical stock data from Yahoo Finance. We’ll use it to get real data for our visualizations.

    Next, let’s import the libraries we’ll need in our Python script:

    import pandas as pd
    import matplotlib.pyplot as plt
    import yfinance as yf
    

    Acquiring Stock Data

    For this example, we’ll fetch historical data for a popular stock, such as Apple (AAPL), for a specific time frame using yfinance.

    # Define the ticker symbol and time frame
    ticker_symbol = "AAPL"
    start_date = "2023-01-01"
    end_date = "2024-01-01" # Data up to (but not including) this date
    
    # Fetch data using yfinance
    df = yf.download(ticker_symbol, start=start_date, end=end_date)
    
    # Display the first few rows of the DataFrame
    print(df.head())
    

    The df.head() output will show you a Pandas DataFrame with columns like Open, High, Low, Close, Adj Close, and Volume.

    Basic Line Plot: Closing Price Over Time

    The simplest yet most fundamental visualization is a line plot of the closing price over time. This helps us quickly grasp the general trend and price movements of the stock.

    plt.figure(figsize=(12, 6))
    plt.plot(df.index, df['Close'], label='AAPL Close Price', color='blue')
    plt.title(f'{ticker_symbol} Close Price Over Time ({start_date} to {end_date})')
    plt.xlabel('Date')
    plt.ylabel('Close Price (USD)')
    plt.grid(True)
    plt.legend()
    plt.show()
    

    Adding More Insights: Volume and Moving Averages

    To gain a deeper understanding, we can incorporate additional data points like trading volume and calculated indicators such as moving averages. Moving averages smooth out price data to identify trends, while volume indicates the strength of price movements.

    Let’s calculate a 50-day and 200-day Simple Moving Average (SMA).

    # Calculate Simple Moving Averages
    df['SMA_50'] = df['Close'].rolling(window=50).mean()
    df['SMA_200'] = df['Close'].rolling(window=200).mean()
    
    # Create subplots for price and volume
    # sharex=True ensures both subplots share the same x-axis (date)
    # gridspec_kw adjusts the height ratio, giving more space to price
    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(14, 10), sharex=True, gridspec_kw={'height_ratios': [3, 1]})
    
    # Plot Close Price and Moving Averages on the first subplot
    ax1.plot(df.index, df['Close'], label='Close Price', color='blue', linewidth=1.5)
    ax1.plot(df.index, df['SMA_50'], label='50-Day SMA', color='orange', linestyle='--', linewidth=1.0)
    ax1.plot(df.index, df['SMA_200'], label='200-Day SMA', color='red', linestyle='--', linewidth=1.0)
    ax1.set_title(f'{ticker_symbol} Price with Moving Averages and Volume')
    ax1.set_ylabel('Price (USD)')
    ax1.legend()
    ax1.grid(True)
    
    # Plot Volume on the second subplot
    ax2.bar(df.index, df['Volume'], label='Volume', color='gray', alpha=0.7)
    ax2.set_xlabel('Date')
    ax2.set_ylabel('Volume')
    ax2.legend()
    ax2.grid(True)
    
    # Adjust layout to prevent overlap
    plt.tight_layout()
    plt.show()
    

    Customizing Your Visualizations

    Matplotlib offers extensive customization options to make your plots more informative and aesthetically pleasing. Here are a few common elements you might want to adjust:

    • Titles and Labels: Already demonstrated with plt.title(), plt.xlabel(), plt.ylabel().
    • Colors and Linestyles: Control appearance using color='red', linestyle='--', linewidth=2 within plt.plot().
    • Legends: plt.legend() uses the label argument provided in plotting functions to identify series.
    • Figure Size: plt.figure(figsize=(width, height)) or fig, ax = plt.subplots(figsize=(width, height)) sets the overall size.
    • Grids: plt.grid(True) adds a grid for better readability of values.
    • Saving Plots: Use plt.savefig('my_stock_plot.png', dpi=300) before plt.show() to save your visualization to a file.

    Conclusion

    Visualizing stock market data is a crucial step in financial analysis, transforming raw numbers into understandable patterns. With Matplotlib and Pandas, you have powerful tools at your fingertips to create insightful charts. We’ve covered how to fetch historical stock data, create basic line plots of closing prices, and integrate more complex elements like moving averages and trading volume into multi-panel plots. This foundational knowledge empowers you to explore more advanced charting techniques and gain deeper, data-driven insights into market trends.

    Category: Data & Analysis
    Tags: Data & Analysis, Matplotlib, Pandas

  • Creating Your Own Productivity Powerhouse: A Django and SQLite Guide

    Categories: Productivity
    Tags: Productivity, Django, Coding Skills

    Creating Your Own Productivity Powerhouse: A Django and SQLite Guide

    Introduction

    In an age of endless distractions, building tools that help us focus and achieve our goals can be incredibly empowering. While there are countless productivity apps available, creating your own offers unmatched customization and a deeper understanding of your workflow. This guide will walk you through building a simple, yet effective, productivity application using Django, a high-level Python web framework, and SQLite, a lightweight, serverless database.

    Why Django and SQLite?

    Choosing the right tools is crucial for any project. Here’s why Django and SQLite are an excellent combination for a personal productivity app:

    • Django:
      • Rapid Development: Comes with a “batteries included” philosophy, offering an ORM (Object-Relational Mapper), an administrative interface, and robust security features out-of-the-box.
      • Scalable: While perfect for small projects, Django is designed to handle complex, large-scale applications.
      • Pythonic: Written in Python, making it easy to learn and incredibly readable for Python developers.
    • SQLite:
      • Zero Configuration: SQLite is a file-based database, meaning there’s no separate server process to set up or manage. The entire database is a single file on your disk.
      • Lightweight: Ideal for development, testing, and small to medium-sized applications where a full-fledged database server like PostgreSQL or MySQL is overkill.
      • Reliable: Despite its simplicity, SQLite is highly reliable and ACID-compliant.

    Setting Up Your Environment

    First, let’s prepare your development environment.

    1. Create a Virtual Environment

    It’s good practice to isolate your project’s dependencies.

    python3 -m venv env
    source env/bin/activate # On Windows, use `env\Scripts\activate`
    

    2. Install Django

    With your virtual environment activated, install Django.

    pip install Django
    

    Project Structure: Starting Your Django Project

    Now, let’s create the core Django project and an application within it.

    1. Start the Project

    django-admin startproject productivity_app .
    

    This creates a productivity_app directory and manage.py in your current folder.

    2. Create an App

    Django projects are composed of reusable “apps.” Let’s create one for our tasks.

    python manage.py startapp tasks
    

    Database Configuration (SQLite)

    Django uses SQLite as its default database, which means you typically don’t need to change anything in productivity_app/settings.py for basic setup. You’ll find a DATABASES configuration that looks like this:

    # productivity_app/settings.py
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
    

    This simply tells Django to use the db.sqlite3 file in your project root as the database.

    Defining Your Productivity Model (Task)

    Let’s define what a “task” looks like. Open tasks/models.py and add the following:

    # tasks/models.py
    
    from django.db import models
    from django.utils import timezone
    
    class Task(models.Model):
        title = models.CharField(max_length=200)
        description = models.TextField(blank=True, null=True)
        created_at = models.DateTimeField(default=timezone.now)
        due_date = models.DateTimeField(blank=True, null=True)
        completed = models.BooleanField(default=False)
    
        def __str__(self):
            return self.title
    

    Now, we need to tell Django about this new app and apply the model changes to our database.

    1. Register the App: Add 'tasks' to your INSTALLED_APPS in productivity_app/settings.py.

      “`python

      productivity_app/settings.py

      INSTALLED_APPS = [
      # … other apps
      ‘tasks’,
      ]
      “`

    2. Make and Apply Migrations:

      bash
      python manage.py makemigrations
      python manage.py migrate

      This will create the Task table in your db.sqlite3 file.

    The Django Admin Interface

    One of Django’s most powerful features is its automatically generated admin interface.

    1. Create a Superuser:

      bash
      python manage.py createsuperuser

      Follow the prompts to create an admin username and password.

    2. Register Your Model with the Admin: Open tasks/admin.py and add:

      “`python

      tasks/admin.py

      from django.contrib import admin
      from .models import Task

      admin.site.register(Task)
      “`

    3. Run the Development Server:

      bash
      python manage.py runserver

      Visit http://127.0.0.1:8000/admin/ in your browser, log in with your superuser credentials, and you’ll see your Task model ready for data entry!

    Basic Views and Templates for Displaying Tasks

    Let’s create a simple page to list our tasks.

    1. Define a View

    Open tasks/views.py and add:

    # tasks/views.py
    
    from django.shortcuts import render
    from .models import Task
    
    def task_list(request):
        tasks = Task.objects.all().order_by('due_date')
        return render(request, 'tasks/task_list.html', {'tasks': tasks})
    

    2. Create URLs for the App

    Inside your tasks directory, create a new file named urls.py:

    # tasks/urls.py
    
    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('', views.task_list, name='task_list'),
    ]
    

    3. Include App URLs in Project URLs

    Open productivity_app/urls.py and include the tasks app’s URLs:

    # productivity_app/urls.py
    
    from django.contrib import admin
    from django.urls import path, include # Import include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('tasks.urls')), # Include tasks app URLs
    ]
    

    4. Create a Template

    Create a new directory templates inside your tasks app, and then tasks inside that (tasks/templates/tasks/). Inside tasks/templates/tasks/, create task_list.html:

    <!-- tasks/templates/tasks/task_list.html -->
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My Productivity App</title>
        <style>
            body { font-family: sans-serif; margin: 20px; }
            h1 { color: #333; }
            ul { list-style: none; padding: 0; }
            li { background-color: #f9f9f9; border: 1px solid #ddd; margin-bottom: 10px; padding: 10px; border-radius: 5px; }
            .completed { text-decoration: line-through; color: #888; }
            .due-date { font-size: 0.9em; color: #666; }
        </style>
    </head>
    <body>
        <h1>My Tasks</h1>
        <ul>
            {% for task in tasks %}
                <li {% if task.completed %}class="completed"{% endif %}>
                    <strong>{{ task.title }}</strong>
                    {% if task.due_date %}
                        <span class="due-date"> (Due: {{ task.due_date|date:"M d, Y H:i" }})</span>
                    {% endif %}
                    {% if task.description %}
                        <p>{{ task.description }}</p>
                    {% endif %}
                    <small>Status: {% if task.completed %}Completed{% else %}Pending{% endif %}</small>
                </li>
            {% empty %}
                <li>No tasks yet! Time to get productive.</li>
            {% endfor %}
        </ul>
    </body>
    </html>
    

    Now, visit http://127.0.0.1:8000/ in your browser. You should see a list of tasks you’ve added via the admin interface!

    Conclusion

    Congratulations! You’ve successfully set up a basic productivity application using Django and SQLite. You now have a functional web application with models, views, templates, and an administrative interface. This is just the beginning; you can expand this app by adding features like user authentication, task creation/editing forms, filtering, and more sophisticated UI/UX. The power is now in your hands to build the productivity tool that truly works for you.

  • Level Up Your Python Skills: 5 Tips for Cleaner Code

    Categories: Productivity
    Tags: Coding Skills

    You’ve written your first “Hello, World!” and now you’re hooked on Python. But as you tackle more complex projects, you might notice your code is getting a bit messy. Don’t worry, that’s a normal part of the learning process! To help you write more elegant and efficient code, here are five essential tips that will make your Python journey smoother and your programs more robust.


    1. Use List Comprehensions for Conciseness

    Stop writing long for loops to create new lists. List comprehensions offer a more compact and readable way to build lists. They’re not just for experienced developers; once you get the hang of them, you’ll wonder how you ever lived without them.

    Traditional Loop:

    python
    squares = []
    for i in range(10):
    squares.append(i * i)

        **List Comprehension:**
    
        ```python
        squares = [i * i for i in range(10)]
        ```
    
        This single line of code does the exact same thing as the loop above, but it's much more concise. List comprehensions can also include conditional logic, making them even more powerful.
    

    2. Leverage enumerate() for Indexed Loops

    Ever found yourself needing both the item and its index while looping through a list? A common approach is to use range(len(my_list)). But Python provides a much better way: the enumerate() function.

    Clunky Method:

    python
    my_list = ['apple', 'banana', 'cherry']
    for i in range(len(my_list)):
    print(f"Item {i} is {my_list[i]}")

    Better Method with enumerate():

    python
    my_list = ['apple', 'banana', 'cherry']
    for i, item in enumerate(my_list):
    print(f"Item {i} is {item}")

    The enumerate() function returns a tuple containing a counter and the value from the iterable. It’s cleaner, more “Pythonic,” and often slightly more efficient.


    3. Embrace Context Managers with with

    Working with files or network connections requires you to be careful about closing resources after you’re done with them. If you forget, it can lead to resource leaks. Python’s with statement simplifies this by creating a context manager. It ensures that a resource is properly cleaned up, even if errors occur.

    Manual File Handling:

    python
    file = open('data.txt', 'r')
    data = file.read()
    file.close() # Easy to forget!

        **Context Manager (`with`):**
    
        ```python
      with open('data.txt', 'r') as file:
    

    data = file.read()

    The file is automatically closed here

    “`

    The with statement guarantees that file.close() is called, making your code safer and more reliable. This principle applies to many other objects, including database connections and locks.


    4. Use F-Strings for Easy Formatting

    Gone are the days of clunky string concatenation or the str.format() method. Formatted string literals, or f-strings, are a modern and highly readable way to embed expressions inside strings. Just prefix your string with an f! 🚀

    “`python
    name = “Alice”
    age = 30

    Old way

    message = “My name is {} and I am {} years old.”.format(name, age)

    F-string way

    message = f”My name is {name} and I am {age} years old.”
    “`

    F-strings are not only more readable but also faster than other string formatting methods. You can even include expressions and function calls right inside the curly braces.


    5. Write Docstrings for Clarity

    Good code isn’t just about what it does; it’s also about what it communicates. Docstrings are multi-line string literals that appear as the first statement in a module, function, class, or method definition. They provide essential documentation for your code.

    “`python
    def calculate_area(radius):
    “””
    Calculates the area of a circle given its radius.

    Args:
        radius (float): The radius of the circle.
    
    Returns:
        float: The calculated area.
    """
        return 3.14159 * radius * radius
        ```
    
        Tools like Sphinx can automatically generate documentation from your docstrings. They are a professional habit that will save you and your future collaborators countless hours of confusion. Remember, your code is a story, and **docstrings are the annotations** that make it understandable.
    
  • Nginx + PHP-FPM + MariaDB 環境でつまづきやすいポイント

    WordPress を動かすために Nginx + PHP-FPM + MariaDB を構築する場合、いくつかのハマりポイントがあります。今回はその中でも特によくあるものを3つ紹介します。


    1. localhost127.0.0.1 の違い

    WordPress の wp-config.php
    php
    define( 'DB_HOST', 'localhost' );

    と書いた場合、MariaDB へは ソケット接続 が使われます。一方で
    php
    define( 'DB_HOST', '127.0.0.1' );

    とすれば TCP接続 になります。
    MariaDB の設定と合わないと「Error establishing a database connection」が出るので注意が必要です。


    2. PHP-FPM のログ出力先

    php-fpm.conferror_log が存在しないディレクトリを指していると、起動自体に失敗します。
    特に /usr/local/var/log/ 配下は、権限やファイルシステムの状態でエラーが出やすいので、/var/log/php-fpm.log など OS 標準の場所に寄せるのが安全です。


    3. SELinux やファイルパーミッション

    一見正しく設定していても、502 Bad Gateway や DB 接続エラーが出る場合は、SELinux やディレクトリの権限を疑いましょう。
    テスト時は getenforce で Disabled/Permissive を確認、本番では適切なコンテキストを付与するのがベストです。


    まとめ

    環境構築でハマったときは
    1. DB 接続方法(ソケットかTCPか)
    2. PHP-FPM の設定ファイルとログパス
    3. SELinuxや権限まわり

    この3つを順に確認すると、原因切り分けがスムーズになります。

  • Organizing Files Automatically with Python

    Categories: Automation, Productivity
    Tags: files, organization, os, automation

    Hello!
    Are you tired of your “Downloads” folder being a complete mess?
    With just a few lines of Python, you can automatically sort files into folders based on their type.
    This is a perfect small project for everyday productivity.

    Step 1: Import Required Modules

    import os
    import shutil
    from pathlib import Path
    

    Step 2: Define File Categories

    We can map file extensions to their respective folders. Update or extend this mapping as needed.

    FILE_TYPES = {
        "Images": [".jpg", ".jpeg", ".png", ".gif"],
        "Documents": [".pdf", ".docx", ".txt"],
        "Audio": [".mp3", ".wav"],
        "Videos": [".mp4", ".mov", ".avi"]
    }
    

    Step 3: Create a Sorting Function

    The function below moves files into folders based on their extension. Files with no matching category are skipped.

    def organize_files(folder):
        for file in Path(folder).iterdir():
            if file.is_file():
                moved = False
                for category, extensions in FILE_TYPES.items():
                    if file.suffix.lower() in extensions:
                        target_folder = Path(folder) / category
                        target_folder.mkdir(exist_ok=True)
                        shutil.move(str(file), target_folder / file.name)
                        print(f"Moved {file.name} → {category}")
                        moved = True
                        break
                if not moved:
                    print(f"Skipped {file.name} (no matching category)")
    

    Step 4: Run the Script

    Set the downloads variable to the folder you want to organize and call the function.

    downloads = str(Path.home() / "Downloads")
    organize_files(downloads)
    

    Result

    Now, instead of a messy folder full of random files, your Downloads directory will be neatly organized into Images, Documents, Audio, and Videos.

    This script can be expanded further:
    – Add more categories (e.g., Spreadsheets, Archives)
    – Run it on a schedule with cron or Task Scheduler
    – Combine with cloud storage (Google Drive, Dropbox)

    A little automation can make your daily computer life much smoother. Try running this script once, and you’ll never want to go back to manual file sorting!

  • How to Automatically Sort Emails with Python


    Categories: Automation
    Tags: gmail, automation
    slug: “python-email-auto-sort”
    date: 2025-09-08
    status: “publish”


    How to Automatically Sort Emails with Python

    Introduction

    Is your inbox overflowing with unread messages? Manually sorting emails into folders can be time-consuming and stressful. With a small Python script, you can automate this process and let your computer do the boring work. In this article, we’ll build a simple script that labels Gmail messages automatically.

    Requirements

    • Python 3
    • A Gmail account
    • Gmail API access
    • Libraries: google-api-python-client, google-auth-httplib2, google-auth-oauthlib

    Install the libraries:

    bash
    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

    Step 1: Enable the Gmail API

    1. Open Google Cloud Console and create a project.
    2. Enable Gmail API for that project.
    3. Create OAuth 2.0 Client ID (Desktop app) credentials.
    4. Download credentials.json and place it in your project folder.

    Step 2: Authenticate and Connect

    “`python
    from future import print_function
    import os.path
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build

    SCOPES = [‘https://www.googleapis.com/auth/gmail.modify’]

    def get_service():
    creds = None
    if os.path.exists(‘token.json’):
    creds = Credentials.from_authorized_user_file(‘token.json’, SCOPES)
    if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
    creds.refresh(Request())
    else:
    flow = InstalledAppFlow.from_client_secrets_file(‘credentials.json’, SCOPES)
    creds = flow.run_local_server(port=0)
    with open(‘token.json’, ‘w’) as f:
    f.write(creds.to_json())
    return build(‘gmail’, ‘v1’, credentials=creds)
    “`

    Step 3: Create or Find Labels

    We’ll need a label ID to apply to messages. The function below finds a label by name (e.g., GitHub) or creates it if it doesn’t exist.

    python
    def get_or_create_label_id(service, label_name):
    labels = service.users().labels().list(userId='me').execute().get('labels', [])
    for lb in labels:
    if lb['name'].lower() == label_name.lower():
    return lb['id']
    body = {
    'name': label_name,
    'labelListVisibility': 'labelShow',
    'messageListVisibility': 'show'
    }
    created = service.users().labels().create(userId='me', body=body).execute()
    return created['id']

    Step 4: Sort Incoming Emails

    Example: apply the GitHub label to all messages from notifications@github.com.

    “`python
    def sort_emails(service):
    label_id = get_or_create_label_id(service, ‘GitHub’)
    results = service.users().messages().list(
    userId=’me’, q=”from:notifications@github.com”, maxResults=50
    ).execute()
    messages = results.get(‘messages’, [])

    if not messages:
        print("No GitHub emails found.")
        return
    
    for msg in messages:
        msg_id = msg['id']
        service.users().messages().modify(
            userId='me',
            id=msg_id,
            body={'addLabelIds': [label_id]}
        ).execute()
        print(f"Labeled message: {msg_id}")
    

    “`

    Step 5: Run the Script

    python
    if __name__ == "__main__":
    svc = get_service()
    sort_emails(svc)

    Conclusion

    With the Gmail API and Python, you can take control of your inbox and eliminate repetitive sorting. Extend this idea by labeling newsletters, creating daily digests, or even auto-responding to specific senders.

    Full source code and updates: (GitHub link to be added)