With every web app there comes a time when you will need to serve up some static files. Maybe it is the JS or image files, or maybe you are just trying something out. Let’s talk about how to handle this in Flask.
“Why flask? Why not Django?”
Recently I was asked this after introducing a new subsystem at work. Originally part of a monolithic Django app, this was new micro-service was one of the first pieces to be split out on its own. I had chosen Flask to be the web framework, and now it was time to explain why…
I’m not anti-Django, but…
Recently I decided to pull an old project out and use it as the basis of a new exciting thing I’m working on. More on that later! The old code was running on Google App Engine (GAE) and was written using the python web2app framework. Let’s take a look at the why and how of converting from web2app to flask.
GAE is pretty generous when it comes to letting developers run small apps on their service. That is a great way to get developer buy-in on your product. For years I’ve been running a couple of small projects for free on GAE and then talking about them in interviews.
The documentation for GAE used to use web2app as the Python sample code. Over the years they have evolved the service and documentation and today they tend to show Flask and Django when they are displaying sample code.
As a result of this, all of my GAE hosted projects have wound up being on web2app. And in the case of my current project, this is a problem.
Why switch to flask?
While doing some performance test on a new flask microservice, I noticed it was not handling very very many connections per second. Additionally, nginx (which we are using as our front end webserver) was reporting a ton of errors. WTF?!? I thought flask was fast. I need a faster flask!
Recently I was working on a project that involved breaking up a Django app into smaller services. As we looked at the new services it occurred to me that we might be able to use Flask instead of Django. It was pretty straight forward to move things to Flask, but there were a couple of little gotchas that I thought I would share on moving from Django to Flask.
Network troubleshooting is an essential skill here in the age of the cloud. As systems become more and more distributed (hello microservices!) the network connection between them becomes more and more important. Let’s look at some quick and basic techniques you can use to try and understand what is going on with the network.
Linux: Your best friend/worst enemy
I have found that linux (or some other unix environment) is the best tool with which to do network troubleshooting. It has very powerful tools with installed by default, or they are only a quick “apt-get install” away. Windows does have good tools also, but it seemed to me that linux had the edge for many years. At any rate, I recommend using linux for anything development related.
One thing to be aware of: while the linux tools are powerful, the documentation around them can be a little bit… difficult. This is partly because the tools are being written and used by experts, and partly because its difficult to make some of these topics accessible in a quick manner.
Let’s dive in!
Over the last year I have spent a lot of time with Flask which is a python web framework. It is commonly referred to as a micro-framework because it tends to be lightweight and make use of plugins (as opposed to having everything built in like Django).
These plugins are what makes Flask great, there is a great variety of common programming tasks that can be accomplished using these plugins. This helps you the programmer because now you don’t have to reinvent the wheel. Without these flask resources you would wind up recreating a lot of code.
So here are the Flask plugins and tools that I have found really helpful while developing websites for my clients.
In my last post, I talked about some great resources to help build your software development skills. In this post I’m going to share some more resources that I have found very useful to both learn something new, and to get a quick refresher on a topic.
Awww, but I don’t want to read!
In my last post I talked about some great newsletters that will literally bring the most interesting projects and tutorials right to you. But not everyone learns best by reading. And sometimes there’s just no substitute for someone showing you something. This is where the magic of the internet shines, because we have tons of great video resources out there.
Pycon is a great conference. I’ve was lucky enough to get to attend it a few years ago, and I had such an awesome time it convinced me I needed to switch from working in Java to working in Python. There are so many great talks and demos that are given every year, it can be really hard to catch them all! Plus as you mingle with the awesome python community it is really easy to miss out on important tidbits in the talks.
But never fear, most of the talks are recorded! And the best part is THAT THEY ARE ALL AVAILABLE FOR FREE. Here’s where you need to go:
- Search Youtube for “pycon”
- The awesome (but soon to shutdown) pyvideo.org (Note, the data powering this site is available here on github)
- More awesome python videos (a curated list)
These videos cover a wide range of python topics, from frameworks to algorithms, to internals of CPython, to best practices, to inspiring hacks. They are truly great. This is why software development is a great field to be in, we love to share the knowledge. Thank you PSF for making this happen!
But I’m not a python developer!
My condolences. 😉 But in all seriousness, there are a ton of videos available for a ton languages and platforms. Here’s my recipe for getting ahead of the curve in software development no matter what your language or skill:
- Go to youtube.com
- Search for just the name of your language or framework or tool
- Look to see if there are any “channels” dedicated to that topic
- Start watching!
Be sure to make a note of the sites that are creating the videos. If you then go an google for them you will find even more resources. There are other video providers out there (like Vimeo) but honestly Youtube is where you are going to get your biggest returns. Don’t overlook the others, but hit youtube first.
I need someone to hold my hand
That’s cool, we all need that from time to time. If the youtube videos aren’t quite doing it for you, and you need a little more of a guided tour on a topic? I’ve got a great resource for you: The O’Reilly Learning Paths.
Walking on a cowpath vs building a road
O’Reilly makes great technical books (I would know, I have a bunch of them) and in the last few years they have begun making videos courses. I have bought a few of these (Mastering Git and Machine Learning among other) and absolutely love them. Great instructors, great pacing, and you can rewind if you miss something!
The only downside was that the video courses could get a little bit expensive. Investing in yourself is important, so you shouldn’t fret about spending $79 for a video. But… if you’ve got 5 related video courses you want watch, it will get expensive pretty quickly.
O’Reilly recognized this and created bundles of these videos called “Learning Paths“. The bundles have a theme, be it mobile development, front end development, or even Python development. Each bundle seems to have at least 3 of their video courses rolled into it for a reduced price. The price varies from bundle to bundle, but basically what I’ve seen is you get at least 3 video courses (usually more) for what you normally pay for 2. That to me is a really great deal.
Wrapping it up
Videos are a great way to improve your skills when it comes to software development. I hope you can use these resources to help you become a better version of yourself! Let me know if you have any great resources or questions. I’m on twitter as @nloadholtes
If you are going to make web pages, you might as well make them fast. No one like sitting around waiting for something to load up. NO ONE. Don’t be that website. Be fast! Optimize for an awesome visitor experience!
Lets talk about some tools to help speed up your pages. If your site isn’t on the public internet for whatever reason (and yes, there are a lot of pages that can’t run on the public internet for very good reasons) you can still profile and optimize your pages using the Chrome Developer tools.
Firefox and other browsers also have developer tools. I personally favor Chrome, so I’m going to focus on that one for this article. Your browser of choice probably has tools that are very similar, I encourage you to learn them!
Unleash the Chrome Developer Tools
Click on “View->Developer->Developer Tools” to get started. From here you can see a tool bar now splits your browser window:
The tab you are interested in is “Audits”. Click on the “Run” button and it will reload your page while profiling it to see where the code is spending all of its time. When it is done running you’ll get a nice little report that is color coded to let you know where the trouble spots are.
This information is really great. In this case it is pointing out that reorganizing some script tags and a CSS call will allow the browser to optimize the downloading of the files. That should allow the browser to render the page faster (which means the user will see the content quicker, and thus be happier).
This report will also tell you not-so-useful things however. For example, the line about “base.css: 65% is not used by current page” is not really helpful. For this project base.css is where all of the css for the site has been condensed into. For this page, only 35% of the styles in that page are being used. This is ok, other pages will use other styles. It would be nice to know if there are styles that are not used anywhere in the site, but that’s a tall order to fill and these tools don’t offer that insight.
Check out GTMetrix to optimize for the network
If your site is on the internet, GTMetrix is a great tool. It is similar to the Chrome Developer tools, but it runs from a computer somewhere on the internet. This allows it to see a more realistic view of your page’s performance because it involves actual network data transfers. (The chrome tools run locally so they don’t always reflect the true performance that an end user will experience.)
To use this tool you tell it the URL of your site, and then it send a browser to look at it. That browser is configured to run some performance tools that will analyze its performance. This allows you to see some different opinions about your site and how you could optimize it. These tests will tend to find things like places where assets could/should be compressed, and also where you could be using a Content Distribution Network (CDN) to speed up your page rendering and delivery.
This is a great tool to double check your work. Its free, so I highly recommend running your site on it at least once to see how your site looks to the outside world. Thanks to the 100 MBA for recommending this great resource!
Hopefully this post has given you some ideas on how and why you should optimize your website. Please go and try them out! If you can make your website 20% your customers will love it! Be sure and drop a note to let us know how these tools work out for you!
In the last few posts we’ve talked about why experiments are important (you are doing some right?), and how to use some very simple tools to get very insightful information. But one thing that we have not discussed is how to get people to actually use your experiments.
In the first post the idea of statistical significance was presented. This basically states that you can’t decide between two things until you have had enough people look at them. For example, if you are testing out 2 different “call to action” buttons, you are going to need more than 10 people to look at those buttons (and click on one or the other) before you can make a real decision about which one is better.
So using Evan Miller’s A/B site we can see that most of the time we will need several hundred users (or visits, I’m using the terms interchangeably here) to look at your experiment. The next question you need to ask yourself if is “How long will I have to wait until I get that many views?”. You do know how many people visit your site, right? If not, stop and install something like google analytics or addthis right now!
Look at my experiment!
Let’s say you decide you will need 1,000 views to determine the outcome of your experiment. But, your website only gets 100 views a month. That means it will take 10 months to determine the winner of the experiment! That is way too long to wait, in that time all kinds of external factors could come into play and affect the outcome. Not to mention if your business depends on this answer, you could be out of money well before then!
Obvisiouly you need more people to visit your site. This is the eternal struggle of all businesses both on the internet and in real life. Here are some ideas on how to drum up some visitors to your site. Please note that these ideas will get people to your site, but that does not necessarily mean that they will purchase things from your site.
One way to quickly get quality views of your site is to purchase ads that will take users to your content and experiment directly. The advantage of this is that as long as you set up your ads correctly, you should be getting very targeted people. This means that they will be more likely to engage (e.g. do something) with your website. The downside is that this can get expensive over time. In short timeframes though, this is a really good approach to quickly testing something out.
As far as cost, it is a case of you get what you pay for. Costs are typically per-click on the ad, so if you get a ton of clicks it will cost more. You can also set how much you want to pay per click. The details for this are specific to each ad vendor, so be sure to check with them for the fine print. Here’s some of the heavy hitters in this area:
This is a cheaper alternative to buying ads, but the quality of the traffic is much lower. Depending on where you buy the traffic from you might be able to request a certain type of person visit your site (think: “someone interested in games”), but from what I have seen the overall traffic is typically not very good: The visitors don’t seem to engage or stick around for very long.
There’s several potential reasons for this which are beyond the scope of this post. The market for supplying this kind of traffic is… “fluid” so I’m not going to recommend any links. A quick web search will probably be able to get you in touch with the current industry leaders.
Experiment with Social Media
Using social media (like Facebook, twitter, etc.) is great for generating traffic to your site and your experiment. Its free, if done correctly its authentic (in that it reflects your brand), and it can be long lasting.
The downside is that it can take a while to get going. Not every posting goes viral, so this is a long game approach. Setting a schedule and posting good content regularly is key. Sharing the content with your friends and connections is also important and can help spread the word. Just be careful not to spam your friends too much, that can be very bad for your reputation and stats.
There are services out there that can help boost things on various social platforms. While these can work, they do require great content from you to get going. Bad posts, ugly pictures, uninteresting stories simply will not work no matter how much money you throw at them. If you do decide to use they types of services, I highly recommend you spend some time working on your content first. Content is king!
In this post we talked a little more about statistics and why its important to get people to visit your site and interact with your experiments. There’s lots of choices when it comes to getting people to look at your content. I personally recommend taking a long game approach and building up great content, and then augmenting it with ads periodically to get focused attention to specific experiments.
Good luck and let me know in the comments what works best for you!