In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
User
write me a program in nodejs with express and ejs. i want it to recursively scan a directory, and serve a nested list of files and folders as a webpage
Assistant
User
Recreate the similiar app in Python with Django
Assistant
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Here is an example program in Node.js using Express and EJS that recursively scans a directory and serves a nested list of files and folders as a webpage:
code
const express = require('express');
const app = express();
const ejs = require('ejs');
const path = require('path');
const fs = require('fs');
// Set the views directory and the view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Define the route for the directory listing
app.get('/list/:dir(*)', (req, res) => {
// Get the directory path from the URL parameter
const dirPath = path.join(__dirname, req.params.dir);
// Check if the directory exists
if (fs.existsSync(dirPath)) {
// Read the contents of the directory
const files = fs.readdirSync(dirPath);
// Create an array to hold the nested list data
const listData = [];
// Iterate over each file/folder in the directory
files.forEach(file => {
// Construct the full path to the file/folder
const filePath = path.join(dirPath, file);
// Check if the item is a directory
if (fs.statSync(filePath).isDirectory()) {
// Recursively call this function to get the nested list of the subdirectory
const nestedList = getNestedList(filePath);
// Add the nested list data to the current item
listData.push({ name: file, type: 'directory', items: nestedList });
} else {
// Add the file data to the current item
listData.push({ name: file, type: 'file' });
}
});
// Render the EJS template with the nested list data
res.render('list', { dirPath, listData });
} else {
// If the directory does not exist, return a 404 error
res.status(404).send('Directory not found');
}
});
// Define the function to recursively get the nested list of a directory
function getNestedList(dirPath) {
// Read the contents of the directory
const files = fs.readdirSync(dirPath);
// Create an array to hold the nested list data
const nestedList = [];
// Iterate over each file/folder in the directory
files.forEach(file => {
// Construct the full path to the file/folder
const filePath = path.join(dirPath, file);
// Check if the item is a directory
if (fs.statSync(filePath).isDirectory()) {
// Recursively call this function to get the nested list of the subdirectory
const subList = getNestedList(filePath);
// Add the nested list data to the current item
nestedList.push({ name: file, type: 'directory', items: subList });
} else {
// Add the file data to the current item
nestedList.push({ name: file, type: 'file' });
}
});
// Return the nested list data
return nestedList;
}
// Start the server on port 3000
app.listen(3000, () => console.log('Server started on port 3000'));
This program defines a route /list/:dir(*) that takes a directory path as a parameter and returns a webpage with a nested list of files and folders in that directory. The getNestedList() function is used to recursively scan the directory and construct the nested list data. The EJS template views/list.ejs is used to render the webpage with the nested list data.
Note: This program assumes that the directory being scanned is a local directory on the server running the Node.js application. If you want to scan a directory on a remote server or from a client-side web application, you will need to modify the program accordingly.
from django.shortcuts import render
from django.http import HttpResponseNotFound
import os
# Define the view for the directory listing
def directory_listing(request, dir_path):
# Get the full path to the directory
full_path = os.path.join(os.getcwd(), dir_path)
# Check if the directory exists
if os.path.isdir(full_path):
# Read the contents of the directory
files = os.listdir(full_path)
# Create an array to hold the nested list data
list_data = []
# Iterate over each file/folder in the directory
for file in files:
# Construct the full path to the file/folder
file_path = os.path.join(full_path, file)
# Check if the item is a directory
if os.path.isdir(file_path):
# Recursively call this function to get the nested list of the subdirectory
sub_list = get_nested_list(file_path)
# Add the nested list data to the current item
list_data.append({'name': file, 'type': 'directory', 'items': sub_list})
else:
# Add the file data to the current item
list_data.append({'name': file, 'type': 'file'})
# Render the HTML template with the nested list data
return render(request, 'list.html', {'dir_path': dir_path, 'list_data': list_data})
else:
# If the directory does not exist, return a 404 error
return HttpResponseNotFound('<h1>Directory not found</h1>')
# Define the function to recursively get the nested list of a directory
def get_nested_list(dir_path):
# Read the contents of the directory
files = os.listdir(dir_path)
# Create an array to hold the nested list data
nested_list = []
# Iterate over each file/folder in the directory
for file in files:
# Construct the full path to the file/folder
file_path = os.path.join(dir_path, file)
# Check if the item is a directory
if os.path.isdir(file_path):
# Recursively call this function to get the nested list of the subdirectory
sub_list = get_nested_list(file_path)
# Add the nested list data to the current item
nested_list.append({'name': file, 'type': 'directory', 'items': sub_list})
else:
# Add the file data to the current item
nested_list.append({'name': file, 'type': 'file'})
# Return the nested list data
return nested_list