Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dean fitzgerald - web calc #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 1 addition & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1 @@
# Web calculator

## Description

Create a simple web-based calculator using Flask.

## Objectives

### Learning Objectives

After completing this assignment, you should understand:

* The very basics of making a web application

### Performance Objectives

After completing this assignment, you should be able to:

* Create a Flask app

## Details

### Deliverables

* A Git repo called web-calculator containing at least:
* `README.md` file explaining how to run your project
* a `requirements.txt` file
* a suite of tests for your project

### Requirements

* Passing unit tests
* No PEP8 or Pyflakes warnings or errors

## Normal Mode

Create a simple web application that works as a calculator. The first page, which you get via a GET request, should have a form with two text boxes, a dropdown box for the operation (+, -, *, /) and a button to calculate.

The form uses POST to go to a second screen which shows the results of your calculation.

## Hard Mode

In addition to the requirements from **Normal Mode**:

1. The form should also appear on the second page -- the calculation results page. The first operand should be filled in with the results of the previous calculation.

2. Add [Foundation](http://foundation.zurb.com/) or [Bootstrap](http://getbootstrap.com/) to your application to make it look nicer.

## Additional Resources

* [The main Flask site](http://flask.pocoo.org/)
* [Explore Flask](https://exploreflask.com/)
* [Full Stack Python](http://www.fullstackpython.com/)
Enter a numerical value into each of the empty boxes and choose an operator from the drop-down menu. Click the "Calculate" button to see the result.
26 changes: 26 additions & 0 deletions templates/calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE HTML>

<html>
<head>
<title>Web Calculator</title>
<head>
<body>
<div>
<h1>Calculator</h1>
<form action="/result.html" method="POST">
<input type="text" name="value1">
<form>
<div>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<div>
<form action="." method="POST">
<input type="text" name="value2">
</form>
<div>
<button type="submit">Calculate!</button>
</form>
28 changes: 28 additions & 0 deletions templates/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE HTML>

<html>
<head>
<title>Web Calculator</title>
<head>
<body>
<div>
<h1>Calculator</h1>
<h3>Your result is:</h>
<p1> {{ result }} </p>
<form action="/result" method=['GET', "POST"]>
<input type="text" name="value1" value=""{{ result }}"">
<form>
<div>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<div>
<form action="." method="POST">
<input type="text" name="value2">
</form>
<div>
<button type="submit">Calculate!</button>
</form>
21 changes: 21 additions & 0 deletions web_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def home():
return render_template('calculator.html')

@app.route('/calculator.html', methods=['POST', 'GET'])
def calculate():
value1 = request.form['value1']
value2 = request.form['value2']
operator = request.form['operator']
result = eval(value1 + operator + value2)

return render_template('result.html'), result



if __name__ == "__main__":
app.run(debug=True)