能否通过Neo4j Browser直接导出PNG/CSV?支持JS或Curl调用吗?
Hey there! Let's tackle your two Neo4j questions with clear, actionable details:
1. Can you directly export PNG/CSV files from Neo4j Browser?
Absolutely—this is built right into the browser interface:
- CSV Exports: After running your query, look for the "Download CSV" button at the top of the results panel. Click it, and your query results will save as a CSV file instantly.
- PNG Exports: When viewing a graph visualization (after running a query that returns nodes/relationships), you can export the view as a PNG. You'll find the option either in the top-right menu of the visualization area or by right-clicking directly on the graph canvas.
2. Can you automate PNG/CSV exports with JavaScript or cURL?
Yes, this is totally feasible—you just need to use the right tools instead of trying to replicate manual browser clicks. Let's break down both methods:
CSV Exports with cURL
You can leverage Neo4j's HTTP API to run queries and export results to CSV. Here's a working example (adjust credentials, endpoint, and query to match your setup):
# For Neo4j 4.x+ (using the database-specific query endpoint) curl -u your-username:your-password -X POST http://localhost:7474/db/neo4j/query \ -H "Content-Type: application/json" \ -d '{"query":"MATCH (u:User) RETURN u.name, u.email", "resultDataContents":["row"]}' \ | jq -r '.results[0].data[] | .row | @csv' > user-data.csv
Note: You'll need jq installed to parse the JSON response into CSV. If you prefer raw CSV output directly from the API, set the Accept: text/csv header instead—check your Neo4j version's docs for exact syntax, as endpoints can vary slightly.
PNG Exports with JavaScript (Headless Browser)
PNG exports rely on the browser's visualization engine, so you can't get them directly via the HTTP API. Instead, use a headless browser like Puppeteer to automate the Neo4j Browser workflow. Here's a simplified snippet to get you started:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ headless: "new" }); const page = await browser.newPage(); // Navigate to Neo4j Browser and log in await page.goto('http://localhost:7474'); await page.waitForSelector('#neo4j-browser-input'); await page.type('#neo4j-browser-input', 'neo4j'); await page.type('#password', 'your-password'); await page.click('[type="submit"]'); // Wait for the query editor, run your graph query await page.waitForSelector('#query-editor'); await page.type('#query-editor', 'MATCH (u:User)-[:FOLLOWS]->(f:User) RETURN u, f LIMIT 20'); await page.click('[data-testid="play-button"]'); // Wait for graph to render, trigger PNG export await page.waitForSelector('.graph-visualization'); await page.click('[data-testid="export-png-button"]'); // Capture and save the downloaded PNG const download = await page.waitForEvent('download'); await download.saveAs('follow-graph.png'); await browser.close(); })();
A quick heads-up: Selectors (like the export button's data-testid) might change between Neo4j Browser versions, so use your browser's dev tools to verify them for your setup. If your previous attempts failed, it's likely because you tried to fetch PNGs directly via API—this headless browser approach is the correct way to automate that workflow.
内容的提问来源于stack exchange,提问作者zetta

