3
\$\begingroup\$

This is an app that generates random statement types, and basic conjunctions. How to improve its readability and function? How to deploy this code app online? This is the GitHub repository: https://github.com/attilavjda/shiny-goggles

#paragraph_builder.py
from flask import Flask, render_template
from random import choice
app = Flask(__name__)
statement_type = ["question", "exclamation", "statement", "implication"]
basic_conjunctions = ["because", "but", "so"]
@app.route("/new")
def new_output():
 return home()
@app.route("/")
def home():
 output = f"""
 Statement type: {choice(statement_type)}
 Basic conjunction: {choice(basic_conjunctions)}
 """
 return render_template("index.html", output=output)
if __name__ == "__main__":
 app.run(debug=True)
<!-- /templates/index.html -->
<pre>{{ output }}</pre>
<button onclick="window.location.href='/'">New Term</button>
<script>
 const btn = document.querySelector("button");
 btn.addEventListener("click", () => {
 fetch("/");
 });
</script>
asked Dec 21, 2023 at 15:48
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Vercel has a free tier which you can use to deploy online using their website.

As far as the code goes. It's too small to review.

There are two nitpicks though. As per PEP-8 global constants must be capitalized. So, statement_type should be STATEMENT_TYPE. It's always good to have a main() function to disambiguate scope. But you are not going to use this as a package so it is fine.

Apart from that you are doing all the right things like using multiline text, and using f-strings to interpolate strings.

If you have something more substantial then a more informative review can be done.

answered Dec 21, 2023 at 21:29
\$\endgroup\$
2
\$\begingroup\$

statement_type and basic_conjunctions should be immutable tuples and not lists.

Routes for / and /new can be added to a single function, so do that.

Not a good choice to use a pre with fields, nor is it a good idea to perform output formatting in the web control code. Instead, break this out to something more semantically well-structured, like a dl.

Your HTML markup is invalid. It needs the typical html, etc. tags.

I don't see a need for your <script>; delete it and just make the button refresh the current page.

Suggested

from flask import Flask, render_template
from random import choice
app = Flask(__name__)
statement_type = ('question', 'exclamation', 'statement', 'implication')
basic_conjunctions = ('because', 'but', 'so')
@app.route('/')
@app.route('/new')
def home():
 return render_template(
 'index.html',
 statement_type=choice(statement_type),
 basic_conjunction=choice(basic_conjunctions),
 )
if __name__ == '__main__':
 app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Paragraph Structure</title>
</head>
<body>
<dl>
 <dt>Statement type:</dt>
 <dd>{{statement_type}}</dd>
 <dt>Basic conjunction:</dt>
 <dd>{{basic_conjunction}}</dd>
</dl>
<button onclick="window.location.reload();">New Term</button>
</body>
</html>
answered Dec 21, 2023 at 23:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.