We are very excited to announce a new feature that allows you to add files to your Python trinkets. Many users have additional modules they’ve written to import, data files they would like to read in and manipulate, and other types of files that make really powerful programs. Before today you would need to copy and paste the contents of those files into your trinket and use them directly. Today that’s all changed with a new tab interface that lets you create new files and separate out your main program from other content.
Let’s take a look at a simple example that creates a module for calculating the Fibonacci series and imports it.
You’ll see two tabs: main.py and fibo.py. Click on the fibo.py tab to see the implementation of the methods used for doing the calculation. By putting methods into modules like this your students can focus on using the methods you provide yet they can still see how they were written. This also teaches best practices by keeping separate concerns in their own files. Remember: to make the methods from your additional files available to your main program you’ll use the import filename (without the .py).
You can also use the built-in open method to read in contents from any tab. Here’s another example that reads in a simple JSON file, parses the content and uses Pygal, a charting library, to display the results.
To add another file simply click the plus icon to the right of your main.py file and you’ll be prompted to name the file. If you’re writing a module to import you’ll need to follow Python conventions for naming your file.
You’ll be able to rename files or delete them if they’re no longer needed by selecting a tab and clicking on the gear icon.
We’re looking forward to seeing how this improvement changes your use of trinkets and can’t wait to see some of the cool programs you and your students create. Share your new programs with us on Twitter at @trinketapp or email us at email@example.com. Of course we’d love your feedback on this update and the next thing you need to create ever more powerful trinkets.