<?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 Bonfire Algorithm on Medium]]></title>
        <description><![CDATA[Stories by Bonfire Algorithm on Medium]]></description>
        <link>https://medium.com/@bonfirealgorithm?source=rss-e511065e522c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zTPiIZzzHkyH0lbXnD5lOA.jpeg</url>
            <title>Stories by Bonfire Algorithm on Medium</title>
            <link>https://medium.com/@bonfirealgorithm?source=rss-e511065e522c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:08:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bonfirealgorithm/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[How to Set up Vue.js with Phoenix 1.4]]></title>
            <link>https://medium.com/@bonfirealgorithm/how-to-set-up-vue-js-with-phoenix-1-4-2ed8bda7f4e0?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ed8bda7f4e0</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Tue, 05 Nov 2019 19:19:36 GMT</pubDate>
            <atom:updated>2019-11-05T19:26:54.542Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QJI3Q4OJZNHCEaU8esVWkA.png" /></figure><p>Start by creating a new Phoenix project. We use the --no-ecto flag here, since we don’t need a database for the example.</p><pre>$ mix phx.new vue_example --no-ecto</pre><p>Then cd into the new projects assets directory and install the dependencies we need:</p><pre>$ cd vue_example/assets<br>$ npm install vue vue-loader vue-template-compiler --save-dev</pre><p>Phoenix comes preconfigured with Webpack, so all we need to do is modify the file assets/webpack.config.js</p><p>First, include the VueLoaderPlugin in the list of imports:</p><pre>const { VueLoaderPlugin } = require(&#39;vue-loader&#39;);</pre><p>Next, in the list of rules, add a rule for vue files:</p><pre>{<br>  test: /\.vue<em>$</em>/,<br>  exclude: /node_modules/,<br>  loader: &#39;vue-loader&#39;,<br>}</pre><p>Lastly, add the VueLoader plugin in the list of plugins:</p><pre>plugins: [<br>  ...<br>  new VueLoaderPlugin()<br>],</pre><p>The finished file should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/658149a8716f52f6bd01af84012a59c5/href">https://medium.com/media/658149a8716f52f6bd01af84012a59c5/href</a></iframe><p>Now create a folder assets/js/components. We’ll put our Vue components here. Create a file Message.vue in this folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/389e928d7ed4f2d2ff4781719c60d85d/href">https://medium.com/media/389e928d7ed4f2d2ff4781719c60d85d/href</a></iframe><p>We now need to import this component in assets/js/app.js. Add the following lines to the end of this file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/012dc840d8c19e652752d91198736930/href">https://medium.com/media/012dc840d8c19e652752d91198736930/href</a></iframe><p>Now all that’s left is display this in a HTML template. Replace the contents of lib/vue_example_web/templates/page/index.html.eexwith this:</p><pre>&lt;div <em>id</em>=&quot;hello&quot;&gt;&lt;/div&gt;</pre><p>That’s it. Start the server (mix phx.server) and go to <a href="http://localhost:4000,">http://localhost:4000,</a> and you should see a button provided by Vue.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ed8bda7f4e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JWT Auth with DjangoREST API]]></title>
            <link>https://medium.com/swlh/jwt-auth-with-djangorest-api-9fb32b99b33c?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fb32b99b33c</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Sun, 03 Nov 2019 17:16:31 GMT</pubDate>
            <atom:updated>2022-04-10T09:48:58.596Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*N5Iep1wJY1iXgMzpHxzE8w.png" /></figure><p>Token based authentication allows the server and the frontend (whether that be web, native mobile or something else) to be decoupled and reside on different domains. <a href="https://jwt.io/">JSON Web Tokens</a> (JWT) is a popular implementation of token based authentication, and in this article we’ll use it to authenticate users in an API for notes built with <a href="https://www.django-rest-framework.org/">Django REST Framework</a>.</p><p>We’ll set up user registration and authentication, and we will define the notes model. We will also ensure that the current logged in user is set as the owner of a note when it is created, and that a user can perform read and write operations only to his own notes.</p><p>The full source code for the final project is here: <a href="https://github.com/bonfirealgorithm/notes-api">https://github.com/bonfirealgorithm/notes-api</a></p><h3>1. Create Django Project</h3><pre>$ mkdir notes &amp;&amp; cd notes</pre><p>Create a virtual environment and activate it.</p><pre>$ python3 -m venv venv<br>$ source venv/bin/activate</pre><p>Install Django, DjangoREST and django-cors-headers:</p><pre>$ pip install django django-rest-framework django-cors-headers</pre><p>We install basic Django, as well as Django REST Framework, which will assist us in implementing the API. We also install django-cors-headers, which makes it possible to access our endpoints from browsers on other domains than the Django server.</p><p>Next we create a new Django project:</p><pre>$ django-admin startproject project .</pre><p>The dot at the end of the command will create the project inside the project we’re in, the notes directory.</p><p>Next we create a notes app for our project and run migrations:</p><pre>$ python manage.py startapp notes<br>$ python manage.py migrate</pre><p>We’ll add the installed apps and REST Framework settings in project/settings.py:</p><p><a href="https://github.com/bonfirealgorithm/notes-api/blob/master/project/settings.py">project/settings.py</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cdd0ac88aa7852ce65828f4cf129372/href">https://medium.com/media/0cdd0ac88aa7852ce65828f4cf129372/href</a></iframe><p>To check that everything’s working, let’s run the server:</p><pre>$ python manage.py runserver</pre><p>Visit<a href="http://localhost:8000/"> http://localhost:8000</a></p><p>You should see the Django welcome page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AThIvqQnSuQ4AB_lFPcdAQ.jpeg" /></figure><p>But we still have no API and no data to serve in the API. Let’s make a notes model next.</p><h3>2. Create a Notes Model and a Superuser</h3><p>In notes/models.py add the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f9f2cec98c8c9be1dbb2f55191298d4/href">https://medium.com/media/9f9f2cec98c8c9be1dbb2f55191298d4/href</a></iframe><p>We create a Note model with five attributes. The owner is a User object, which we import from Django’s auth module. Later, when we have added authentication, we will ensure that the owner is set to the currently logged in user. We also have title and content for the note, a shorter char field and a longer text field, respectively. Lastly we add a created and updated field.</p><p>Also register the model with the admin site so that it will appear there. notes/admin.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba2b7acb9fb679983cd04dcc2ee0500a/href">https://medium.com/media/ba2b7acb9fb679983cd04dcc2ee0500a/href</a></iframe><p>Since we’ve created a new model we need to create and run migrations to sync it with the database:</p><pre>$ python manage.py makemigrations<br>$ python manage.py migrate</pre><p>Let’s also create a superuser so we can inspect and create notes objects in the admin panel. (You can just select the default username and leave email blank when prompted.)</p><pre>$ python manage.py createsuperuser</pre><p>Let’s start the server again.</p><pre>$ python manage.py runserver</pre><p>Then visit <a href="http://localhost:8000/admin/">http://localhost:8000/admin/</a> and log in with your superuser’s credentials.</p><p>You should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*foMCtQ5o5yxyxMyneDa-XQ.png" /></figure><p>Add a couple of notes so we have some data to work with.</p><h3>3. Serve the Notes data as an API</h3><p>We now need to add some urls so we can access the notes API.</p><p>Change project/urls.py to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b42c39996d1cd060b62fe7ebeff09a2/href">https://medium.com/media/9b42c39996d1cd060b62fe7ebeff09a2/href</a></iframe><p>Next create the file notes/urls.py and add the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55058eb4de9225dcc7802d3001e142d9/href">https://medium.com/media/55058eb4de9225dcc7802d3001e142d9/href</a></iframe><p>We’re using rest_framework’s SimpleRouter to automatically create the routes for us. Also notice that we import NoteViewSet, which we haven’t created yet. We’ll do that soon, but first we need to create a serializer for our model.</p><p>Serializers come with rest_framework and provide a way to translate our model’s data back and forth to a format that is suitable for our API, in our case JSON. Serializers can also perform validations as well as let us specify which fields to include in the data we exchange with the rest of the world.</p><p>Create the file notes/serializers.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0b8fe6c8f37bdea4d03728cc5afad4d/href">https://medium.com/media/f0b8fe6c8f37bdea4d03728cc5afad4d/href</a></iframe><p>Next we’ll create the view in notes/views.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1cae0e618580a1f15ce7a07ca9c627b/href">https://medium.com/media/a1cae0e618580a1f15ce7a07ca9c627b/href</a></iframe><p>Ensure that your are logged in with your superuser, and go to <a href="http://localhost:8000/api/notes/">http://localhost:8000/api/notes/</a></p><p>You should now be able to see the notes you have created, as well as create new notes.</p><h3>4. Authentication and Permissions</h3><p>For us to explore authentication and permission, create a new user in the admin panel. (All you need to add is username and password.)</p><p>Then add the following line to the urlpatterns list in project/urls.py</p><pre>path(&#39;auth/&#39;, include(&#39;rest_framework.urls&#39;)),</pre><p>This ensures that we have a path for logging in with django_rest. You’ll now see that there’s a down arrow by the user name in the upper right corner:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w66izjr9zQq6MIsfwgq76g.png" /></figure><p>If you click on it you can log out and switch between users.</p><p>One obvious problem now is that every user can see every other user’s notes. Even worse, users who are not logged in can see and create notes. (You can verify this by logging on and then going to <a href="http://localhost:8000/api/notes/">http://localhost:8000/api/notes/</a>)</p><p>Let’s fix that.</p><p>The reason even users who are not logged in have access to notes, is that we have configured rest_framework with AllowAny. Go into your project/settings.py file and add the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73e898e18edc928d7794b547aa6278a0/href">https://medium.com/media/73e898e18edc928d7794b547aa6278a0/href</a></iframe><p>Then try to access <a href="http://localhost:8000/api/notes/">http://localhost:8000/api/notes/</a></p><p>As long as you are logged out, you should no longer see any notes, but instead receive the following message: “detail”: “Authentication credentials were not provided.”</p><p>Log in again, and you will see the notes. How can we fix the problem with one logged in user being able to see every other user’s notes?</p><p>Make the following changes in notes/views.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11180eb4b25f5d1e0bc242727f215a4d/href">https://medium.com/media/11180eb4b25f5d1e0bc242727f215a4d/href</a></iframe><p>We remove the queryset property from the class and instead override the get_queryset method, where we filter the notes objects to ensure we only return those who belong to the current user.</p><p>Furtermore we add an IsOwner permission check. This ensures that a user can only modify (update/delete) his own objects.</p><p>Lastly we override the perform_create method so that when a new note object is created the owner is always set to the current user.</p><p>If you visit <a href="http://localhost:8000/api/notes/">http://localhost:8000/api/notes/</a> you should only see the notes belonging to the current logged in user. If you try to make a new note and set the owner to be another user than the one you are logged in as, the note will be created, but the user will be the current logged in user regardless.</p><p>With these changes in place we can remove the owner field from the serializer, since any note a user will deal with will always be the user’s own. Make the following change to notes/serializers.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0b8fe6c8f37bdea4d03728cc5afad4d/href">https://medium.com/media/f0b8fe6c8f37bdea4d03728cc5afad4d/href</a></iframe><p>We have now implemented authentication and permissions, but django_rest is still using session_authentication.</p><p>What we want is for any client, web or otherwise, to be able to register a user, login and logout and be authenticated from anywhere. To do that we will use JWT.</p><h3>5. Adding JSON Web Token Authentication</h3><p>To implement token authentication with JWT, we will use a library, <a href="https://github.com/davesque/django-rest-framework-simplejwt">Simple JWT</a>:</p><pre>$ pip install djangorestframework_simplejwt</pre><p>Then we need to add it to our list of authentication classes in our project/settings.py file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86c8332ad12a75256310d1ab5e1a23d9/href">https://medium.com/media/86c8332ad12a75256310d1ab5e1a23d9/href</a></iframe><p>And add two new endpoints to our project/urls.py file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2f7e86abd823424fa724319f7249dc5/href">https://medium.com/media/a2f7e86abd823424fa724319f7249dc5/href</a></iframe><p>These new endpoints provide what we need for user log in. The route for login would be /api/token/. The /api/refresh/ route is used to get a new token before the old expires.</p><p>And we actually don’t need an endpoint for logging out, since the server doesn’t maintain any state. To log out we can simply delete the token on the client. The token will expire “on it’s own” (the time can be set using Simple JWT settings).</p><p>But what we currently miss is a way to register a user and get a JWT token back.</p><h4>User Sign Up</h4><p>We need a new endpoint where users can sign up. This doesn’t belong in the Notes app, since this is another domain, authentication. So we’ll start by creating a new app, jwtauth:</p><pre>$ python manage.py startapp jwtauth</pre><p>Add app in project/settings.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7c49fdd01a37460df2b4d24a5e6d339/href">https://medium.com/media/c7c49fdd01a37460df2b4d24a5e6d339/href</a></iframe><p>Next we’ll need a serializer for the User object. Add jwtauth/serializers.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f3149ae118a49b0d2e3d501c3fb13cb/href">https://medium.com/media/4f3149ae118a49b0d2e3d501c3fb13cb/href</a></iframe><p>We’ll be using Django’s built in User model, which we get by calling the get_user_model() function. It’s good practice to do it this way instead of importing the User directly, since it will ensure that we get the currently active User model even if we have customized it.</p><p>We also override the create() method and check that the confirmation password is identical to the password, and that no other user has the same email address.</p><p>Next we’ll add a view in jwtauth/views.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b410853664b3460bc57dd02dac1f33b1/href">https://medium.com/media/b410853664b3460bc57dd02dac1f33b1/href</a></iframe><p>This is the first time we use a function based view and not a class based view. We choose a function based view here since it only responds to the POST http verb, and we use decorators to ensure this, as well as make and exception from the permissions defined in the settings.py file to allow anyone access to just this endpoint.</p><p>We do a check to see if the serializer has validated the data we got, and if not return it’s error object. If everything is fine we save the serializer, which returns the newly created user object. We can then obtain a JWT token for this user and return it.</p><p>We need to create a urls file add the view to it, jwtauth/urls.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89475251577412cdd8f6130e0f264fe9/href">https://medium.com/media/89475251577412cdd8f6130e0f264fe9/href</a></iframe><p>And lastly include the jwtauth urls in project/urls.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87b0dfa964a2ce6074507f593706d6d7/href">https://medium.com/media/87b0dfa964a2ce6074507f593706d6d7/href</a></iframe><p>And that’s it. We should now have a new endpoint at <a href="http://localhost:8000/api/accounts/register/">http://localhost:8000/api/jwtaut/register/</a></p><p>We can test it in Postman:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kO1GSF9Z4Kepy315KePhEQ.png" /></figure><p>If the validations pass it should return an object with refresh and access tokens.</p><h3>6. Adding Swagger Docs</h3><p>The last thing we’ll do is add Swagger docs so consumers of the API can see what endpoints are available.</p><pre>$ pip install django-rest-swagger</pre><p>Add it to your INSTALLED_APPS list in project/settings.py. Also include the new DEFAULT_SCHEMA_CLASS in REST_FRAMEWORK settings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6014b5d281f4b4d49d7d1f0e25ad7306/href">https://medium.com/media/6014b5d281f4b4d49d7d1f0e25ad7306/href</a></iframe><p>And include it in your project/urls.py file. To finish up we’ll also do a little bit of refactoring. Refresh and token are separate endpoints now. Since they have to do with authentication it would be better if they were in the jwtauth app. Let’s move them there, as well as add a url for the docs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd3ab71c5b81274d2f5f40557da947aa/href">https://medium.com/media/bd3ab71c5b81274d2f5f40557da947aa/href</a></iframe><p>Then put the token and refresh enpoints in jwtauth/urls.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01207d061fb26305a56714cfdb2aa909/href">https://medium.com/media/01207d061fb26305a56714cfdb2aa909/href</a></iframe><p>Go to <a href="http://localhost:8000/api/docs/">http://localhost:8000/api/docs/</a> to see the full list of APIs. You should also see that the token endpoints now belong to the /jwtauth grouping.</p><p>And that’s it! You now have a fully functionall notes API that can be consumed by a client from anywhere.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fb32b99b33c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/jwt-auth-with-djangorest-api-9fb32b99b33c">JWT Auth with DjangoREST API</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Integrate Vue.js with Rails 6]]></title>
            <link>https://medium.com/swlh/how-to-integrate-vue-js-with-rails-6-5bd4c6263b18?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5bd4c6263b18</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[webpacker]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Mon, 21 Oct 2019 21:07:56 GMT</pubDate>
            <atom:updated>2019-10-22T16:43:01.351Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPE6qrYXEv3TiTVu5PxXEg.jpeg" /><figcaption>Rails with a Vue. Photo by <a href="https://unsplash.com/@austinsmart?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Austin Smart</a> on <a href="https://unsplash.com/s/photos/rails?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Starting with Rails 6 the default JavaScript compiler is Webpacker (not Sprockets as before). Webpacker wraps the JavaScript build/bundling library Webpack (yes, the names are very similar and it does cause confusion).</p><p>With Rails 6 it’s pretty easy to integrate a JavaScript framework like Vue.js or React.</p><p>Simply start a new Rails project with the Webpack flag (exchange vue with react or a JS framework of your choice depending on what you like):</p><pre>$ rails new hello_vue --webpack=vue</pre><p>This will install a new project with Vue.js included as a <em>pack</em>. A pack is Rail’s terminology for any JavaScript we add to the project.</p><p>Open the file app/javascript/packs/hello_vue.js that Rails has generated for us. The file contains some JS code and instructions for how to integrate Vue.js with Rails.</p><p>First we comment out the first code snippet in the file, since we want to use Vue.js with the Webpacker compiler. At the bottom of the file we find the instructions for using Vue.js with Turbolinks. Rails comes with Turbolinks enabled, and this is the code we want, so we uncomment it. Also notice it says we should install vue-turbolinks:</p><pre>$ yarn add vue-turbolinks</pre><p>We then copy the following code from the file:</p><pre>&lt;div id=&#39;hello&#39;&gt;<br> {{message}}<br> &lt;app&gt;&lt;/app&gt;<br>&lt;/div&gt;</pre><p>And generate a controller and a view:</p><pre>$ rails g controller Hello say_hello</pre><p>We delete all the HTML in app/views/hello/say_hello.html.erb and replace it with the code we copied. We also add erb code to add the script. This could also be placed in the layout file, but since this is the only view using it, we put it here:</p><pre>&lt;%= javascript_pack_tag &#39;hello_vue&#39; %&gt;</pre><pre>&lt;div id=&#39;hello&#39;&gt;<br> {{message}}<br> &lt;app&gt;&lt;/app&gt;<br>&lt;/div&gt;</pre><p>That should be it. But before we try it out, we’ll start a separate webpack server. We don’t need to do this, but it will make JS compile times much faster and also auto reload the page on JS change, so it is nice to have. Open a separate console, go into the project directory and type:</p><pre>$ ./bin/webpack-dev-server</pre><p>With the server started we fire up Rails:</p><pre>$ rails s</pre><p>And navigate to <a href="http://localhost:3000/hello/say_hello">http://localhost:3000/hello/say_hello</a></p><p>You should see a “Hello Vue!” message.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5bd4c6263b18" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-integrate-vue-js-with-rails-6-5bd4c6263b18">How to Integrate Vue.js with Rails 6</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I Start a Django app]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-snippet">Starting a new Django project comes with some considerations. This is the way I like to do it.</p><p class="medium-feed-link"><a href="https://medium.com/@bonfirealgorithm/how-i-start-a-django-app-75b262822f6?source=rss-e511065e522c------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@bonfirealgorithm/how-i-start-a-django-app-75b262822f6?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/75b262822f6</guid>
            <category><![CDATA[setup]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Sun, 20 Oct 2019 14:38:34 GMT</pubDate>
            <atom:updated>2019-10-21T06:07:33.117Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Speed Up Your Server-Rendered Pages with Turbolinks]]></title>
            <link>https://medium.com/@bonfirealgorithm/spa-like-responsiveness-with-turbolinks-2657d0f013e0?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2657d0f013e0</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[turbolinks]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Sat, 08 Sep 2018 13:18:29 GMT</pubDate>
            <atom:updated>2018-09-08T14:17:13.678Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RnzS3z_HB6kvAPtK_yhnOQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/l75UcYjdiC0?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">chuttersnap</a> on <a href="https://unsplash.com/search/photos/speed?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://github.com/turbolinks/turbolinks">Turbolinks</a> is a JavaScript library that makes navigation in a web app faster. It can be used to speed up navigation between pages, between sub-menus and even form submissions and redirects. It can give your webabb the responsiveness of a single page application (SPA) while the HTML is still static pages delivered by the server. In other words, you can develop your whole application using a backend framework like Django or Rails and make it feel like an SPA.</p><h3>The problem with server rendered pages</h3><p>When HTML is rendered on the server and delivered to the browser, for each page load the browser needs to reconstruct the render tree and set up CSS and JavaScript. Even though your server response time might be very good, say below 50 ms, it is the re-rendering of the page that takes time, often 0.5–1 seconds. This is enough of a delay that the server rendered web apps don’t feel as smooth and instantaneous to the user as SPAs. SPAs achieve this by receiving only data from the server through AJAX and updating just parts of the DOM tree.</p><h3>Turbolinks to the rescue</h3><p>Turbolinks uses a neat trick to circumvent the page re-rendering in the browser. When a request is sent to the server (the user clicks a link or submits a form) Turbolinks intercepts the request, prevents the browser from following it and requests the new page using XMLHttpRequest. When the server returns the HTML Turbolinks swaps the body part of current HTML with the new content. In the latest version of Turbolinks it is also possible to use partial updates, swapping only parts of the HTML.</p><p>The result is that the load time for e.g. page navigation can be greatly reduced, down to 100–250 ms. With that kind of responsiveness a fully server rendered app can feel like an SPA.</p><h3>Reduced complexity and simpler development</h3><p>Generating all HTML server side reduces complexity and makes development simpler and faster. This is an advantage not least for solo developers and small teams. Instead using a backend framework like Rails, Spring or Django for the API and a frontend framework like Angular, React or Vue — the developers now only need a backend framework. You also don’t end up duplicating data structures and update and validation logic on the frontend and the backend. The end result is less complexity, less code to maintain, less technical debt and faster development.</p><h3>So how can I get it?</h3><p>Turbolinks comes with Rails 5 and is turned on by default for links. You can also easily add it for form submissions. It works pretty seamlessly out of the box.</p><p>In other frameworks you need to add Turbolinks manually. All you need to do is include it in the head section of your base HTML template and it will speed up all link clicks. To get it to work with forms you need to intercept the submit with JavaScript and send it as an AJAX request. You’ll also usually need to add middleware to get it to work with redirects. Many frameworks have plug-ins that eases this process.</p><h3>Conclusion</h3><p>Turbolinks is an interesting middle ground between strictly server rendered pages and full-blown SPAs. Other libraries, like <a href="https://unpoly.com/">Unpoly</a> and <a href="http://intercoolerjs.org/">Intercooler</a>, offer some of the same functionality and takes it even further. I like Turbolinks’s simplicity and unobtrusiveness. The hype around SPAs has lasted several years and is still strong. Turbolinks and similar libraries offer a promising direction where you can get much of the responsiveness of an SPA without the complexity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2657d0f013e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beyond the For-Loop: Higher Order Functions and List Comprehensions in Python]]></title>
            <link>https://medium.com/@bonfirealgorithm/beyond-the-for-loop-higher-order-functions-and-list-comprehensions-in-python-695b58ab71d3?source=rss-e511065e522c------2</link>
            <guid isPermaLink="false">https://medium.com/p/695b58ab71d3</guid>
            <category><![CDATA[higher-order-function]]></category>
            <category><![CDATA[list-comprehension]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Bonfire Algorithm]]></dc:creator>
            <pubDate>Wed, 29 Aug 2018 19:15:15 GMT</pubDate>
            <atom:updated>2019-11-09T09:17:14.271Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XpduAXTdIHi9_HiiBd4iJg.jpeg" /></figure><p>A higher order function is a fuction that operates on other functions, either by taking a function as its argument, or by returning a function. Since functions are first-class objects this is easy to do in Python, and the standard library comes with several higher order functions that make list operations more convenient. Let’s take a look at some of those.</p><h4>Filter</h4><p>Let’s say you have a list of integers and you only want the numbers that are even.</p><p>We could solve this with a for-loop:</p><pre>integers = range(0, 10)  <br>even = [] <br>for i in integers:     <br>    if i % 2 == 0:         <br>        even.append(i)  </pre><pre>&gt;&gt;&gt; even <br>&gt;&gt;&gt; [0, 2, 4, 6, 8]</pre><p>However, Python has a built in higher order function which can do the same more succinctly — filter(). It takes a function and an iterable as arguments and constructs a new iterable by applying the function to every element in the list.</p><pre>even = filter(lambda x: x % 2 == 0, integers)</pre><p>We have reduced the for-loop’s four lines to one. Note that we use a lambda function, an anynomous function, that can be convenient for short ad-hoc functions. We could also have written:</p><pre>def is_even(x):     <br>    return x % 2 == 0  </pre><pre>even = filter(is_even, integers)</pre><p>In addition to higher order functions Python has list comprehensions, so the above could also be written as:</p><pre>even = [x for x in integers if x % 2 == 0]</pre><p>What’s going on here? We simply say that we want each integer in integers if it is divisible by 2. It is in essence a flattened, more succint for loop. We don’t have to call any functions.</p><p>One problem, three solutions. How do you know which one to use? It comes down to a matter of taste, speed and readability.</p><p>The for-loop is the longest, but it’s also easier to read and understand for someone new to Python or higher order functions. For someone who knows Python well the other versions might be both easier to read and write since they are shorter.</p><p>The higher order function and the list comprehension are both one-liners. Seasoned Python programmers tend to prefer list comprehensions. They state their intent clearly and can be read from left to right. In this case the list comprehnsion solution is also faster, since it doesn’t make any function calls.</p><h4>Map</h4><p>Map applies a function to every element in a list. Unlike filter, it leaves the number of elements in the list unchanged. Let’s say we want to square every element in a list (since map() and filter() return iterators in Python 3, we surround map() with list() to get a readable output):</p><pre>integers = range(0, 10)<br>list(map(lambda x: x * x, integers))</pre><pre>&gt;&gt;&gt; [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre><p>And as a list comprehension:</p><pre>integers = range(0, 10)<br>[x * x for x in integers]</pre><pre>&gt;&gt;&gt; [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre><p>Again, the list comprehension is faster, and I would argue it is also more convenient and easier to read once the notation is familiar.</p><h4>Reduce</h4><p>So far we’ve looked at functions that both consume and return lists. There are also numerous functions that consume a list but return a single value.</p><p>Reduce works by applying a function that takes two arguments to the elements in the list, from left to right. Here we use it to get the product of all the integers in the list:</p><pre>from functools import reduce</pre><pre>integers = range(1, 10)  <br>reduce(lambda x, y: x * y, integers)  </pre><pre>&gt;&gt;&gt; 362880</pre><p>What about using a list comprehension here? We can’t. List comprehensions always returns iterables, not single values.</p><p>Reduce is very powerful, but can also be hard to comprehend. Notice the import statement—reduce() has been ostracized from Python’s core functions and had to be imported. In fact, Guido van Rossum hates reduce(). In his <a href="https://www.artima.com/weblogs/viewpost.jsp?thread=98196">own words</a>:</p><blockquote><em>“So now reduce(). This is actually the one I’ve always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what’s actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it’s better to write out the accumulation loop explicitly.”</em></blockquote><p>Luckily Python includes a handful of functions that could all have been implemented by reduce(), but are convenient to have as stand-alone functions. Since they neither consume or return functions, they are not higher order functions.</p><h4>Sum</h4><p>Sum simply adds all the elements in a list:</p><pre>integers = range(1, 10)<br>sum(integers)  </pre><pre>&gt;&gt;&gt; 45</pre><h4>Any and All</h4><p>Any and all works just like the names suggest:</p><pre>any([False, True, False])   </pre><pre>&gt;&gt;&gt; True    </pre><pre>all([False, True, False])   </pre><pre>&gt;&gt;&gt; False</pre><p>The all() and any() functions are good examples of how functions that take an iterator can be combined with list comprehensions. To check for even numbers in a list:</p><pre>integers = range(1, 10)<br>any(x % 2 == 0 for x in integers)   </pre><pre>&gt;&gt;&gt; True</pre><pre>all(x % 2 == 0 for x in integers)   </pre><pre>&gt;&gt;&gt; False</pre><p>When not overused this combination can result in succinct, readable and Pythonic code.</p><h4>Are We Functional Now?</h4><p>Not really. Like we mentioned, Guido van Rossum <a href="https://www.artima.com/weblogs/viewpost.jsp?thread=98196">was never a fan</a>, and Python lacks the enforcement of both immutable data structures and pure (side effect-free) functions you see in more strictly functional languages.</p><p>But in line with Python’s pragmatic approach, the languge has assimilated some useful concepts. Personally I think list comprehensions work so well in Python I seldom reach for map() or filter().</p><p>Check out <a href="https://docs.python.org/3/library/functions.html">Python’s built in functions</a> for other higher order functions. Also, take a look at <a href="https://docs.python.org/3/library/itertools.html?highlight=itertools">Python’s itertools library</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=695b58ab71d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>