Skip to content

Commit f407e32

Browse files
committed
Tweaks from run-through
1 parent a7f4c65 commit f407e32

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

-3.48 KB
Loading

turning-a-script-into-a-website.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,29 @@ the theory is the same.
6767

6868
### Step 2: create a website
6969

70+
Firstly, [create a PythonAnywhere account](https://www.pythonanywhere.com/pricing/) if you haven't already. A free "Beginner" account is enough for this tutorial.
71+
72+
Once you've signed up, you'll be taken to the dashboard, with a tour window. It's worth going through
73+
the tour so that you can learn how the site works -- it'll only take a minute or so.
74+
75+
<img width="500" src="/static/images/flask-tutorial-signed-in.png">
76+
77+
At the end of the tour you'll be presented with some options to "learn more". You can just click "End tour" here,
78+
because this tutorial will tell you all you need to know.
79+
80+
<img width="500" src="/static/images/flask-tutorial-tour-learn-more.png">
81+
82+
Now you're presented with the PythonAnywhere dashboard. I recommend you check your email and confirm your email address --
83+
otherwise if you forget your password later, you won't be able to reset it.
84+
85+
<img width="500" src="/static/images/flask-tutorial-initial-dashboard.png">
86+
87+
Now you need to create a website, which requires a web framework.
7088
The easiest web framework to get started with when creating this kind of thing is
7189
[Flask](http://flask.pocoo.org/); it's very simple and doesn't have a lot of the built-in
7290
stuff that other web frameworks have, but for our purposes that's a good thing.
7391

74-
Assuming that you already have a PythonAnywhere account, log in and go to the "Web" page:
92+
To create your site, go to the "Web" page using the tab near the top right:
7593

7694
<img width="500" src="/static/images/flask-tutorial-web-tab-empty.png">
7795

@@ -97,7 +115,7 @@ PythonAnywhere has various versions of Python installed, and each version has it
97115

98116
<img width="500" src="/static/images/flask-tutorial-create-web-app-choose-location.png">
99117

100-
This page is asking you where you want to put your code. Code on PythonAnywhere is stored in your home directory, `/home/`*yourusername*, and in its subdirectories. Now, Flask is a particularly lighweight framework, and you can write a simple Flask app in a single file. PythonAnywhere is asking you where it should create a directory and put a single file with a really really simple website. The default should be fine; it will create a subdirectory of your home directory called `mysite` and then will put the Flask code into a file called `flask_app.py` inside that directory.
118+
This page is asking you where you want to put your code. Code on PythonAnywhere is stored in your home directory, `/home/`*yourusername*, and in its subdirectories. Flask is a particularly lightweight framework, and you can write a simple Flask app in a single file. PythonAnywhere is asking you where it should create a directory and put a single file with a really really simple website. The default should be fine; it will create a subdirectory of your home directory called `mysite` and then will put the Flask code into a file called `flask_app.py` inside that directory.
101119

102120
*(It will overwrite any other file with the same name, so if you're _not_ using a new PythonAnywhere account, make sure that the file that it's got in the "Path" input box isn't one of your existing files.)*
103121

@@ -166,7 +184,7 @@ function of its own. It's generally a good idea to keep the web app code -- th
166184
display pages -- from the more complicated processing code (after all, if we were doing the stock analysis
167185
example rather than this simple add-two-numbers script, the processing could be thousands of lines long).
168186

169-
So, we'll create a new file for our processing code. Go back to the browser tab that's showing your editor page; you'll see
187+
So, we'll create a new file for our processing code. Go back to the browser tab that's showing your editor page; up at the top, you'll see
170188
"breadcrumb" links showing you where the file is stored. They'll be a series of directory names separated
171189
by "/" characters, each one apart from the last being a link.
172190
The last one, just before the name of the file containing your Flask code,
@@ -299,7 +317,7 @@ numbers, and click the "Do calculation" button, and you'll get... an incomprehen
299317
<img width="500" src="/static/images/flask-tutorial-method-not-allowed.png">
300318

301319
Well, perhaps not entirely incomprehensible. It says "method not allowed". Previously we were
302-
using the "get" method to get our page, but we told the form that it should use the post message
320+
using the "get" method to get our page, but we just told the form that it should use the "post" method
303321
when the data was submitted. So Flask is telling us that it's not going to allow that page to
304322
be requested with the "post" method.
305323

@@ -354,7 +372,7 @@ returns the multi-line string with this:
354372
<body>
355373
{errors}
356374
<p>Enter your numbers:
357-
<form>
375+
<form method="post" action=".">
358376
<p><input name="number1" /></p>
359377
<p><input name="number2" /></p>
360378
<p><input type="submit" value="Do calculation" /></p>
@@ -453,7 +471,7 @@ However, if you get completely stuck, here's the code you should currently have:
453471
return '''
454472
<html>
455473
<body>
456-
<p>{result}</p>
474+
<p>The result is {result}</p>
457475
<p><a href="/">Click here to calculate again</a>
458476
</body>
459477
</html>
@@ -654,7 +672,7 @@ with comments after each block of code. Starting off:
654672
app = Flask(__name__)
655673
app.config["DEBUG"] = True
656674

657-
All that is just copied from the last program.
675+
All that is just copied from the previous website.
658676

659677
inputs = []
660678

@@ -703,7 +721,7 @@ So, if the "Calculate number" button was the one that the user clicked...
703721
'''.format(result=result)
704722

705723
...we do the calculation and return the result (clearing the list of the inputs at the same
706-
time so that the user can try again with another list):
724+
time so that the user can try again with another list).
707725

708726
If, however, we get past that `if request.form["action"] == "Calculate number"` statement, it means either that:
709727

@@ -774,7 +792,7 @@ What this all means is that **global variables don't work for storing state in w
774792
On each server that's running to control your site, everyone will see the same global variables.
775793
And if you have multiple servers, then each one will have a different set of global variables.
776794

777-
What do do?
795+
What to do?
778796

779797
## Sessions to the rescue!
780798

@@ -1011,7 +1029,8 @@ Firstly, we put our calculationg routine into `processing.py`, as normal:
10111029

10121030
Again, we'll go through that line-by-line in a moment (though it's worth noting that
10131031
although this feels like something that should be much harder than the first case, the
1014-
Flask app is much shorter :-) But let's try it out first -- we visit the page:
1032+
Flask app is much shorter :-) But let's try it out first -- once you've saved the
1033+
code on PythonAnywhere and reloaded the site, visit the page:
10151034

10161035
<img src="/static/images/script-to-webapp-file-processing-start-page.png">
10171036

@@ -1049,7 +1068,7 @@ This is our normal Flask setup code.
10491068
@app.route("/", methods=["GET", "POST"])
10501069
def file_summer_page():
10511070

1052-
As usual, we define a view
1071+
As usual, we define a view.
10531072

10541073
if request.method == "POST":
10551074

0 commit comments

Comments
 (0)