<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alyssa Liguori on Medium]]></title>
        <description><![CDATA[Stories by Alyssa Liguori on Medium]]></description>
        <link>https://medium.com/@alyssa.liguori?source=rss-c16c7ad8b1e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*6A7clzLIgIEuMKt7ro8lpw.png</url>
            <title>Stories by Alyssa Liguori on Medium</title>
            <link>https://medium.com/@alyssa.liguori?source=rss-c16c7ad8b1e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:37:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alyssa.liguori/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploying a predictive python ML model with Flask and Heroku: Part 3]]></title>
            <link>https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-3-afc877794957?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/afc877794957</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-app-development]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Mon, 23 Dec 2019 18:06:55 GMT</pubDate>
            <atom:updated>2019-12-23T18:06:55.693Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, we’ll set up the model to intake the user’s form inputs, generate a prediction, and serve that prediction to the user via html-jinja.</p><p>Check <a href="https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-1-54996bb07577">Part 1</a> and <a href="https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-2-d272d55dc117?source=your_stories_page---------------------------">Part 2</a> to catch up to speed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3HWHO5sJnfiYByt4jtrWEA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@nate_dumlao?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Nathan Dumlao</a> on <a href="https://unsplash.com/s/photos/machine?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Navigate to your app.py file. It looks like this currently:</p><pre>from flask import Flask<br>app = Flask(__name__)</pre><pre>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;])<br>def test():<br>    return &#39;Hello World!&#39;</pre><p>In place of the function <strong>test</strong>, we will declare a function called <strong>render_main </strong>which will display main.html when a user makes a GET request to the server (goes to our web app’s URL).</p><pre>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;])<br>def render_main():<br>    return flask.render_template(&#39;main.html&#39;)</pre><p>The <strong>render_main</strong> function calls flask’s <strong>render_template</strong> method and passes it the argument <strong>main.html</strong>. When a user navigates to this URL, this method will find our html file, generate a jinja2 template object from it, and return it to the browser.</p><p>Now, we will test render_main. In your terminal, in the root of your web app repo, execute the command <strong>flask run</strong> then open your browser to your localhost. When the server starts up, you should see your form in the browser. If you do not see it, go back to the terminal to check for error messages.</p><p>Next, let’s add a second decorator for POST requests made to the server. We also declare a function <strong>make_prediction</strong> that will:</p><ol><li>Get values from the form fields we need</li><li>Run user’s through our model</li><li>Return a prediction to the user</li></ol><p>Below, we do #1 and test it by printing the submitted values to the terminal.</p><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;]<br>def make_prediction():<br>    res_age = request.form.get(&#39;age&#39;)<br>    res_sex = request.form.get(&#39;sex&#39;)<br>    res_cheese = request.form.get(&#39;cheese&#39;)<br>    res_tj = request.form.get(&#39;milk1&#39;)<br>    res_silk = request.form.get(&#39;milk2&#39;)<br>    res_zen = request.form.get(&#39;milk3&#39;)<br>    res_wf = request.form.get(&#39;milk4&#39;)<br>    print(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf)</pre><p>Next, we call <strong>generate_prediction</strong> from within the <strong>make_prediction</strong> function. But what is generate_prediction?</p><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;]<br>def make_prediction():<br>    res_age = request.form.get(&#39;age&#39;)<br>    res_sex = request.form.get(&#39;sex&#39;)<br>    res_cheese = request.form.get(&#39;cheese&#39;)<br>    res_tj = request.form.get(&#39;milk1&#39;)<br>    res_silk = request.form.get(&#39;milk2&#39;)<br>    res_zen = request.form.get(&#39;milk3&#39;)<br>    res_wf = request.form.get(&#39;milk4&#39;)<br>    print(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf)<br>    prediction = generate_prediction(res_age, res_sex, res_cheese, res_tj,     res_silk, res_zen, res_wf)<br></pre><p>Generate_prediction is the name you will give to your ML model that will be stored in a second py file. Let’s take a moment to set that up and import it into app.py so that our <strong>make_prediction</strong> function runs.</p><p>Go to your terminal and create a second py file. You can name it scripts.py. Take a moment to import all the necessary libraries needed to run your ML model. For example, you might import pandas, numpy, sklearn, etc. Also, be sure to import joblib as that will allow us to pipeline our model from our separate data-science repo.</p><p>Now, navigate to the separate repo you used to build your model. Import joblib then add the code to pickle your model. Here’s an example of what it might look like:</p><pre>joblib.dump(clf, &#39;../src/models/clf.pkl&#39;, compress=1)</pre><p>The first argument is the name of the model that we want to pickle for use in our web app. The second argument is the filepath where we want the pickle file to be saved. The third argument is the keyword argument compress and we have set its value to 1. Read the <a href="https://joblib.readthedocs.io/en/latest/generated/joblib.dump.html">joblib docs</a> for more information.</p><p>Now, copy the clf.pkl file and paste it into your web app repo manually. Navigate back to the scripts.py file and load clf:</p><pre>clf = joblib.load(&#39;./clf.pkl&#39;)</pre><p>Here’s an example of what your scripts.py file could look like:</p><pre>[import libraries here]<br>import joblib<br>from sklearn.linear_model import LogisticRegression</pre><pre>def generate_prediction(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf):<br>    vals = [res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf]<br>    clf = joblib.load(&#39;./clf.pkl&#39;)<br>    prediction = clf.predict(vals)<br>    return prediction</pre><p>It is likely that you will need at least one additional function in this scripts.py file. That function will convert the input values to a format that is usable by your ML model. You can declare and define the cleaning function in this file but then be sure to call it within generate_prediction because our web app only calls that function.</p><p>Back to our app.py file. When we last left off, it looked like this:</p><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;]<br>def make_prediction():<br>    res_age = request.form.get(&#39;age&#39;)<br>    res_sex = request.form.get(&#39;sex&#39;)<br>    res_cheese = request.form.get(&#39;cheese&#39;)<br>    res_tj = request.form.get(&#39;milk1&#39;)<br>    res_silk = request.form.get(&#39;milk2&#39;)<br>    res_zen = request.form.get(&#39;milk3&#39;)<br>    res_wf = request.form.get(&#39;milk4&#39;)<br>    print(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf)<br>    prediction = generate_prediction(res_age, res_sex, res_cheese, res_tj,     res_silk, res_zen, res_wf)</pre><p>Now, let’s import generate_prediction at the top.</p><pre>from scripts import generate_prediction </pre><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;]<br>def make_prediction():<br>    res_age = request.form.get(&#39;age&#39;)<br>    res_sex = request.form.get(&#39;sex&#39;)<br>    res_cheese = request.form.get(&#39;cheese&#39;)<br>    res_tj = request.form.get(&#39;milk1&#39;)<br>    res_silk = request.form.get(&#39;milk2&#39;)<br>    res_zen = request.form.get(&#39;milk3&#39;)<br>    res_wf = request.form.get(&#39;milk4&#39;)<br>    print(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf)<br>    prediction = generate_prediction(res_age, res_sex, res_cheese, res_tj,     res_silk, res_zen, res_wf)</pre><p>Lastly, let’s set complete # 3 and return a prediction to the user.</p><pre>from scripts import generate_prediction</pre><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;]<br>def make_prediction():<br>    res_age = request.form.get(&#39;age&#39;)<br>    res_sex = request.form.get(&#39;sex&#39;)<br>    res_cheese = request.form.get(&#39;cheese&#39;)<br>    res_tj = request.form.get(&#39;milk1&#39;)<br>    res_silk = request.form.get(&#39;milk2&#39;)<br>    res_zen = request.form.get(&#39;milk3&#39;)<br>    res_wf = request.form.get(&#39;milk4&#39;)<br>    print(res_age, res_sex, res_cheese, res_tj, res_silk, res_zen, res_wf)<br>    prediction = generate_prediction(res_age, res_sex, res_cheese, res_tj,     res_silk, res_zen, res_wf)<br>    return render_template(&#39;res.html&#39;, prediction=prediction)</pre><p>Again, we used the render_template method from flask and passed it the argument of the output HTML file and the jinja2 variable <strong>prediction </strong>which is set equal to the value returned when we call <strong>generate_prediction</strong>. It’s a good time to edit our res.html file so we can see where <strong>prediction</strong> is going to be passed.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&#39;en&#39;&gt;<br>    &lt;head&gt;<br>        &lt;meta charset=&#39;utf-8&#39; name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;<br>        &lt;title&gt;Results&lt;/title&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;div&gt;<br>            &lt;p&gt;Based on your responses...&lt;/p&gt;<br>            &lt;p&gt;The model predicted ...&lt;/p&gt;<br>            &lt;p&gt;&lt;b&gt;{{prediction}}&lt;/b&gt;&lt;/p&gt;<br>        &lt;/div&gt;<br>     <br>    &lt;/body&gt;<br>&lt;/html&gt;</pre><p>At this point, all of the functionality of the app will be working. Be sure to test it out and troubleshoot paying special attention to the format of your arguments.</p><p>Next post, we’ll deploy the model to Heroku so others can see your work and interact with the model!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afc877794957" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying a predictive python ML model with Flask and Heroku (Part 2)]]></title>
            <link>https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-2-d272d55dc117?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d272d55dc117</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Wed, 18 Dec 2019 16:05:14 GMT</pubDate>
            <atom:updated>2019-12-23T18:47:11.796Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploying a predictive python ML model with Flask and Heroku: Part 2</h3><h3>In this post, we will create a form to get user inputs that will later be fed into our model.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XEnEE7B_IYvKobT0vmaG7A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@euwars?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Farzad Nazifi</a> on <a href="https://unsplash.com/collections/385548/work-and-collaboration?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In <a href="https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-1-54996bb07577">Part 1</a>, we created a repo, a virtual environment, installed flask and gunicorn and set up a starter app.py file that looks like this:</p><pre>from flask ipmort Flask <br>app = Flask(__name__)</pre><pre>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;])<br>def test():<br>    return &#39;Hello World&#39; </pre><p>We are able to view ‘Hello World’ on our localhost when we execute this command:</p><pre>flask run </pre><p>Now, we have our app working in its most basic form. Next, let’s add two jinja2-html templates. How do we name these? Jinja2 does not have a default file extension so we will just use .html as our file extension. You can use a different one, for example .jinja. Let’s also create a folder called <strong>templates </strong>to store them. In the root of your app, create the <strong>templates</strong> directory then move into that to create a main.html and res.html file.</p><pre>mkdir templates<br>cd templates<br>touch main.html res.html</pre><h4>What is the purpose of the jinja-html files?</h4><p>The main.html file will be displayed when someone navigates to the app’s URL. It will include a form. The user will complete this form and submit it to be run through the model. The res.html file will display the model’s output. For example, if you were predicting whether a person was affiliated with the Republican or Democratic party, you would display the model’s prediction of “Republican” or “Democratic” on res.html. This model output is passed to the file as a jinja variable which is the reason we are using a jinja2-html template rather than basic HTML.</p><h4>How do we create our HTML5 form with fields for user responses?</h4><p>Open main.html. Start by declaring the doctype and setting up the scaffolding. Add an HTML5 template like this:</p><pre>&lt;!DOCTYPE html&gt;</pre><pre>&lt;html lang=&quot;en&quot;&gt;</pre><pre>&lt;head&gt;</pre><pre>&lt;meta charset=&quot;UTF-8&quot;&gt;</pre><pre>&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;</pre><pre>&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;</pre><pre>&lt;title&gt;Document&lt;/title&gt;</pre><pre>&lt;/head&gt;</pre><pre>&lt;body&gt;</pre><pre>&lt;/body&gt;</pre><pre>&lt;/html&gt;</pre><p>If you are using VSCode as your text editor, you can generate this template by typing <strong>html:5 </strong>on the first line of the blank file and then clicking <strong>enter</strong>. Alternatively, you can manually type it.</p><p>Within the body tags, add form tags. Within the form tags, add fieldset tags to contain a group of fields. Our form will have two fieldset tag pairs. The first will house the inputs related to demographic information and the second will house inputs related to brand preferences.</p><p>Here’s the code that I added <em>within the body tags</em> for the Demographic Info fieldset:</p><pre>&lt;form&gt;</pre><pre>&lt;fieldset&gt;</pre><pre>&lt;legend&gt;Demographic Info&lt;/legend&gt;</pre><pre>&lt;label for=&#39;age&#39;&gt;Age:&lt;/label&gt;</pre><pre>&lt;input type=&#39;number&#39; name=&#39;age&#39; required&gt;</pre><pre>&lt;p&gt;Select one:&lt;/p&gt;</pre><pre>&lt;label for=&#39;female&#39;&gt;Female&lt;/label&gt;</pre><pre>&lt;input type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;female&#39;&gt;</pre><pre>&lt;label for=&#39;male&#39;&gt;Male&lt;/label&gt;</pre><pre>&lt;input  type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;male&#39;&gt;</pre><pre>&lt;/fieldset&gt;</pre><pre>&lt;/form&gt;</pre><p>Let’s walk through it. Within the form tags, I have opening and closing fieldset tags. Within the fieldset tags, I have a legend that has the title of the container. Since these questions are about demographics, I have named it <strong>Demographic Info</strong>. Next, I have label tags for age followed by an input tag. After that, there is a p tag which allows me to add instructions to the user followed by two pairs of label and input tags.</p><p>The functionality of the form is created by the input tags. When we specify the attribute <strong>type </strong>equal to the string <strong>number</strong>, we are creating a field for an integer input. The usability of this form for humans is created by the label tags and the p tags. For our app to work, we don’t need those bits but if we don’t include them, the forms is going to be unusable by people.</p><p>In our app.py file, we will write a line of code to grab the <strong>value</strong> of the inputs by calling the <strong>name</strong> attribute. The first input type is <strong>number </strong>so the value that will be passed into our model (or into a data cleaning function prior to being passed into our model) is the number a user enters into that field. It is different for the radio buttons. For the radio buttons, if a user clicks the first button with the label <strong>Female</strong>, the value that will be passed to our model is currently ‘female’ because the value attribute is ‘female.’ Alternatively we could change it to 1 or 0 which might save us a few steps in data cleaning. Let’s do that. See below that I have set the value of female to 1 and the value of male to 0.</p><pre>&lt;form&gt;</pre><pre>&lt;fieldset&gt;</pre><pre>&lt;legend&gt;Demographic Info&lt;/legend&gt;</pre><pre>&lt;label for=&#39;age&#39;&gt;Age:&lt;/label&gt;</pre><pre>&lt;input type=&#39;number&#39; name=&#39;age&#39; required&gt;</pre><pre>&lt;p&gt;Select one:&lt;/p&gt;</pre><pre>&lt;label for=&#39;female&#39;&gt;Female&lt;/label&gt;</pre><pre>&lt;input type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;1&#39;&gt;</pre><pre>&lt;label for=&#39;male&#39;&gt;Male&lt;/label&gt;</pre><pre>&lt;input  type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;0&#39;&gt;</pre><pre>&lt;/fieldset&gt;</pre><pre>&lt;/form&gt;</pre><p>Great, let’s create our second fieldset. This will ask the user about their brand preferences. I have added the second fieldset after the first fieldset so you are able to see the entire form:</p><pre>&lt;form&gt;</pre><pre>&lt;fieldset&gt;</pre><pre>&lt;legend&gt;Demographic Info&lt;/legend&gt;</pre><pre>&lt;label for=&#39;age&#39;&gt;Age:&lt;/label&gt;</pre><pre>&lt;input type=&#39;number&#39; name=&#39;age&#39;&gt;</pre><pre>&lt;p&gt;Select one:&lt;/p&gt;</pre><pre>&lt;label for=&#39;female&#39;&gt;Female&lt;/label&gt;</pre><pre>&lt;input type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;female&#39;&gt;</pre><pre>&lt;label for=&#39;male&#39;&gt;Male&lt;/label&gt;</pre><pre>&lt;input  type=&#39;radio&#39; name=&#39;sex&#39; value=&#39;male&#39;&gt;</pre><pre>&lt;/fieldset&gt;</pre><pre>&lt;fieldset&gt;</pre><pre>&lt;legend&gt; Brand Preferences&lt;/legend&gt;</pre><pre>&lt;label for=&#39;cheese&#39;&gt;Favorite brand of cheese:&lt;/label&gt;</pre><pre>&lt;input type=&#39;text&#39; name=&#39;cheese&#39;&gt;&lt;br&gt;</pre><pre>&lt;p&gt;Which brand(s) of soy milk would you buy?&lt;/p&gt;</pre><pre>&lt;input type=&#39;checkbox&#39; name=&#39;milk1&#39; value=&#39;tj&#39;&gt;Trader Joe&#39;s&lt;br&gt;</pre><pre>&lt;input type=&#39;checkbox&#39; name=&#39;milk2&#39; value=&#39;silk&#39;&gt;Silk&lt;br&gt;</pre><pre>&lt;input type=&#39;checkbox&#39; name=&#39;milk3&#39; value=&#39;zen&#39;&gt;Zensoy&lt;br&gt;</pre><pre>&lt;input type=&#39;checkbox&#39; name=&#39;milk4&#39; value=&#39;wf&#39;&gt;Whole Foods 365&lt;br&gt;</pre><pre>&lt;/fieldset&gt;</pre><pre>&lt;/form&gt;</pre><p>We used a text box for our question about cheese and a set of checkbox options (users can select more than one) for our question about soy milk. We have now covered four input types so you have a good number to select from in creating your form. You can also check <a href="https://www.w3schools.com/html/html_form_input_types.asp">other resources like w3schools</a> to learn about other input types. Quick note — the <strong>br </strong>tags are for line breaks and not needed.</p><p>Now that we’ve created our jinja-html template with our user input fields, what’s next?</p><p>We need a way for the user to submit this information to us. Before we create a submit button that triggers a POST request, we won’t be able to do anything with the user’s answers as we don’t have access to them yet!</p><p>Add this code right before the closing tag of the form:</p><pre>&lt;button type=&#39;submit&#39; value=&#39;Submit&#39; method=&#39;POST&#39;&gt;Submit&lt;/button&gt;</pre><p>The method attribute is POST which will trigger the post request that will pass these inputs to our app Flask object.</p><p>Oh, also go ahead and change the text within the title tags in your head to name the page something other than “Document”:</p><pre>&lt;title&gt;My Survey&lt;/title&gt;</pre><p>You can view this file in your browser to see if you’re happy with how it looks.</p><p>Next, we will define a function in app.py that gets the user responses from this survey and passes them to our data science model! Here’s <a href="https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-3-afc877794957">part 3</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d272d55dc117" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying a predictive python ML model with Flask and Heroku: Part 1]]></title>
            <link>https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-1-54996bb07577?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/54996bb07577</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[web-applications]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Fri, 13 Dec 2019 16:50:10 GMT</pubDate>
            <atom:updated>2019-12-18T16:06:17.694Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploying a predictive python ML model with Flask and Heroku (Part 1)</h3><h3>Virtual environment and flask set up (Part 1)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XuFKgiMkvePkiQ1nnkoceA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@lenneek?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Elena Koycheva</a> on <a href="https://unsplash.com/s/photos/future?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>After you build a data science project, you might want to share it with others. One way to do so is make your GitHub project repository public and share the link. The user would then be able to clone the repo and run your model given a few other pieces are in place. This requires a decent amount of work on their end in understanding and navigating your repo’s organizational structure. Fortunately, there is an easier way to share your model with others, including folks that might not use Github or be interested in the project from a technical perspective.</p><p>This series, starting with this post — Part 1, will take you through deploying your model using Python, Flask, HTML/CSS, and Heroku. I will set up a form to accept multiple inputs into my predictive model. Submission of the form will trigger the model to generate a prediction that will then be passed in a variable to an jinja-html file to be displayed for the user. All of this will be deployed on Heroku so users can experience your model with only your URL and an internet connection.</p><p>Let’s get started.</p><h3>Create a Github repo for your web app.</h3><p>If you have already have a repo with your machine learning model and Jupyter notebooks, I would recommend that you create a different one for your web app. This way, you’ll only push what is part of the web app to Heroku and not the entire data science process of data ingestion, exploration, parameter tuning, etc. Initialize your repo with a license (I use MIT), readme, and python .gitignore file. You don’t necessarily have to use the GitHub GUI to create a repo but that’s my preference. Next, clone the repo to your local environment and navigate to the root.</p><h3>Create a virtual environment.</h3><p>Python3.3 and above comes with a <strong>venv </strong>module to create a virtual environment. In the root of your flask app repo, execute the command:</p><pre>python3 -m venv virtual</pre><p>The <strong>-m</strong> flag runs the <strong>venv</strong> module as a script. Add <strong>virtual </strong>after the module name to create a new directory called virtual. If we now <strong>cd </strong>into <strong>virtual </strong>and <strong>ls</strong>, we will see the following have been created.</p><pre>bin<br>include<br>lib<br>pyenv.cfg</pre><p>To run the virtual environment, run the following (Mac):</p><pre>source bin/activate</pre><p>Why use a virtual environment? To create an isolated environment for application dependencies.</p><h3>Install flask and gunicorn</h3><p>Now that our virtual environment is running, pip install flask and gunicorn.</p><pre>pip install flask gunicorn</pre><h3>Create app.py file for your flask app</h3><p>Navigate to the root directory of your flask app’s repo and create a file called, for our purposes <strong>app.py</strong>. You can name it something else but it will not be automatically detected unless it’s called app.py or wsgi.py when you execute the command <strong>flask run</strong>. To keep it easy, we’ll call it app.py.</p><pre>touch app.py<br>open app.py</pre><h3>Edit app.py</h3><p>You have your app.py file open so now import Flask from flask and instantiate a Flask object. What’s the difference between the titlecase Flask and lowercase flask? The lowercase flask is the code that makes up our web framework. The titlecase Flask is an object we instantiate in order to direct our app to behave in certain ways using our own python code.</p><pre>from flask import Flask</pre><p>Here we instantiate the object Flask and store it as a variable. It usually makes sense to call it app or application. I’ll call it <strong>app</strong>. Why the parameter __name__? The python interpreter assigns __main__ to the main program/file that is being run. For example, if I run app.py, that will be assigned __main__. On the other hand, if I run app.py and I have imported a file into it called scripts.py, the python interpreter will name that scripts (just removing the .py file extension).</p><pre>app = Flask(__name__)</pre><p>Now, let’s employ a decorator. What is a decorator? It is a tool in python that allows us to modify the behavior of a function without modifying the actual function itself. It wraps around a function. Below, we have “@app.route(‘/’, methods=[‘GET’])” which wraps around our <strong>test </strong>function to direct it to be run only when there is a get request from the app’s home page. Notice we’re using our Flask object called app here and calling the function <strong>route</strong> which has a parameter <strong>rule</strong> where we pass the URL where this function should be run. We passed <strong>(‘/’)</strong> for rule. The @ symbol means that for whatever comes below the decorator, that should be passed to the object after the @ symbol. So, we are in effect passing our test function to app.route(). It’s the equivalent of: if the user goes to “www.my-app.herokuapp.com/”, call the function <strong>test:</strong></p><pre>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;])<br>def test():<br>    return &#39;Hello World!&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rgwNv_1gGZYm8eZI-rOJ9Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@martimbraz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Martim Braz</a> on <a href="https://unsplash.com/s/photos/hello-world?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Let’s talk about the argument <strong>methods</strong>=[‘GET’]. I have added it here for illustrative purposes though this is default behavior for <strong>route. </strong>Alternatively, we could change it to ‘POST’ or to [‘GET’, ‘POST’] in which the <strong>test </strong>function would be triggered both with get requests and post requests.</p><p>The <strong>test </strong>function returns “Hello World!” to test that our app is working.</p><p>Now, go to your terminal and execute the following command:</p><pre>flask run </pre><p>If your app is not named app.py, you will need to provide the environment variable for FLASK_APP before the command. For example:</p><pre>FLASK_APP=myapp.py flask run </pre><p>Open your browser and navigate to your localhost <a href="http://127.0.0.1:5000/">http://127.0.0.1:5000/</a>. You should see “Hello World!” in your browser. To terminate the server, use CTRL+C (Mac) in the terminal.</p><p>Now that you have tested your app and see “Hello World!”, we’ll take a break here and end this post. In the next post, we will continue to build out our app.py file, add an additional python file to run our ML model, set up jinja-html templates to ask users for input and return their prediction, and deploy the app to Heroku.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UfaRvNjmkz0FKNdlwMPa-w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@vki?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Viktor Kiryanov</a> on <a href="https://unsplash.com/s/photos/step?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Thank you for reading my post! Please let me know what questions you have.</p><p>Here’s the <a href="https://medium.com/@alyssa.liguori/deploying-a-predictive-python-ml-model-with-flask-and-heroku-part-2-d272d55dc117">link to Part 2 </a>where we create the form for user inputs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54996bb07577" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to set up your AWS EC2 instance and get Jupyter Notebook up and running]]></title>
            <link>https://medium.com/analytics-vidhya/how-to-set-up-your-aws-ec2-instance-and-get-jupyter-notebook-up-and-running-2aead043be76?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2aead043be76</guid>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Fri, 15 Nov 2019 16:31:23 GMT</pubDate>
            <atom:updated>2019-11-25T12:05:16.109Z</atom:updated>
            <content:encoded><![CDATA[<p>Create an EC2 instance, install python3, pip, and python libraries, and get Jupyter Notebook running.</p><ol><li>Create an <a href="https://aws.amazon.com/">AWS Account or login to an existing account</a> and navigate to the <strong>AWS Management Console</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mH0Eg3iy53pDgsGDl06Qag.png" /><figcaption>AWS Management Console</figcaption></figure><p>2. Under <strong>All Services</strong> &gt; <strong>Compute</strong> &gt; Click <strong>EC2</strong></p><p>3. Click <strong>Launch Instance</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*1if9Hft4yLqMi6psSmEqFw.png" /></figure><p>4. You now have 7 steps to creation of your instance.</p><p>Heads up, you will either need to create a public/private key pair or supply an existing key pair in order to launch your instance. From AWS’s <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html">EC2 Key-Pairs documentation </a>— “At boot time, the public key content is placed on the instance in an entry within ~/.ssh/authorized_keys. To log in to your instance, you must specify the private key when you connect to the instance.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WXchHWUqlY0TKXXoWkOeOQ.png" /></figure><p>&gt; Step 1: <strong>Choose AMI</strong>. If you want to learn more, check <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html">this AMI User Guide documentation</a>. To follow my code, use Amazon Linux.</p><p>&gt; Step 2: <strong>Choose Instance Type</strong>. Read more about i<a href="https://aws.amazon.com/ec2/instance-types/">nstance types in this documentation</a>.</p><p>&gt; Step 3: <strong>Configure Instance Details</strong>. Leave defaults in place unless your use case requires tweaks.</p><p>&gt; Step 4: <strong>Add Storage</strong>. The free tier qualifies for up to 30 GB storage. If needed, change the default of 8 GB to 30.</p><p>&gt; Step 5: <strong>Add Tags</strong>. This step is to help you stay organized. Add or leave off tags depending on your preference.</p><p>&gt; Step 6: <strong>Configure Security Group</strong>. Read more about <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-security-groups.html">security groups with AWS to get a good understanding</a>.</p><p>&gt; Step 7: <strong>Review Instance Launch</strong>. Ensure all settings are as you like then click <strong>Launch</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXX9zuOYRQseFVZkxEbIrQ.png" /><figcaption>Step 7: Review Instance Launch</figcaption></figure><p>5. Your instance has launched. Check the status by navigating to the <strong>EC2 Dashboard </strong>(Services &gt; EC2) then click <strong>Instances</strong>. When you see <strong>running</strong> under <strong>Instance State</strong>, proceed to the next step! (You may need to horizontal scroll to see all of the details of your instance).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*sXRiMUtxdIzX9yeDFMTWHQ.png" /><figcaption>EC2 Dashboard to check Instance State</figcaption></figure><p>6. Next to the <strong>Launch Instance</strong> button, click <strong>Connect</strong>. Keep this screen handy, you will need it shortly.</p><p>7. Depending on your machine, either download PuTTY or use the terminal to initiate connection to the SSH (Secure Shell) server. You will need your private key here. <a href="https://www.ssh.com/ssh/">Here’s a quick description of SSH</a>. Refer to the Connect screen mentioned in Step 6. Assuming you have a pem file with your private key in it. Navigate to that directory then use the following code to change the permissions of your pem file then connect to the instance’s public DNS.</p><pre># Here&#39;s an example with a pem file named &quot;my-pem-file&quot; with a private key</pre><pre>chmod 400 &#39;my-pem-file.pem&#39;</pre><pre># Your login is ec2-user if you chose the Amazon Linux AMI during instance creation. After the @ symbol, enter your instance&#39;s Public DNS (IPv4) found on the same page where you found the Instance State </pre><pre>ssh -i &#39;my-pem-file.pem&#39; ec2-user@insert-public-dns-here</pre><p>8. Now that you’ve connected, check the python version on your instance</p><pre>python --version<br>python3 --version</pre><p>9. Amazon Linux only comes with python 2.7 so we will need to install an update. Here’s the command:</p><pre>sudo yum install python3 </pre><p>10. Install pip3</p><pre>curl -O <a href="https://bootstrap.pypa.io/get-pip.py">https://bootstrap.pypa.io/get-pip.py</a><br>python3 get-pip.py</pre><p>11. Install desired python libraries using pip3 (numpy, pandas, jupyter, scikit-learn, matplotlib)</p><p>12. Open the .bashrc file with vim. Add <strong>alias python=python3 </strong>then <strong>:wq</strong> to save and quit vim. Double check it worked.</p><pre>ls -la <br>vim .bashrc<br>python --version # should now print out the python 3.7 instead of 2.7 </pre><p>13. Change your default jupyter kernel to python3 then check the spec list.</p><pre>ipython3 kernelspec install-self<br>jupyter kernelspec list</pre><p>14. To use Jupyter in your browser, go to <strong>EC2</strong> <strong>Dashboard</strong> &gt; <strong>Security Groups</strong> and edit the security group attached to your instance. Add inbound rule: For <strong>Type </strong>use<strong> Custom TCP Rule </strong>and set <strong>Port Range </strong>to <strong>8888</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*UM6y9ij9IkaFVOubw9skaw.png" /><figcaption>AWS Security Groups</figcaption></figure><p>15. Start your jupyter notebook server</p><pre>jupyter notebook — ip=* — allow_origin=* </pre><p>16. Paste your public DNS (IPv4) into your favorite browser followed by <strong>:8888</strong></p><p>17. You will see this screen. Go to your terminal to retrieve the token that printed after you ran…</p><pre>jupyter notebook — ip=* — allow_origin=*</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*fe4VYRg7JkJoTC8fSBSAxQ.png" /><figcaption>Page requesting token in the browser</figcaption></figure><p>and paste token into the token text box in your browser.</p><p>18. Congratulations! You’re ready to start working in your Jupyter Notebook on your new EC2 instance.</p><p>Please let me know what questions you have or if you need help troubleshooting by leaving a comment!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2aead043be76" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/how-to-set-up-your-aws-ec2-instance-and-get-jupyter-notebook-up-and-running-2aead043be76">How to set up your AWS EC2 instance and get Jupyter Notebook up and running</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 Things I learned exploring the FEC’s individual campaign contributions data]]></title>
            <link>https://medium.com/@alyssa.liguori/5-things-i-learned-exploring-the-fecs-individual-campaign-contributions-data-40d2072ffee9?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/40d2072ffee9</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[politics]]></category>
            <category><![CDATA[campaign-finance]]></category>
            <category><![CDATA[data-exploration]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Thu, 14 Nov 2019 15:53:34 GMT</pubDate>
            <atom:updated>2019-11-15T23:25:05.605Z</atom:updated>
            <content:encoded><![CDATA[<h3>Getting started with FEC campaign contributions data</h3><p>A few tips and links for getting acquainted with the FEC’s campaign contributions data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*mExI_E4IowaQfPiq-OlZKQ.jpeg" /></figure><h3>Campaign finance data is available for download from the FEC</h3><p>The <a href="https://www.fec.gov/">Federal Election Commission</a> (FEC), founded in 1974 to oversee campaign finance law, provides data to the public. One series of datasets they have is on campaign contributions for presidential campagins. It is organized by the <a href="https://classic.fec.gov/disclosurep/PDownload.do">individual contributor’s state of residence</a>. Features included are contributor’s name, occupation, employer, zip code, contribution amount, and candidate recipient. See the <a href="https://cg-519a459a-0ea3-42c2-b7bc-fa1143481f74.s3-us-gov-west-1.amazonaws.com/bulk-downloads/Presidential_Map/2020/DATA_DICTIONARIES/CONTRIBUTOR_FORMAT.txt">Data Dictionary</a> for more information.</p><h3>Each record is a transaction and contributors often have multiple records</h3><p>The contributor name feature, CONTBR_NM, is not unique, because an individual may donate smaller amounts multiple times. The field for transaction ID, TRAN_ID, records each transaction.</p><h3>Use the Form Type feature to filter</h3><p>The form type feature, FORM_TP, indicates whether the transaction is an individual contribution, a committee contribution, or a refund using three separate codes. These codes come from line numbers in the FEC’s Form 3P Schedule A: Report of Receipts and Disbursements by an Authorized Committee of a Candidate for the Office of President or Vice President. Line17A, coded <strong>SA17A</strong>, refers to “the total amount of itemized contributions (other than loans) from individuals/persons other than political committees.” Line18A, coded <strong>SA18A</strong>, refers to the “total amount of transfers from other authorized committees of the same candidate.” And Line 28B, coded <strong>SA28B</strong>, refers to “ the total amount of contribution refunds to individuals/ persons other than political committees.”</p><p>Happy exploring!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40d2072ffee9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a MySQL instance using AWS]]></title>
            <link>https://medium.com/@alyssa.liguori/how-to-create-a-mysql-instance-using-aws-9e33f0c5f97a?source=rss-c16c7ad8b1e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e33f0c5f97a</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Alyssa Liguori]]></dc:creator>
            <pubDate>Thu, 03 Oct 2019 21:00:22 GMT</pubDate>
            <atom:updated>2019-11-15T23:02:44.053Z</atom:updated>
            <content:encoded><![CDATA[<h3>In 5 steps with screenshots</h3><p><em>Step 1</em>: Register for an <a href="http://aws.amazon.com/">Amazon Web Services (AWS) account.</a> Choose the <strong>Free Tier </strong>to get started. You will need: credit card information (no fees) and a phone on-hand (for verification).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*2Fzpq8EtSc3rwyxl74m2kQ.png" /></figure><p><em>Step 2</em>: Open the <a href="https://aws.amazon.com/console/">AWS Management console</a>. Under <strong>AWS Services</strong>, under subtitle <strong>Database</strong>, click <strong>RDS</strong> (Relational Database Service).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*5E1f2NTun7f7bGHd3ffNFw.png" /></figure><p><em>Step 3</em>: You are now in the RDS console. In the right-hand corner, you will see a region auto-selected. You can choose a different one from a list of about 30. I recommend selecting the region closest to you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b1Nqb6W67wTjoVShviLI8w.png" /></figure><p><em>Step 4</em>: Scroll to <strong>Create Database</strong> and click <strong>Create Database</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*wLOkibTMzZy1htwmFFZ7rA.png" /></figure><p>Change these defaults and leave the rest:</p><ul><li>Engine type- MySQL</li><li>Templates- Free Tier</li><li>Storage autoscaling- disable</li><li>Master username- [type login ID]</li><li>Master password- [type password]</li><li>Confirm password- [type password again]</li></ul><p><em>Step 5</em>: Click Create Database then, wait for db to be created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*HpZngn_V8JGyk4XGqQFGbA.png" /></figure><p>You’ve created your MySQL database instance…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*_42XUNVtKaha1vPhtpxgRg.png" /></figure><p>What does the <strong>Free Tier</strong> offer?</p><ul><li>750 hours of Amazon RDS db.t2.micro usage per month running MySQL in your Single-Availability Zone (e.g. Singapore)</li><li>20 GB of General Purpose Storage</li><li>20 GB for automated backup storage</li></ul><p>For additional resources, check AWS’s documentation and training including <a href="https://aws.amazon.com/getting-started/tutorials/create-mysql-db/?trk=gs_card"><em>Create and Connect to a MySQL Database</em></a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e33f0c5f97a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>