Adventures in NotebookTemplating: Report Generation in the Real World

Anthony Zupnick walks through notebook templating for report generation, showing how it works in real projects. He covers practical examples of building reports from templates, both for internal use and client work.
Chapters
My name is Anthony Zupnick and I'm here to present Adventures in Notebook Templating Report Generation in the real world. So basically what I want to do today is talk about the notebook Templating packlet, but in more of a use case scenario presentation where I show you actual use cases that are used in the real world. So get started. So first of all, I want to, you know, talk to you about what notebook templating is. So it's a packlet which allows you to create reports using template notebooks. So move this over to the left here. So here we've got a notebook template and you know, it has, it has some slots which you can ingest data into and some code which evaluates on generation. So what we do is we feed data into that template. So here we have some data which we run, generate document with. And when we run this report, we get an output like this. So we get a nice little interactive report which then you can send to people. You can deploy this to the cloud, you can package it as a PDF, although if you package it as a PDF you lose the interactivity. You could deploy it as a CDF if you wanted. So let me just show you basic example of notebook templating. So first of all, want to look at some data. So apologies, this might be a little small to read, but we've got a data set with some people and some sales data, some fictitious sales data that I created. And we're going to create a template notebook. You can do so by going into file new template notebook. And we're going to just create a report. So let's make a report that's going to run on demand, but I'm going to give the date. So I'm going to make this an expression that run, that runs on generation. So it's going to produce my date string and then I'm going to loop over some, loop over the name. So sales. For now we want to look at this data set quickly. Well, we've got the key, I mean we can look at the keys. I guess we can go for the first one. So we've got name, age, gender and sales. So we want to, first of all, we want to loop over a, over the data and. Oops, sorry. So we're looping over the data and then we're going to give the name here. And then we want to do a plot for that person, that person, sales data. So it's a list plot of their sales data. Sales. And then we can put their age in. Why not? There the age of the person is age. And we're going to add some cell behavior where we evaluate the information and we evaluate the cell and then we delete. So now with our, we've added our template items and now we're going to generate this document. Generate document. We provide the notebook, which can be a file, it can be a cloud object. In this case, it's a notebook object. And we're supplying our data where we've normalized the data set because we need it in as a list of associations. And when we run this, what have we done wrong? We've, what we have is we have. This stuff is not grouped together. So what we can do quickly is add some grouping and then we'll see that this applied. This loop applies over this, over this group. So when we run this now we have our report, report just ran right now. And we've got the sales data for John Smith, Emily Johnson and you see he's got their ages, etc. Etc. So we made a nice quick little report in a couple of minutes. So just to let you know, you know, there's many different names for notebook templating. Notebook templating is the official, official packlet name. But historically we've used different terms for it such as report generation and document generation. In fact, there's a function called document generator which is really for generating reports from, from, from a, from a document. So let me talk to you about some use cases. So, yeah, there are some questions and I will probably answer these questions at the end. I will increase the size of these, the zoom level of the notebooks. I thought that 150% was fine actually. So my apologies, that isn't, isn't large enough. So I'll increase it as I go along. It's going to be a bit fiddly. So first use case is a compiler benchmark. So I was asked to create a benchmarking system for the compiler so that we could test the speed of the compiler, the initialization compile times and execution times. And this data was pushed to data bins from our benchmarking machine. But once we have that data, what do we do with it? Well, we want to be able to visualize that data in some way. The notebook templating packlet was the perfect way to do so. What we have is we have a template like so make this a bit bigger for you where we have, you know, when the report was run a day. There's a, there's a bunch of stuff here. You really need to understand what's happening here. Exactly. But they, it basically shows some plots and it goes over all the, all the benchmarking, all the different benchmarks and visualizes them. So, so what we get is, well, we have a, the document generator, Document generator looks like in the cloud looks like this and we can run reports on demand. So if I was to run a report now, for example, it will take a bit of time. I suspect by the time I click this link it won't be evaluated. But let's have a look. So we click on the report button and we have an example report which is available on the cloud. So we can see that this report ran at quarter past 12 and I think that's UK time because I was the one who, who set all this up. And, and it uses, it uses my date times, but I could have adjusted the time zone if I wanted it to be an American time. And we can see it ran on a 64 bit Linux system. We can see a quick snapshot of the, of the compile times and then we can see bigger bits of information where we can see some details of the last build, the code that was actually run. And then finally we've got. This stuff has been running quite a long time since 2021. And we can see the history of these compile times. So it gives us an idea of whether or not the compiler is getting faster or slower. And for the most it's getting faster. You know, there was a couple of instances where it's been a bit, a bit of a yo yo. So, so yeah, this is a really good example of how internally we've used the report generation system to create, to create. Well, to, to, to, to create reports for, to solve a specific problem. So a couple of years back we met with a financial firm to showcase new features of, of Mathematica. And one of the things that we, we, you know, they showed us what they were doing and basically they had, they were doing some calculations with Wolfram application server and they were pushing that, these, they were running some calculations and they were pushing this, this information to Excel and they, you know, they were using Excel links so they could push, you know, more easily push things into certain, in certain cells. So, but really what they were doing is they were using Excel to, to for the report generation. They weren't aware that notebook templating existed as a package for people to be able to use. So we showed them the report generation stuff and they thought that that was really, really good. They wanted us to create a dummy example for them to see if this was Useful for them. So, so yeah, we will basically were given a Excel spreadsheet. Excel spreadsheet sort of looks something like this and it has some data and we had a. We created template. I'll make this bigger again. Created a template which eventually makes like a two page report. Let's look at one of the final ones here where it basically makes a grid where it connects everything together into a. For a single page. And there's two, there's basically two very large grids in this, in this report. And the idea was that this report can be outputted as a, as a PDF and looked very similar to the Excel reports that they were, that they were creating themselves. So you know, we've, we've got the template and we've got the data so we can use generate document to run this. And what we have is we have a two page report which looks quite nice. Hopefully you, you agree with this. And one thing that we can do is just to show you, in case you weren't aware, is that actually Wolfram language is quite good at printing printing to PDF. So let me just print this to PDF. I'll just open it directly in preview and we can see side by side that these two things are equivalent. So there are a couple of minor styling issues here, but this was a proof of concept and so we could have refined this further and made it even nicer. So other use cases internally, we use this for the compiler in a number of places. We have this package called the documentation called the compiler manual and I spelled it wrong. In the compiler manual. This is probably a little bit outdated, but we have some reports and we have this type report and this is just a different type of. Type of report where we've added a doc cell where you can search for. For types with some autocomplete we can get a link or sorry, make this a bit bigger for you and you know, it's got links to where these types are defined. All of which you can, you can find online. Oh sorry in, in the Mathematica layout. And we got the function report as well which has a list of all the functions that are known to the compiler. And we'll move this over here as well. I won't show you the template. I, I kind of forgot to. To add the, add the template to this presentation, I think. Yeah, but yeah, we can autocomplete again so we can find where the definition for cosine is and yeah, or you've got cosine is built into the Wolfram language and then we have A link to the, to the documentation where cosine is defined as well. And then we've got some type information here as well. So it's pretty, it's pretty nice to be able to have one place where we can search for all of the functions and have them laid out in this specific way. And this wouldn't really be possible without the, you know, the UI of. The UI of notebook templating. So it's made things a lot easier. So I found out more recently the machine learning group, well maybe I say more recently, maybe it was about a year ago now, were using notebook templating to where they were running some benchmarks similar to I guess what we're doing with a compiler and they wanted to generate reports. So here's our. Is there. I think this is, like I said, this is a bit outdated but they had this template which runs certain experiments and they have a report that's generated here with some kind of summary and some, some timings. Great. So these are internal. I do have one external example that we use which is the Wolfram Physics project. So the Wolfram Physics project, you can go to wolframphysics.org and we can look at models and it's quite nice looking models. I don't really understand this stuff but what you'll see is there's a editable. Well, there's a notebook that you can download for this model and this notebook is actually being generated from a notebook template. So can have a look just quickly at what the template looked like. So we had a template which runs through its model and does some loops and has some conditions. Sorry, I forgot to make it bigger. And yeah, so yeah, we see we have some template slots, then we have some conditions. I don't know if we have any template expressions. Right. So yeah, and you know, the report that gets generated looks like this, which is similar to what we saw on the Wolfram Physics website. So. Wow, okay. I, I ran through way faster than I thought I would actually. I guess I panicked because I lost a few minutes at the, at the start of the presentation. But let me conclude and then, and then, and then I'll be happy to answer your. Answer your questions. So. So basically what I hope I've shown you is a good display of notebook templating. Reports are very important to every company. I mean every company, pretty much every part company we speak to is interested in report generation when we mention it and they know it's a thing. One of our weaknesses is that we are. We. We haven't advertised this functionality Very well. Even though I think the notebook templating packet packlet is amazing, it's really, really useful and we, you know, when I, you know, even internally it's not well advertised and people find out about it and start using it using internally to generate their own reports. So it's very, it's very useful. So notebook templating has a, you know, has this intuitive interactive UI that allows you to create reports from a template and hopefully I've displayed that to you in a timely manner. So the one of the main reasons why this is obviously good is that you're reducing your requirement of using other software. So, you know, it's just another, another avenue, another piece of technology that Wolfram has that is fully integrated into the Wolfram technology stack. And they, you know, we see that notebook templating integrates with, with other elements of the Wolfram technology stack, like the Wolfram Cloud with Wolfram Datadrop. You know, it's, it's, it's seamless. It's, it's, it's, it's fantastic. So, so yes, thank you. And hopefully, hopefully you found that interesting. So let me answer some questions then. So can we template and templating notebook? So was this a question to, to create, to create a notebook a template which creates templates? Yes. I can't remember what the term we used internally for that meta templating, I think is what we called it, using templates to create templates. Yes, that is possible. And we created this language at some point. It's quite a few years ago now where, where you can write notebook template slot where we got these notebook template slots for the slots and notebook template expression for expression. Because these things didn't quite work a few years back. And basically if you want to be able to ingest the slots into your, your meta notebook template, then then you can do. So it's, it's not, not something that has, you know, been. No one's done this internally really. It's a feature that was added that I added some, some time ago. So, so yeah. So, right. Can you show how to automatically save as when you generate a report? Oh, okay. So in, I mean we can look at the generate document documentation. I don't know actually if it's these examples aren't. I mean, it's only got one example, but it does have some specifications at the top. You can see that if you want to specify a notebook argument in an output, then use this three argument specification and we can write generate document. So let's say we had template notebook. And I'll just do something very simple. So generate document and then we're going to say world goes to test and then if you want to save to an output to a specific location, should just be able to write output.bookbook here. Actually I won't even put this slash then because it will go to my, to my directory. So this now is in my directory, which is in my financial example. So I'll just open this up quickly so you can see it in, in the, in the example here. So yeah, here we go. Output notebook is very small. Sorry, I don't think I can make this, made this bigger for you. So, so yeah, we see the output. We can open this up and we can see hello test. So it's, it's, it's, it's replaced the world slot with, with test and it's created it to a, to a notebook. So this, you can, you can give this a cloud object as well. So if you want to give it a random cloud object, let's see what happens here. I haven't signed into the cloud, but now we've signed into the cloud and it's pushed it to this cloud object and we can see it in the cloud as well. So there are a few specifications. If we go back to the doc, the generate document documentation we can see, we can give it a file cloud object, notebook notebook object. So yeah, you can even give it just a notebook expression. It doesn't even have to be an object. So it's pretty nice. Anything else? So there was a question, How do you typically kick these off? Enterprise schedule or events, etc. Sorry, I'm not quite sure I understand the question. If you're talking about, you know, where do you start with notebook templating? Well, obviously you start with a template and then you can just run generate document with that template. If you want to start to add a scheduler, you use the document generator functionality which allows you to run it pushes that template to the cloud and allows you to set time specifications for how often you want to run that report. I didn't show this example because I thought I wouldn't have time. And you know, this was really a presentation for showing some use cases of the of notebook templating. So, so yeah, what we can see here is, you know, you run you cloud, deploy a document generator with a basic example here. And then if you want to just run your, your template instantly, you can use Task Execute, but you can set up specifications where you can run these reports hourly, daily or any kind of specification. You can imagine. Okay, Right. Sorry. Yeah, so you did clarify and I just carried on. So hopefully you found that. Hopefully you found that useful. So. Great. I think that's pretty much, you know, we've finished a couple of minutes early, but that's good because that means you can. You can take a. Have a. Have a cup of tea before the, before the next presentation. So thank you very much for listening, and I hope you have a great day.
More from Guy

Cheryl Strauss Einhorn | The Human Edge: Smarter Decisions in the Age of AI | Talks at Google

Rachel Glennerster | Innovation and the Center for Global Development | Talks at Google

Rebecca Winthrop | Rethinking the Purpose of Education in the Age of AI | Talks at Google

Dr. Robert Wachter | A Giant Leap: How AI Is Transforming Healthcare... | Talks at Google






