from flask import Flask, render_template, jsonify


app = Flask(__name__)
app.config.update(
    JSONIFY_PRETTYPRINT_REGULAR=False,
)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/api/search/<fragment>')
def search(fragment):
    results = []
    with open('typesetter/data/words.txt') as words:
        for word in words:
            word = word.strip('\n')
            if fragment in word:
                results.append({
                    'word': word,
                    'category': classify(word),
                })

    return jsonify(results)


def classify(word):
    length = len(word)

    if length < 7:
        return 'short'
    elif length < 10:
        return 'medium'
    else:
        return 'long'

from flask import Flask, render_template, jsonify


app = Flask(__name__)
app.config.update(
    JSONIFY_PRETTYPRINT_REGULAR=False,
)

# Read in the entire wordlist at startup and keep it in memory.
# Optimization for improving search response time.
with open('typesetter/data/words.txt') as f:
    WORDS = f.read().split('\n')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/api/search/<fragment>')
def search(fragment):
    results = []

    for word in WORDS:
        if fragment in word:
            results.append({
                'word': word,
                'category': classify(word),
            })

    return jsonify(results)


def classify(word):
    length = len(word)

    if length < 7:
        return 'short'
    elif length < 10:
        return 'medium'
    else:
        return 'long'

Reduce search response time by keeping wordlist in memory
Reduce search response time by keeping wordlist in memory

  from flask import Flask, render_template, jsonify
  
  
  app = Flask(__name__)
  app.config.update(
      JSONIFY_PRETTYPRINT_REGULAR=False,
  )
+ 
+ # Read in the entire wordlist at startup and keep it in memory.
+ # Optimization for improving search response time.
+ with open('typesetter/data/words.txt') as f:
+     WORDS = f.read().split('\n')
  
  
  @app.route('/')
  def index():
      return render_template('index.html')
  
  
  @app.route('/api/search/<fragment>')
  def search(fragment):
      results = []
+ 
+     for word in WORDS:
-     with open('typesetter/data/words.txt') as words:
-         for word in words:
-             word = word.strip('\n')
-             if fragment in word:
+         if fragment in word:
-                 results.append({
+             results.append({
-                     'word': word,
+                 'word': word,
-                     'category': classify(word),
+                 'category': classify(word),
-                 })
+             })
  
      return jsonify(results)
  
  
  def classify(word):
      length = len(word)
  
      if length < 7:
          return 'short'
      elif length < 10:
          return 'medium'
      else:
          return 'long'
  
Reduce search response time by keeping wordlist in memory
## Code Before:
from flask import Flask, render_template, jsonify


app = Flask(__name__)
app.config.update(
    JSONIFY_PRETTYPRINT_REGULAR=False,
)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/api/search/<fragment>')
def search(fragment):
    results = []
    with open('typesetter/data/words.txt') as words:
        for word in words:
            word = word.strip('\n')
            if fragment in word:
                results.append({
                    'word': word,
                    'category': classify(word),
                })

    return jsonify(results)


def classify(word):
    length = len(word)

    if length < 7:
        return 'short'
    elif length < 10:
        return 'medium'
    else:
        return 'long'

## Instruction:
Reduce search response time by keeping wordlist in memory
## Code After:
from flask import Flask, render_template, jsonify


app = Flask(__name__)
app.config.update(
    JSONIFY_PRETTYPRINT_REGULAR=False,
)

# Read in the entire wordlist at startup and keep it in memory.
# Optimization for improving search response time.
with open('typesetter/data/words.txt') as f:
    WORDS = f.read().split('\n')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/api/search/<fragment>')
def search(fragment):
    results = []

    for word in WORDS:
        if fragment in word:
            results.append({
                'word': word,
                'category': classify(word),
            })

    return jsonify(results)


def classify(word):
    length = len(word)

    if length < 7:
        return 'short'
    elif length < 10:
        return 'medium'
    else:
        return 'long'
