Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/
Pipfile.lock
images/*.csv
9 changes: 8 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ verify_ssl = true
[packages]
matplotlib = "*"
flask = "*"
mypy = "*"
flake8 = "*"
isort = "*"
black = "*"

[requires]
python_version = "3.6"
python_version = "3.9"

[pipenv]
allow_prereleases = true
201 changes: 0 additions & 201 deletions Pipfile.lock

This file was deleted.

24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Open http://127.0.0.1:5000/, draw a character, click on Send and watch your term

<img src="images/out.png" alt="image-20191206022811472" style="zoom:33%;" />



### Introduction

In this project I am experimenting with sending data between Javascript and Python using the web framework Flask. Additionally I will use matplotlib to generate a dynamic graph based on the provided user input data.
Expand All @@ -33,14 +31,14 @@ In this project I am experimenting with sending data between Javascript and Pyth

### Important bits

Send the `outputData` from Javascript to Python with a POST call to postmethod and use the form variable canvas_data. The POST call give a response from Python and the page is redirected to the results page with the given uuid.
Send the `outputData` from Javascript to Python with a POST call to postmethod and use the form variable canvas_data. The POST call give a response from Python and the page is redirected to the results page with the given unique ID.

```javascript
...
$.post( "/postmethod", {
canvas_data: JSON.stringify(outputData)
}, function(err, req, resp){
window.location.href = "/results/"+resp["responseJSON"]["uuid"];
window.location.href = "/results/"+resp["responseJSON"]["unique_id"];
});
...
```
Expand All @@ -49,11 +47,11 @@ Retrieve the `canvas_data` from the POST request and write the content to a file

```python
...
@app.route('/postmethod', methods = ['POST'])
@app.route('/postmethod', methods=['POST'])
def post_javascript_data():
jsdata = request.form['canvas_data']
unique_id = create_csv(jsdata)
params = { 'uuid' : unique_id }
params = {'unique_id': unique_id }
return jsonify(params)
...
```
Expand Down Expand Up @@ -87,10 +85,10 @@ def index():
return render_template('layouts/index.html',
title=title)

@app.route('/results/<uuid>', methods=['GET'])
def results(uuid):
@app.route('/results/<unique_id>', methods=['GET'])
def results(unique_id):
title = 'Result'
data = get_file_content(uuid)
data = get_file_content(unique_id)
return render_template('layouts/results.html',
title=title,
data=data)
Expand All @@ -99,7 +97,7 @@ def results(uuid):
def post_javascript_data():
jsdata = request.form['canvas_data']
unique_id = create_csv(jsdata)
params = { 'uuid' : unique_id }
params = { 'unique_id' : unique_id }
return jsonify(params)

@app.route('/plot/<imgdata>')
Expand All @@ -123,8 +121,8 @@ def create_csv(text):
file.write(text[1:-1]+"\n")
return unique_id

def get_file_content(uuid):
with open('images/'+uuid+'.csv', 'r') as file:
def get_file_content(unique_id):
with open('images/'+unique_id+'.csv', 'r') as file:
return file.read()

if __name__ == '__main__':
Expand Down Expand Up @@ -199,7 +197,7 @@ $( document ).ready(function() {
$.post( "/postmethod", {
canvas_data: JSON.stringify(outputData)
}, function(err, req, resp){
window.location.href = "/results/"+resp["responseJSON"]["uuid"];
window.location.href = "/results/"+resp["responseJSON"]["unique_id"];
});
}

Expand Down
Loading