Include "Export to CSV" function to script output #3339

Closed
opened 2025-12-29 18:27:54 +01:00 by adam · 4 comments
Owner

Originally created by @WillIrvine on GitHub (Feb 17, 2020).

Environment

  • Python version: 3.6.9
  • NetBox version: 2.6.12

Proposed Functionality

We have been heavily utilizing the new "scripts" function through Netbox since its release, Its been fantastic being able to convert some of our scripts that were previously external to the application to make them accessible through it.

Some of our scripts do not include data changes and instead act as advanced data exports / reports. These are designed to pick up where Export Templates lack the available functionality (Linking multiple models together, performing calculations, being able to script in native django, etc).

What would be great would be able to include an "Export as CSV" button to the "output" tab of the successful script. Or have a option next to the "Commit Changes" tickbox to "Output to CSV"

This means we can have an external copy of changes that have been made (if changes are made), or in our case export the advanced report without having to copy into a notepad and convert to .csv format.

  1. Export to CSV
    image

  2. Output to CSV
    image

Use Case

Allow advanced data exports to be made through the new Scripts function.

Database Changes

None

External Dependencies

None

Originally created by @WillIrvine on GitHub (Feb 17, 2020). <!-- NOTE: IF YOUR ISSUE DOES NOT FOLLOW THIS TEMPLATE, IT WILL BE CLOSED. This form is only for proposing specific new features or enhancements. If you have a general idea or question, please post to our mailing list instead of opening an issue: https://groups.google.com/forum/#!forum/netbox-discuss NOTE: Due to an excessive backlog of feature requests, we are not currently accepting any proposals which significantly extend NetBox's feature scope. Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.9 * NetBox version: 2.6.12 <!-- Describe in detail the new functionality you are proposing. Include any specific changes to work flows, data models, or the user interface. --> ### Proposed Functionality We have been heavily utilizing the new "scripts" function through Netbox since its release, Its been fantastic being able to convert some of our scripts that were previously external to the application to make them accessible through it. Some of our scripts do not include data changes and instead act as advanced data exports / reports. These are designed to pick up where Export Templates lack the available functionality (Linking multiple models together, performing calculations, being able to script in native django, etc). What would be great would be able to include an "Export as CSV" button to the "output" tab of the successful script. Or have a option next to the "Commit Changes" tickbox to "Output to CSV" This means we can have an external copy of changes that have been made (if changes are made), or in our case export the advanced report without having to copy into a notepad and convert to .csv format. 1. Export to CSV ![image](https://user-images.githubusercontent.com/32685892/74616101-c4f5c800-518a-11ea-848f-21e142a1c7f7.png) 2. Output to CSV ![image](https://user-images.githubusercontent.com/32685892/74616183-4188a680-518b-11ea-8077-4bc1b622c6c1.png) <!-- Convey an example use case for your proposed feature. Write from the perspective of a NetBox user who would benefit from the proposed functionality and describe how. ---> ### Use Case Allow advanced data exports to be made through the new Scripts function. <!-- Note any changes to the database schema necessary to support the new feature. For example, does the proposal require adding a new model or field? (Not all new features require database changes.) ---> ### Database Changes None <!-- List any new dependencies on external libraries or services that this new feature would introduce. For example, does the proposal require the installation of a new Python package? (Not all new features introduce new dependencies.) --> ### External Dependencies None
adam closed this issue 2025-12-29 18:27:55 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 17, 2020):

This would not make sense as the output of a script is completely arbitrary, and defined by the script's author. It might be CSV, it might be JSON, it might be unformatted text, it might be nothing. This approach was chosen specifically to enable the greatest degree of flexibility when writing custom scripts.

One thing we could feasibly do is to return the output of the script as a downloadable file, instead of the UI response. However, this doesn't strike me as very clean, as the logging data (which is intended to serve as the primary output) would be lost, and the user would be left with a form that appears completed but not submitted after downloading the file.

A similar approach would be to save the resulting output to disk locally, and return a response to the user indicating where they can access the file for download. IMO this is beyond the scope of intended use for custom scripts as it would introduce concerns around permissions, garbage collection, etc. And most importantly: there's nothing preventing a custom script author from doing this already. Your script can simply write the desired output to a local file in a directory served by the httpd (e.g. nginx or Apache); the script can then log a line containing a link to where the user can download the file.

@jeremystretch commented on GitHub (Feb 17, 2020): This would not make sense as the output of a script is completely arbitrary, and defined by the script's author. It might be CSV, it might be JSON, it might be unformatted text, it might be nothing. This approach was chosen specifically to enable the greatest degree of flexibility when writing custom scripts. One thing we could feasibly do is to return the output of the script as a downloadable file, instead of the UI response. However, this doesn't strike me as very clean, as the logging data (which is intended to serve as the primary output) would be lost, and the user would be left with a form that appears completed but not submitted after downloading the file. A similar approach would be to save the resulting output to disk locally, and return a response to the user indicating where they can access the file for download. IMO this is beyond the scope of intended use for custom scripts as it would introduce concerns around permissions, garbage collection, etc. And most importantly: there's nothing preventing a custom script author from doing this already. Your script can simply write the desired output to a local file in a directory served by the httpd (e.g. nginx or Apache); the script can then log a line containing a link to where the user can download the file.
Author
Owner

@WillIrvine commented on GitHub (Feb 18, 2020):

Thanks for the response Jeremy

One thing we could feasibly do is to return the output of the script as a downloadable file, instead of the UI response.

Would a mix of these work? Have the output accessible via the output tab as it currently is. but also have an "Output to file" button on the output tab page that produces a generic file with no extension.

This would retain the logging data as the primary output, keep the flexibility of the user deciding what the output will be, but also make it easily accessible to download for the users of the script should they want a copy.

@WillIrvine commented on GitHub (Feb 18, 2020): Thanks for the response Jeremy > One thing we could feasibly do is to return the output of the script as a downloadable file, instead of the UI response. Would a mix of these work? Have the output accessible via the output tab as it currently is. but also have an "Output to file" button on the output tab page that produces a generic file with no extension. This would retain the logging data as the primary output, keep the flexibility of the user deciding what the output will be, but also make it easily accessible to download for the users of the script should they want a copy.
Author
Owner

@jeremystretch commented on GitHub (Feb 21, 2020):

Would a mix of these work?

No, I don't think so. It's important that we maintain a predictable workflow, where we return either a UI page (as we do currently) or present a file for download (ideally via a 302 redirect to keep the form clean). Having to account for either scenario in perpetuity frustrates development.

@jeremystretch commented on GitHub (Feb 21, 2020): > Would a mix of these work? No, I don't think so. It's important that we maintain a predictable workflow, where we return either a UI page (as we do currently) _or_ present a file for download (ideally via a 302 redirect to keep the form clean). Having to account for either scenario in perpetuity frustrates development.
Author
Owner

@WillIrvine commented on GitHub (Feb 21, 2020):

Ok. Will close this off as I can't see a good way around that limitation. I've found a way to get the file emailed via smtp relay which fits the workflow I'm after. Thanks.

@WillIrvine commented on GitHub (Feb 21, 2020): Ok. Will close this off as I can't see a good way around that limitation. I've found a way to get the file emailed via smtp relay which fits the workflow I'm after. Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3339