Auto download result from custom script as file

182 views
Skip to first unread message

Mats Brorson

unread,
May 14, 2021, 6:57:03 AM5/14/21
to NetBox
Allright, next challenge:-)

I have a script that gather some information from various models that the user would like to download as a file (in this case CSV)

Now returning my data ends up in the results part when script is done. 

Next I tried to use Django HttpResponse to automatically stream it as a file to my client, just as done when exporting a model from the UI. Code snippet I tried, just for test purpouse:

        response = HttpResponse(
            content_type='text/csv'
        )
        response["Content-Disposition"] = "inline;filename='output.csv'"
        writer = csv.writer(response)
        writer.writerow(['object1', 'A', 'B', 'C'])
        writer.writerow(['object2 ', 'A', 'B', 'C',])

I does not stream, but under the output tab I get:
<HttpResponse status_code=200, "text/csv">

Is it possible to achieve what I want, or am I doing it wrong?

felix ezequiel Andre

unread,
Nov 12, 2021, 10:37:35 AM11/12/21
to NetBox
Hello Colleagues, 

I want to do something very similar to what Mats asked. 

In my case, to download a .txt file with a configuration snippet for device interfaces. All information will be gathered by existing models. 

The web UI looks like this.

When clicking in Run, it will gather the variables in some dictionary and render a config template. 
The result .txt would look like this: 

#--------------------------------------------------
echo "Router (Network Side) Configuration"
#--------------------------------------------------
    router Base
        interface "to-DC01"
            port 1/1/1
            address 192.168.10.2/30
            no shutdown
        exit
        interface "to-DC02"
            port 1/1/2
            address 192.168.11.2/30
            no shutdown
        exit
        interface "system"
            address 10.0.10.1/32
            no shutdown
        exit
#--------------------------------------------------

Finally, I would like the end-user to download this file. 

My questions:
- Is it possible to do what I'm trying to do? 
- I'm lost in the part of how to download a file. Could you share an example?
- About how to render my config template: Within the python script, I could have something like:

var_config_file = ( "#--------------------------------------------------\n" "echo "Router (Network Side) Configuration"' "\n" "#--------------------------------------------------\n" + var_basic_config + var_config_interfaces + 
"#--------------------------------------------------\n"
        )

But it would be nicer to use some Jinja2 templates[1] to feed get the configs. I'm thinking this will grow to more large configurations.

i.e Jinja2 folders:

├── jinja_templates │   ├── common │   │   ├── cpu.j2 │   │   ├── log_events.j2 │   │   ├── interfaces.j2 │   │   ├── sys_config.j2 │   │   ├── services.j2 │   │   └── user_security.j2


I went through this tutorial[2] and is the basic concept but did anybody make something similar?

I welcome any advice or guidance :-)

Best Regards,
Felix 

get_config.png
Reply all
Reply to author
Forward
0 new messages