Hannah Fry recently did a great Numberphile video about how train timetables are made I took that one further and put pen to pixel!
Train graphs are quite a niche field, let alone train timetables. You’re more likely to cure someones insomnia than trigger some kind of curiosity when it comes to train timetables. Even though Hannah Fry does a great video explaining how these are made, check it out here! Alas over the last few years my professional career has involved looking at or the result of these timetables quite regularly unfortuatnely.
UK train timetables come in many forms, posters at your local station or more commonly as a results of a search in google maps or the trainline app (other brands also available). These timetables are typically static, agreed months in advance and updated twice a year as a baseline timetable provided to customers.
In reality, these timetables change a lot, one recent example is some of the practices used by Transpennine to alter tomorrows timetable, though in this case not great news for customers in this case. In railway command and control operations sometimes operators need to look at one train in detail, how 2 trains interact with each other or how the entire service is operating, particularly if its in major disruption.
In the later case its difficult to look at a timetable poster or the state of the railways and see whats going on. This is where train graphs can come in useful, not just for planning but in operations as well. I recently moved to Newcastle and wanted to get an idea of the local Metro service timetable, I wanted to see if I could create a web accessible train graph of the timetable.
As with all ideas these days, I asked ChatGPT for some advice for a good web application framework I could use to get started with, one of the top ideas that stood out to me was Dash/Plottly. After 10 mins of google and youtubing research it looked like a good fit.
I couldn’t find Nexus Metros timetable anywhere in a useful format, thankfully someone had requested these via What Do They Know but in Excel. After a quick bit of excel magic, we had a csv of the timetable listing each trains, id, location and departure time in one big list.
I began by importing the necessary libraries, including Dash, Plotly Express, and pandas. We then loaded in my timetable csv file, and used Plotly Express to create a scatter plot of location vs departure time. Next to add in a line graph and connect the points grouped by the train ID.
Add in a bit of customisation and functionise the python code and this could be deployed very quickly and easilty into a web application.
Next, we used Dash to create the layout for our web application, which included an H1 heading, a Div with text describing our scatter plot, and a Graph component displaying our plot. In particular taking advantage of Bootstrapping to make things look a little sleeker. Once it was good to go and working in a development server locally it was time to zip it up.
Finally, I deployed our zipped up web application to AWS Elastic Beanstalk. This required creating an Elastic Beanstalk application and environment. We uploaded this zip file to Elastic Beanstalk and launched our web application, which was now accessible to anyone with an internet connection. Match that with a simple routing through my domain and we now have http://nexus.stuart-gordon.co.uk.
Overall, we found that using Plotly and Dash made it relatively straightforward to create a powerful and interactive web application. And with Elastic Beanstalk, we were able to deploy our application quickly and easily, making it accessible to a wide audience, a quick integration with a CI/CD pipeline through git hub and we have a deployment pipeline fit.
Its great to see what the Nexus timetable looks like particularly to see the additional set up units for the morning and evening peak as well as the ECS movements at the end of these periods. Unfortuantely this is all static data at the moment, although the framework is interactive.
It would be great to see actual performance against the timetable so we could see ‘how the day is going’ or if there was an incident, where did it cause an impact and how did the delay propagate though the day.