Words Counter and Paragraphs Counter Flask App using Python – with source code – 2024

Hey, guys in this blog we will build a Words Counter and Paragraphs Counter Flask App using Python. This will be an exciting project, So without any further due, let’s do it…

In today’s digital era, where written content plays a crucial role, tools that help analyze and manage text become indispensable. A Words Counter and Paragraphs Counter Flask App built with Python can prove to be a valuable asset for writers, bloggers, students, and professionals alike.

This article will guide you through the process of creating a Flask application that counts words and paragraphs in a given text, empowering you to enhance your writing efficiency.

Checkout the video here – https://youtu.be/0lI18eHU1U8

Functionalities of our App:

  • Words Counter – Our App can count words.
  • Paragraphs Counter – Our App can count paragraphs.
  • Characters Counter – Our App can count characters.

Sneak Peek at our App

Words Counter and Paragraphs Counter Flask App using Python
Words Counter and Paragraphs Counter Flask App

Source Codes for Words Counter and Paragraphs Counter Flask App

app.py

# Words Counter and Paragraphs Counter Flask App using Python

from flask import Flask,request,render_template
from datetime import date

#### Defining Flask App
app = Flask(__name__)


#### Saving Date today in 2 different formats
datetoday = date.today().strftime("%m_%d_%y")
datetoday2 = date.today().strftime("%d-%B-%Y")

def replace_multiple_newlines(text):
    lines = text.split('\n')
    lines = [line for line in lines if line.strip()]
    return len(lines)

################## ROUTING FUNCTIONS #########################

#### Our main page
@app.route('/')
def home():
    return render_template('home.html',datetoday2=datetoday2) 

#### This function will run when we add a new user
@app.route('/count',methods=['GET','POST'])
def count():
    text = request.form['text']
    
    words = len(text.split())
    
    paras = replace_multiple_newlines(text)

    text = text.replace('\r','')
    text = text.replace('\n','')

    chars = len(text)
    return render_template('home.html',words=words,paras=paras,chars=chars,datetoday2=datetoday2) 


#### Our main function which runs the Flask App
if __name__ == '__main__':
    app.run(debug=True)

home.html

<!doctype html>
<html lang="en">

<style type='text/css'>
    * {
        padding: 0;
        margin: 0;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }


    body {
        background-image: url('https://cutewallpaper.org/21/1920-x-1080-gif/1920x1080-Wallpapercartoon-Wallpapers-Driverlayer-Search-.gif');
        background-size: cover;
        font-family: sans-serif;
        margin-top: 40px;
        height: 100vh;
        padding: 0;
        margin: 0;
    }

    table {
        border: 1px;
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 86%;
        margin: auto;
    }

    td,
    th {
        border: 1px solid black !important;
        padding: 5px;
    }

    tr:nth-child(even) {
        background-color: #dddddd;
    }
</style>


<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title>Words and Paragraphs Counter</title>
</head>

<body>


    <div class='mt-1 text-center'>
        <h1 style="width: auto;margin: auto;color: white;padding: 11px;font-size: 44px;">Words and Paragraphs Counter
        </h1>
    </div>

    <div class='mt-3 text-center'>
        <h3 style="font-size: 22px;color:beige;">{{ datetoday2 }} | <span id="clock"></span></h3>
    </div>

    {% if mess%}
    <p class="text-center" style="color: red;font-size: 20px;">{{ mess }}</p>
    {% endif %}

    <div class="row text-center" style="padding: 20px;margin: 20px;">

        <div class="col"
            style="border-radius: 20px;padding: 0px;background-color:rgb(211,211,211,0.5);margin:0px 10px 10px 10px;height: 400px;">
            <form action='/count' method="POST" enctype="multipart/form-data">
                <h2 style="border-radius: 20px 20px 0px 0px;background-color: #0b4c61;color: white;padding: 10px;">Count
                    Words and Paragraphs</h2>
                <label style="font-size: 20px;"><b>Enter Text*</b></label>
                <br>
                <textarea type="text" id="text" name='text' style=" font-size: 20px;width: 650px;margin-top: 10px;height: 125px;margin-bottom: 10px;"
                    required></textarea>
                <br>
                <button
                    style="font-size: 24px;font-weight: bold;border-radius: 10px;width:490px;padding: 10px;margin-top: 30px;margin-bottom: 30px;"
                    type='submit' class='btn btn-primary'>COUNT</button>
                {% if words%}
                <p class="text-center" style="color: black;font-size: 21px;">Words - {{ words }} || Paragraphs - {{ paras
                    }} || Characters - {{ chars }}</p>
                {% endif %}
            </form>
        </div>

    </div>

    <script type="text/javascript">
        var clockElement = document.getElementById('clock');

        function clock() {
            clockElement.textContent = new Date().toString().slice(15, 24);
        }

        setInterval(clock, 1000);
    </script>

</body>

</html>

Results of our Words Counter and Paragraphs Counter Flask App

Words Counter and Paragraphs Counter Flask App using Python

Download Source Code

Conclusion

In this article, we have explored the development of a Words Counter and Paragraphs Counter Flask App using Python. We have covered the steps involved in setting up the Flask app, designing the user interface, implementing the words counter and paragraphs counter functionalities, testing the app, and deploying it to a production environment.

So this is how you can code a Words Counter and Paragraphs Counter Flask App using Python. If you have any doubt regarding this, you can contact me by mail.

FAQs

Can I customize the design of the app?

Yes, the user interface design can be customized according to your preferences. The HTML, CSS, and Bootstrap files can be modified to match your desired style.

Is it possible to count words and paragraphs in different languages?

Yes, the Words Counter and Paragraphs Counter Flask App can handle text in any language. It utilizes Python’s built-in string manipulation functions and regular expressions, which are language-agnostic.

Can I integrate this app with my existing website?

Absolutely! The Flask app is highly modular and can be easily integrated into an existing website or web application. You can leverage Flask’s routing system to incorporate the words and paragraphs counter functionality seamlessly.

Is there a limit on the size of the text that can be analyzed?

While there is no hard limit imposed by the app itself, it is important to consider the performance implications of analyzing large volumes of text. Extremely long texts may impact the app’s responsiveness and processing speed.

Can I export the results obtained from the app?

Yes, the app can be extended to include an export functionality. You can add a feature that allows users to save the word and paragraph count results to a file or database for future reference.

Read my last article – Create Your Own To-Do List Flask App Using Python: Step-by-Step Guide with Source Code Included!

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net

Leave a Reply

Your email address will not be published. Required fields are marked *