Skip to content

Commit b814d3a

Browse files
committed
add menu data preview from csv file
1 parent f900b40 commit b814d3a

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

data/goals_shot_on_target.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pos,club,played,goals,shot_on
1+
Pos,Club,Played,Goals,Shot On Target
22
1,Liverpool,19,47,131
33
2,Arsenal,20,39,102
44
3,Nottingham Forest,20,29,93

data/standings.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pos,club,played,won,draw,lost,points
1+
Pos,Club,Played,Won,Draw,Lost,Points
22
1,Liverpool,19,14,7,1,46
33
2,Arsenal,20,11,7,2,44
44
3,Nottingham Forest,20,12,4,4,40

src/main.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { fileURLToPath } from 'url'
44
import { exit } from 'process'
55
import chalk from 'chalk'
66
import inquirer from 'inquirer'
7+
import csv from 'csv-parser'
8+
import Table from 'cli-table3'
79
import readlineSync from 'readline-sync'
810

911

@@ -20,7 +22,7 @@ const print = console.log
2022
*/
2123

2224

23-
const dataDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'data')
25+
const DATA_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'data')
2426

2527
const showGuide = () => {
2628
print('This is a guide for this program!');
@@ -29,36 +31,66 @@ const showGuide = () => {
2931
}
3032

3133

32-
if (!readlineSync.keyInYNStrict(chalk.green('Back?'))) {
34+
if (!readlineSync.keyInYNStrict(chalk.green('\nBack'))) {
3335
exit(0)
3436
}
3537
}
3638

37-
const runDemo = () => {
39+
const runDemo = async () => {
3840
let files
3941
try {
4042
print('Scanning data input file ...')
41-
files = fs.readdirSync(dataDir)
42-
43-
print('Choose file to do simple demo linear regression: ')
44-
files.forEach(file => {
45-
print(chalk.blue('- ', file))
46-
})
43+
files = fs.readdirSync(DATA_DIR)
4744
} catch (error) {
4845
print(chalk.red('Error reading directory: ', error.message))
4946
return;
5047
}
5148

52-
if (readlineSync.keyInYNStrict(chalk.green('Preview data?'))) {
53-
previewData('data passed')
49+
50+
const answers = await inquirer.prompt([
51+
{
52+
type: 'list',
53+
name: 'data',
54+
message: 'Choose data',
55+
choices: files
56+
}
57+
])
58+
59+
if (readlineSync.keyInYNStrict(chalk.green('\nPreview data'))) {
60+
await previewData(answers.data)
5461
}
55-
5662
}
5763

58-
const previewData = (data) => {
59-
print('Preview data: ', data)
64+
const readFileAsync = (filePath) => {
65+
return new Promise((resolve, reject) => {
66+
const data = []
67+
fs.createReadStream(filePath)
68+
.pipe(csv())
69+
.on('data', (row) => data.push(row))
70+
.on('end', () => {
71+
const table = new Table({
72+
head: Object.keys(data[0])
73+
})
74+
75+
data.forEach(row => {
76+
table.push(Object.values(row))
77+
})
78+
79+
console.log(table.toString())
80+
resolve()
81+
})
82+
.on('error', (error) => reject(error))
83+
})
84+
}
85+
86+
const previewData = async (filePath) => {
87+
print('\n-----------------------------------------------------\n')
88+
print('Preview data: ', filePath)
89+
90+
await Promise.all([readFileAsync(path.join(DATA_DIR, filePath))])
91+
6092

61-
if (readlineSync.keyInYNStrict(chalk.green('Continue next process?'))) {
93+
if (readlineSync.keyInYNStrict(chalk.green('\nContinue next process'))) {
6294
dataIdentification()
6395
}
6496
}
@@ -67,11 +99,12 @@ const previewData = (data) => {
6799

68100

69101
const dataIdentification = () => {
102+
print('\n-----------------------------------------------------\n')
70103
print('Data identification!')
71104
const feature = readlineSync.question('Choose feature: ')
72105
const label = readlineSync.question('Choose label: ')
73106

74-
if (readlineSync.keyInYNStrict(chalk.green('Continue process data?'))) {
107+
if (readlineSync.keyInYNStrict(chalk.green('\nContinue process data'))) {
75108
processing()
76109
}
77110
}
@@ -80,12 +113,13 @@ const dataIdentification = () => {
80113

81114

82115
const processing = () => {
116+
print('\n-----------------------------------------------------\n')
83117
print('Model processing!')
84118
for (let i = 1; i <= 20; i++) {
85119
print('iteration: ', i)
86120
}
87121

88-
if (readlineSync.keyInYNStrict(chalk.green('Show visualization: '))) {
122+
if (readlineSync.keyInYNStrict(chalk.green('\nShow visualization'))) {
89123
visualization()
90124
}
91125
}
@@ -95,6 +129,7 @@ const processing = () => {
95129

96130

97131
const visualization = () => {
132+
print('\n-----------------------------------------------------\n')
98133
print('Visualization here!')
99134
print('Graph, chart, or whatever...')
100135

@@ -122,7 +157,7 @@ const visualization = () => {
122157
//// MYMMMM9 _MM_ _dM_ _dMM__MM_ \M\_ _MM_ /////
123158
/////////////////////////////////////////////////////////////////////
124159

125-
async function main() {
160+
const main = async () => {
126161
do {
127162
console.clear()
128163
print(`
@@ -152,7 +187,7 @@ SIMPLE LINEAR REGRESSION DEMO
152187
showGuide()
153188
break;
154189
case '2. Run Demo':
155-
runDemo()
190+
await runDemo()
156191
break;
157192
case '3. Exit':
158193
print('Thanks, bye!')

0 commit comments

Comments
 (0)