Skip to content

Instantly share code, notes, and snippets.

@jwebgordon
Created October 12, 2016 01:49
Show Gist options
  • Save jwebgordon/c3effff6162b6e4f30e0ee772f502eca to your computer and use it in GitHub Desktop.
Save jwebgordon/c3effff6162b6e4f30e0ee772f502eca to your computer and use it in GitHub Desktop.
Hello!
Flask Tutorial
------------------------
**Dev Setup**
1) Install iTerm2 on your mac
2) Open up iTerm, and check your python version (should be 2.7) by typing
`python -V`
3) Install pip with `sudo easy_install pip`
**Flask Setup**
1) Install flask with `sudo pip install flask`
2) Create a new directory somewhere in your drive `mkdir hackday`
3) Navigate into that folder in your terminal `cd hackday`
4) Create a file called app.py `touch app.py`
5) Open up your directory in your text editor of choice (I use Sublime Text)
**App setup**
Import the flask module and create an app using Flask as shown:
```python
from flask import Flask
app = Flask(__name__)
```
Now define the basic route "/" and its corresponding request handler:
```python
@app.route("/")
def main():
return "Welcome!"
```
Next, check if the executed file is the main program (add this at the bottom of the file) and run the app:
```python
if __name__ == "__main__":
app.run()
```
Save the changes and execute app.py in your terminal:
```python
python app.py
```
Point your browser to http://localhost:5000/ and you should have the welcome message.
**Render a webpage**
So that was pretty cool. Now let's go for an actual webpage.
Flask looks for template files inside the templates folder. So navigate to your app folder and create a folder called `templates`. Inside `templates`, create a file called `index.html`. Open up `index.html` and add some HTML to create a webpage. **Make sure to add your HS Tracking code to the page!**
Then open app.py back up, and update your import statement at the top to say:
```python
from flask import Flask, render_template
```
Modify the main method to return the rendered template file.
```python
def main():
return render_template('index.html')
```
Save the changes and restart the server. Point your browser to http://localhost:5000/ and you should have your HTML page.
**Add a form**
Ok so now add a form to your page, and modify the `method` attribute of the `<form>` element to be `method="POST"` and update the `action` to point to a URL in your app, e.g. `action="http://localhost:5000/form_submit"`
Now when we submit the form, it will be sent to the application, but where will the data go???
**Process the form submissions**
Ok so now we need to setup the app to actually receive that form submission and deal with it. I'll get you started here.
First, we need to import a couple of other modules. Let's install those first. Back in your terminal run:
```
sudo pip install requests
sudo pip install simplejson
```
And then add those modules to your imports at the top of `app.py`
```python
import requests, simplejson
from flask import Flask, render_template, request
```
So the first route that we created (`@app.route("/")`) handles a GET requests to the root ("/"). Now what we want is a route that handles a POST request to "/form_submit"
```python
@app.route('/form_submit', methods=['POST'])
def form_submit():
# Add code here
```
So now when the form is submitted, it will make a POST request to that URL, and this function will be triggered. Next, let's take a look at the data that the form sends through. To do this we'll just print the data to the console.
```python
@app.route('/form_submit', methods=['POST'])
def form_submit():
print request
```
Now, submit the form, and check your terminal to see the request data. The two pieces you'll really need from this are `form` and `cookies`.
Try getting specific cookies and fields from those objects, and printing just those pieces.
```python
@app.route('/form_submit', methods=['POST'])
def form_submit():
first_name = request.form['firstname']
#this assumes your field name for the first name is 'firstname'
print first_name
hsutk = request.cookies['hubspotutk']
print hsutk
```
OK, so now you can get specific values from the form submission, and store them in variables. Now you'll *just* need to figure out how to send those to HubSpot via the Forms API. You'll need the Forms API documentation, along with
- The `requests` docs: http://docs.python-requests.org/en/master/
- The `simplejson` docs: https://simplejson.readthedocs.io/en/latest/
Have at it!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment