Open In App

Typing Speed Test Project Using Python Streamlit Library

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Typing Speed Test Project involves typing content with the input field displayed on the screen where we need to type the same content also a timer of 30 seconds runs continuously, and when it reaches zero, our typing speed is displayed on the screen in words per minute (WPM). This article will guide you through creating a project using the Streamlit library.

Typing Speed Test Project Using Python Streamlit Library

Below is the step-by-step procedure by which we can create a typing speed test project using the Python Streamlit library:

Install Necessary Library

For creating the typing speed test Project in Streamlit, we need first to install it. To install the Streamlit library use the below command.

pip install streamlit

Writing Code

In this example, below Python code uses the Streamlit library to create a typing test game. It defines a function calculate_wpm() to calculate words per minute (WPM) based on user input and time taken. The main() function, displays a prompt for users to type a given text within 30 seconds, then measures their typing speed and displays the result after time runs out.

main.py

Python3
import streamlit as st
import time

def calculate_wpm(text, time_taken):
    words = text.split()
    num_words = len(words)
    minutes = time_taken / 60
    wpm = num_words / minutes if minutes > 0 else 0
    return round(wpm)

def main():
    st.title("Typing Test Game")

    st.write("Welcome to the Typing Test Game! Type the following text as fast as you can within 30 seconds:")

    text_to_type = "GeeksforGeeks is a popular online platform that provides a wide variety of resources for computer science students, professionals, and enthusiasts. It is primarily focused on computer science topics such as algorithms, data structures, programming languages, software engineering, system design, and more."
    st.write(text_to_type)

    # Increase height for larger content
    user_input = st.text_area("Type here:", height=200)

    start_time = time.time()
    timer_placeholder = st.empty()  # Placeholder for timer
    timer = 30
    while timer > 0:
        timer = 30 - int(time.time() - start_time)
        timer_placeholder.write("Time remaining: {} seconds".format(timer))
        time.sleep(1)

    time_taken = time.time() - start_time
    wpm = calculate_wpm(user_input, time_taken)

    st.write("Time's up!")
    st.write("Your typing speed is: {} WPM".format(wpm))


if __name__ == "__main__":
    main()

Run the Server

To run the server use the below command.

streamlit run main.py

Output


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads