<?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 Thomas Antony on Medium]]></title>
        <description><![CDATA[Stories by Thomas Antony on Medium]]></description>
        <link>https://medium.com/@tantony?source=rss-9f0babd0da2f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GLtgnTWsVjGd2d0OUzrlaA.jpeg</url>
            <title>Stories by Thomas Antony on Medium</title>
            <link>https://medium.com/@tantony?source=rss-9f0babd0da2f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 23:48:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tantony/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[Using VirtualCAN inside docker on macOS]]></title>
            <link>https://medium.com/@tantony/using-virtualcan-inside-docker-on-macos-cfa10f7e75d6?source=rss-9f0babd0da2f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfa10f7e75d6</guid>
            <category><![CDATA[virtualcan]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[can]]></category>
            <category><![CDATA[socketcan]]></category>
            <category><![CDATA[mac-osx]]></category>
            <dc:creator><![CDATA[Thomas Antony]]></dc:creator>
            <pubDate>Fri, 09 Feb 2018 03:42:31 GMT</pubDate>
            <atom:updated>2018-02-09T03:51:29.904Z</atom:updated>
            <content:encoded><![CDATA[<p>At <a href="http://www.smart-ag.com/">SmartAg</a>, we use Docker to manage the development and runtime environments for our embedded software. For performing full-system integrated tests, we have built comprehensive simulators that mimic the behavior of the hardware that we automate. Since most of the hardware communication happens over CAN, VirtualCAN is a great way of faking the hardware signals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*xk974-nijAtuCTMhs7B0-w.png" /></figure><p>Until now, any code that required a CAN interface could not run on my Macbook Pro. This was primarily because the linux kernel used by <a href="https://www.docker.com/docker-mac">Docker for Mac</a> lacks support for <a href="https://en.wikipedia.org/wiki/SocketCAN">SocketCAN</a> or VirtualCAN. After much <a href="https://github.com/docker/for-mac/issues/2580">head-bashing</a>, I realized that a better option would be <a href="https://docs.docker.com/machine/overview/">docker-machine</a> , which is how docker was originally run on macOS before the official “Docker for Mac” release.</p><h3>Setting up a CAN-enabled docker-machine VM</h3><ol><li>Install <a href="https://brew.sh/">Homebrew</a> if you don’t already have it.</li><li>Install docker, docker-machine and some associated software</li></ol><pre>brew install docker docker-compose docker-machine docker-credential-helper docker-machine-driver-xhyve</pre><p>3. Clone the repo from <a href="https://github.com/boot2docker/boot2docker">https://github.com/boot2docker/boot2docker</a></p><p>4. Edit kernel_config in the boot2docker repo and add the following at the bottom:</p><pre>CONFIG_CAN=y<br>CONFIG_CAN_RAW=y<br>CONFIG_CAN_VCAN=y<br>CONFIG_CAN_DEV=y<br>CAN_SLCAN=y<br>CAN_BCM=y</pre><p>5. Follow the instructions at <a href="https://github.com/boot2docker/boot2docker/blob/master/doc/BUILD.md">https://github.com/boot2docker/boot2docker/blob/master/doc/BUILD.md</a> to build your custom boot2docker image. This basically boils down to:</p><pre>docker build -t boot2docker .<br>docker run --rm boot2docker &gt; boot2docker-can.iso</pre><p>6. Fix some permissions required by the xhyve docker-machine driver.</p><pre>sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve<br>sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve</pre><p>7. Create your new docker-machine VM with the following command. Change the command appropriately to allocate the desired number of CPUs, amount of RAM and disk space to the VM.</p><pre>docker-machine create default --driver xhyve --xhyve-experimental-nfs-share --xhyve-cpu-count=4 --xhyve-memory-size=4096 --xhyve-disk-size=40000 --xhyve-boot2docker-url boot2docker-can.iso</pre><p>8. Your docker-machine VM is now ready. It should already be running. If you restart your machine, you can start the VM by typing docker-machine start default or stop it with docker-machine stop default .</p><p>9. Set the environment parameters to use docker with your new VM by running eval $(docker-machine env default) . This command has to be run every time you are in a new terminal. You can also add this to your .bashrc to make it automatic.</p><p>10. Check that it all works:</p><pre>docker run -it --rm --privileged alpine /bin/sh</pre><p>Inside the docker container, run:</p><pre>zcat /proc/config.gz | grep CAN</pre><p>The command should display the CAN flags that you set while compiling the kernel. SocketCAN/VirtualCAN is now enabled in any docker container that you start on this VM.</p><h3>Caveats</h3><p>Running docker using docker-machine means that some things, such as port-forwarding does not work as you expect. A handy script for exposing any ports you want from your VM to localhost can be found here: <a href="https://github.com/johanhaleby/docker-machine-port-forwarder">https://github.com/johanhaleby/docker-machine-port-forwarder</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfa10f7e75d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Training a neural network in real-time to control a self-driving car]]></title>
            <link>https://medium.com/@tantony/training-a-neural-network-in-real-time-to-control-a-self-driving-car-9ee5654978b7?source=rss-9f0babd0da2f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ee5654978b7</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[udacity]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[self-driving-cars]]></category>
            <dc:creator><![CDATA[Thomas Antony]]></dc:creator>
            <pubDate>Sun, 11 Dec 2016 05:02:01 GMT</pubDate>
            <atom:updated>2016-12-11T07:16:26.540Z</atom:updated>
            <content:encoded><![CDATA[<p>I was lucky enough to be accepted into the first cohort of Udacity’s <a href="http://www.udacity.com/drive">Self Driving Car NanoDegree</a> program back in October. Though I have been thinking of blogging about my experience since the start, it didn’t really happen till now. I am currently wrapping up Project #3 — “Behavioral Cloning”. This one was considerably tougher than the first two. However, it has been very fulfilling to finally complete it and here I will describe the approach I used for training a neural network to drive a car (in a simulator).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ahKkWS_pXY6ktIJMLptgg.png" /></figure><p>The project description makes it looks easy (relatively). In the first project, we detected lanes using some basic Computer Vision methods and in the second, we classified traffic signs from the <a href="http://benchmark.ini.rub.de/?section=gtsdb&amp;subsection=news">German Traffic Signs dataset</a>. The third one seemed like an extension of the second, where instead of classifying traffic signs, we had to predict steering angles based on a camera feed, using a neural network.</p><h3>Collecting Training Data</h3><p>Unlike Project#2, this time we had to collect our own training data. This was more challenging than expected. As it turned out, controlling Udacity’s SDC simulator with a keyboard is not a simple task. Feedback from other students on Slack seemed to indicate that it also gave bad, jittery steering angle data. Fortunately, I have a friend who is crazy about car racing games and owns a Logitech G27 Racing wheel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*HPYURL9VsoLEBJ68iYK_xg.png" /><figcaption>Udacity’s SDC Simulator</figcaption></figure><p>For training data, I recorded data (“camera” images + steering angle and other telemetry) where I drove through the two tracks a couple of times. I also recorded data while recovering from deliberate perturbations to the sides of the lane to make sure the neural net also learned how to recover from such situations.</p><h3>Picking the Architecture and Training the NN</h3><p>I spent a lot of time experimenting with different convolutional neural network architectures, with varying levels of success. I also tried taking existing trained models like VGG16 and Google’s Inception v3 and adapting them to this purpose. Eventually, I settled upon NVIDIA’s End-to-End Deep Learning architecture described in this <a href="http://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pdf">paper</a>. It was relatively small in size and hence added very little latency when running it. I proceeded to use my gaming desktop with GTX 1070 GPU to train the model on ~32000 images.</p><p>What I learned is that “mean square error” was not at all a predictor of the performance of the network in the actual simulator. Another big issue was the turnaround time between collecting data, training the network and then testing it. Also, the ‘remote driving’ script provided by Udacity set the car’s throttle to a constant value with no way to control the speed.</p><p>That is when I came across <a href="https://medium.com/u/8a1f0a75e0b5">John Chen</a>’s post on our Slack channel about how he developed an “<a href="https://github.com/diyjac/AgileTrainer">Agile Trainer</a>” to reduce the turnaround time between data-collection and testing. His program uses PyGame and Joystick input to manually takeover control from the neural network whenever it deviated too much and also allowed to train the network at the same time.</p><p>I wrote my own live trainer based on this idea, which instead used keyboard input. It turned out that my keyboard based “remote driver” was able to control the simulator more reliably than the simulator’s “Training mode”.</p><p>Best of all, it allowed me to fine-tune the model from my bed with my Macbook Pro by “<a href="https://www.nomachine.com/">NoMachine</a>”-ing into my desktop (which would be difficult with a joystick/racing wheel).</p><h3><strong>How it works</strong></h3><p>The live trainer has a <a href="https://docs.python.org/3/library/tk.html">Tkinter</a>-based UI and a few simple keyboard controls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*kW5kXDzFkn4DCDoIn4hZsg.png" /><figcaption>SDC Live Trainer interface</figcaption></figure><p><strong>Up/Down</strong>: Control cruise speed</p><p><strong>Left/Right</strong>: Steering</p><p><strong>x</strong>: Toggle between autonomous and manual override modes</p><p><strong>c</strong>: Center steering (only in manual override mode)</p><p><strong>z</strong>: Toggle real-time training (only in manual override mode)</p><p>The speed of the car is controlled by a proportional-gain controller. It modulates the throttle based on speed feedback. This rudimentary “cruise control” gets the speed within +/- 1 mph of the target speed, which is good enough in this case.</p><p>The arrow keys increase/decrease the steering angle. The steering also has some slow auto re-centering dynamics added by the trainer. So the steering angle will keep “decaying” towards zero if you don’t press any key. The “autonomous rating” measures what percentage of the current session was spent in fully autonomous mode.</p><h3>Real-time training</h3><p>The process of training itself is pretty simple. You can do one of two things:</p><ol><li>Deliberately perturb the car’s motion while in manual override and engage training mode once you have started the recovery process</li><li>At any spots that the model has trouble with, engage training mode while driving through the spot manually to fine-tune the model.</li></ol><p>The GIF below shows an example of the first case. I purposely drive the car towards the right edge using manual override and then start training the model once I have it recovering and returning to the center of the lane.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FUnknownRemoteHoneycreeper&amp;url=https%3A%2F%2Fgfycat.com%2FUnknownRemoteHoneycreeper&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FUnknownRemoteHoneycreeper-size_restricted.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=gfycat" width="1046" height="618" frameborder="0" scrolling="no"><a href="https://medium.com/media/8899b8292de2409c7d567a560aebd98a/href">https://medium.com/media/8899b8292de2409c7d567a560aebd98a/href</a></iframe><h3>Evaluating model performance</h3><p>My initial model had some trouble navigating a couple of turns on track#1 in the simulator. I was able to successfully tune these spots using live training and finally get this result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FoAujMdetS-0%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DoAujMdetS-0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoAujMdetS-0%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3eb470adb4cf0277a546bebd2a0d7ea1/href">https://medium.com/media/3eb470adb4cf0277a546bebd2a0d7ea1/href</a></iframe><p>Track 2 was a little more difficult, with more sharp gradients and curves. However, I found that decreasing the speed down to 20 mph helped it navigate that track as well!</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSINNzj16PvA&amp;src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSINNzj16PvA%3Ffeature%3Doembed&amp;type=text%2Fhtml&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0911f89d66a363964f5906b725aa1bd/href">https://medium.com/media/a0911f89d66a363964f5906b725aa1bd/href</a></iframe><p>The live trainer code is on Github at:</p><p><a href="https://github.com/thomasantony/sdc-live-trainer">thomasantony/sdc-live-trainer</a></p><h3>Possibilities</h3><p>A live trainer can also probably be used to perform the initial training of the network, though that would involve a lot more baby-sitting during the training process. The good thing is that you can immediately start seeing results as the model converges toward the right weights.</p><p>Is such an architecture extendable to real-world SDCs? It can definitely be done with scale models on scale tracks. On real cars it is probably a little more difficult (though there are probably ways to do it). NVIDIA’s <a href="http://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pdf">paper</a> mentions using real-world data to make a simulator. A live training methodology like the one described here can possibly be incorporated into such a simulator as well!</p><p>Let me know if you have any comments/suggestions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ee5654978b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Souping up my 13.3" MacBook Pro (A1278, Early-2011)]]></title>
            <link>https://medium.com/@tantony/souping-up-my-13-3-macbook-pro-a1278-early-2011-13d4afec3d94?source=rss-9f0babd0da2f------2</link>
            <guid isPermaLink="false">https://medium.com/p/13d4afec3d94</guid>
            <category><![CDATA[macbook]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[diy]]></category>
            <category><![CDATA[upgrade]]></category>
            <dc:creator><![CDATA[Thomas Antony]]></dc:creator>
            <pubDate>Tue, 18 Oct 2016 01:23:28 GMT</pubDate>
            <atom:updated>2016-10-18T02:31:16.319Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fVuozHSKvrv_i4sTaZPcsw.jpeg" /><figcaption>Weird artifacts on the screen on dark backgrounds — Early 2011 MacBook Pro 13.3in</figcaption></figure><p>My Early-2011 13.3in MacBook Pro (non-Retina), was/is the first laptop I ever owned. It has survived five years of air and road trips, drops, scratches, OS upgrades and grad-school.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Le4TbHM1aguDwod30CiWVg.jpeg" /><figcaption>Look ma, no (rubber) feet!</figcaption></figure><p>It is a little beat up with its fair share of dents and scratches and is now starting to get sluggish even after a complete wipe+reinstall. Chrome occasionally causes it to heat up and make the fan scream while everything slows to a crawl. The screen is wobbly and has this weird artifact visible on dark backgrounds as well a couple of what I suspect are dead pixels and white speck that is visible even when the computer is off. The OS has been screaming at me to replace the battery (1150 cycles) for more than a year now and I get some 2 hours of battery life out of it at the most.</p><p>For a few days, I entertained the idea of dropping ~$1500 on a brand new laptop. Maybe one of those sleek, feather-light 12&quot; MacBooks or one of the new 13&quot; MBPs that are supposed to be announced at the end of this month. I was even tempted to get the Dell XPS 13, the Surface Book or the Razer Stealth and maybe “Hackintosh”-ing them. Then I realized there is still a lot of unrealized potential in my current laptop.</p><p>One of the biggest advantages of a non-Retina 13.3in MBP is that despite the comparatively lower specs, it has upgradable RAM and storage. The processor in the Early-2011 MBP is a Core-i5 2.3 GHz, which is going strong and is even comparable in benchmarks to those in the more recent MBPs. So I started trawling eBay and Amazon, looking for suitable parts.</p><ul><li><a href="http://www.ebay.com/itm/351866209022">New SanDisk Ultra II 480GB SSD</a> — $100 (that was a steal!)</li><li><a href="http://www.ebay.com/itm/142140642980">Used OEM screen assembly </a>— $110</li><li><a href="http://www.ebay.com/itm/201652575208">New Corsair Apple-Certified 16GB DDR3 1333 MHz kit (2x8Gb)</a> — $80</li><li><a href="https://smile.amazon.com/gp/product/B00D780NNQ">Anker replacement battery </a>— $54 on Amazon</li><li><a href="http://www.ebay.com/itm/112167574806">Rubber “feet”</a> for the laptop (because why not!) — $2.75</li></ul><p>The total comes up to a little under $350. Given that the Early-2011 MBP lacks USB 3.0 ports, I might end up getting one of those ThunderBolt-to-USB3 adapters to make backups less frustrating. That would take it up to $425 and some change.</p><p>Though it lacks a dedicated GPU, I am hoping that this upgraded version will be enough for the non-GPU work I will have to do for <a href="https://medium.com/u/2929690a28fb">Udacity</a>’s Self Driving Car Engineer Nanodegree program.</p><p>Now I await UPS and USPS to bring me my presents!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13d4afec3d94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[flybywire — Declarative GUIs for Python inspired by React]]></title>
            <link>https://medium.com/@tantony/flybywire-declarative-guis-for-python-inspired-by-react-ad2131d4cbc1?source=rss-9f0babd0da2f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad2131d4cbc1</guid>
            <category><![CDATA[ui-design]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Thomas Antony]]></dc:creator>
            <pubDate>Sun, 04 Sep 2016 05:26:46 GMT</pubDate>
            <atom:updated>2016-09-04T07:09:43.006Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*Ql7p4_Wua5eSiZB41oAAlg.png" /><figcaption>Source: <a href="http://xkcd.com/970/">xkcd#970</a> by Randall Monroe</figcaption></figure><p>Python is amazing. I love the flexibility, the clean and readable syntax and its huge ecosystem of libraries. However, when it comes to making modern GUIs, it comes up sort in many ways.</p><p>There are libraries like <em>PyQT </em>and<em> Kivy</em> to create GUIs for your Python application. However, each of them come with their <a href="https://medium.com/@tryexceptpass/a-python-ate-my-gui-971f2326ce59#.lpb3pnf2d">own problems</a>. Many modern UI solutions like Electron and React-Native looks to the web to avoid the problems of dealing with multiple platforms. After all, the web is one platform that DOES have it’s share of great (and at times awful) UIs.</p><p><em>flybywire</em> is my ongoing experiment to develop a declarative UI library , based off some great work by <a href="https://medium.com/u/138c0eb26be5">Cristian Medina</a>.</p><p><a href="https://github.com/thomasantony/flybywire">thomasantony/flybywire</a></p><p>So, what is <em>flybywire</em> all about? Well, you need a bit of background before I can go into that.</p><h4>React, ES6 and the Endless Abyss</h4><p><a href="http://facebook.github.io/react">React</a> caught my interest sometime late last year when I was trying to build a desktop frontend for a Python project using <a href="http://electron.atom.io/">Electron</a>. I was coming back to web development after a gap of nearly six years and my last experience in the field was with jQuery and PHP.</p><p>After finishing a basic <a href="http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-to-get-by/">tutorial</a>, I was really excited about the possibilities of React. However, that excitement was a little dulled once I discovered the wonderful world of node-based tools that seemed almost mandatory if you wanted to create anything serious. I found that javascript is no longer the innocent, interpreted language I knew it as. ES6, CoffeeScript, TypeScript, transpilers, <em>webpack</em>, the endless abyss that is <strong>node-packages</strong> … it is easy to get lost just trying to get a hang of all these tools and frameworks.</p><p>So where am I going with all this? Well, my (mis?)adventures with React, Redux etc. can be summarized as:</p><ol><li>React seems to have zeroed in on a really good way of defining UIs without much clutter. I especially loved the concept of one-way binding and views defined as pure functions of state which redraws the whole page when updated.</li><li>I found redux to be a great addition to the React ecosystem that provides clean and predictable state management along with some very fancy debugging features.</li><li>A Python programmer who wants to use React or Electron still has to navigate through the very confusing tooling (<em>npm</em>, <em>webpack</em>, <em>babel</em> etc.) before they can even start to create anything useful.</li><li>I don’t want to deal with Javascript to design my UI.</li></ol><p>There has to be a way to do all this in Python!</p><h4>The Magic of Websockets</h4><p>Last week, I came across this <a href="https://medium.com/@tryexceptpass/a-python-ate-my-gui-971f2326ce59#.lpb3pnf2d">article</a> which seemed to reflect many of the problems that I had faced when it came to Python GUIs. The article is part of a 3-part (so far) series that ends with the creation of <a href="http://github.com/tryexceptpass/sofi">Sofi</a> (Cheers <a href="https://medium.com/u/138c0eb26be5">Cristian</a>!).</p><p><a href="https://medium.com/@tryexceptpass/a-python-ate-my-gui-part-3-implementation-39fc105b6d81">A Python ate my GUI — Part 3: Implementation</a></p><p>As described in <a href="https://medium.com/@tryexceptpass/a-python-ate-my-gui-part-3-implementation-39fc105b6d81#.lsuhoosyv">Part 3</a> of the series, Sofi is:</p><p>“… a system that will generate the necessary HTML and JavaScript code typically needed to produce a single-page application and serve it up through WebSockets”</p><p>Though it is at a very early stage in its design, Sofi seems to do what it is supposed to. It allows you to design UIs in Python while leveraging web technologies like Bootstrap to render them. One thing I did not like about Sofi was the imperative nature of the UI design which reminded me of jQuery (and maybe a bit of Java AWT). After my experience with React, I wanted something declarative and functional.</p><p>So, is it possible to leverage websockets to implement something that keeps the general idea behind Sofi, but is declarative and functional like React?</p><h3>flybywire</h3><p><em>flybywire</em> is my ongoing experiment based on Sofi to create a more declarative UI library. It re-renders the page every time the state changes. It uses a virtual-dom system similar to that used by React, to only update those parts of the page that have actually changed. The coding style in <em>flybywire</em> applications was modeled after that of React. The library includes a helper function that allows components to be defined in a syntax inspired by JSX.</p><h4>Usage</h4><p>Below is a really simple demo app that is included with the library:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2FHilariousCarefreeAnchovy&amp;url=https%3A%2F%2Fgfycat.com%2FHilariousCarefreeAnchovy&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FHilariousCarefreeAnchovy-size_restricted.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=gfycat" width="892" height="272" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dc23fcb1cc6c3647bccbfb736334aae/href">https://medium.com/media/0dc23fcb1cc6c3647bccbfb736334aae/href</a></iframe><p>And this is <strong>counter.py.</strong></p><pre>from flybywire.core import App<br>from flybywire.dom import h</pre><pre>def CounterView(count):<br>    &quot;&quot;&quot;A simple functional stateless component.&quot;&quot;&quot;<br>    return h(&#39;h1&#39;, str(count))</pre><pre>class CounterApp(App):<br>    def __init__(self):<br>        &quot;&quot;&quot;Initialize the application.&quot;&quot;&quot;<br>        super().__init__()<br>        self.set_initial_state(0)</pre><pre>def render(self):<br>        &quot;&quot;&quot;Renders view given application state.&quot;&quot;&quot;<br>        return h(&#39;div&#39;,<br>                    [CounterView(count=self.state),<br>                     h(&#39;button&#39;, &#39;+&#39;, onclick = self.increment),<br>                     h(&#39;button&#39;, &#39;-&#39;, onclick = self.decrement)]<br>                )</pre><pre>def increment(self, e):<br>        &quot;&quot;&quot;Increments counter.&quot;&quot;&quot;<br>        self.set_state(self.state + 1)</pre><pre>def decrement(self, e):<br>        &quot;&quot;&quot;Decrements counter.&quot;&quot;&quot;<br>        self.set_state(self.state - 1)</pre><pre>app = CounterApp()<br>app.start()</pre><h4>How it Works</h4><p>The structure of the code above should be very familiar to anyone who has worked with React before. <em>flybywire</em> provides a helper function for building DOM structures. It is also possible to compose the UI out of stateless, functional components (like <strong>CounterView</strong> in the above example). This DOM structure is then converted into JSON format and sent to the browser over websockets using the <a href="http://www.autobahn.ws/python/">autobahn</a> library. The javascript part of the library parses the virtual-DOM structure out of the JSON data and patches the existing DOM wherever it has changed. Any DOM event callbacks are also setup to be passed back to the Python app over websockets.</p><p>Some of the core parts of this library such as the event server and event processor were directly taken from Sofi with a few modifications. Instead of Bootstrap-based widgets, <em>flybywire </em>allows you to define your own components that can be composed to form whatever widget you need.</p><p>It is now possible to make some really simple apps using <em>flybywire</em>. However, the library is at a very early stage, given that the First Commit in the repository was only three days ago!</p><h4>What doesn’t work</h4><p>Here’s some things that are missing and/or acts weird:</p><ul><li>Some basic manipulations of the DOM elements such as focusing a particular input box or clearing the input box after a certain action is completed are currently not possible. This will probably require putting back some of the command system from Sofi that I had removed when making <em>flybywire</em>. This can also possibly be fixed by moving to a platform like Electron which may give greater control over the rendering process.</li><li>The server shuts down as soon as you close the browser window. This is done on purpose as there is currently no way to reset the application state without restarting the server. The shared state also results in weird behavior if you open multiple windows. Once we move to a platform like Electron, <em>flybywire</em> will have hopefully have more fine control over the life cycle of the application.</li></ul><h4><strong>Coming Soon</strong></h4><p>I will probably add a few more features over the weekend, particularly a better Component class for creating stateful components. Any suggestions/comments/ideas and constructive criticism are welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad2131d4cbc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>