1
+ // dotnet fsi run_flask_app flask_sample.fsx [--global](to uses pip-run and venv by default)
2
+ #r " nuget: Fli"
3
+ #r " nuget: EluciusFTW.SpectreCoff"
4
+ //we can use this package to run python commands together with the script
5
+ open Fli
6
+ open SpectreCoff
7
+
8
+ " Python + F#" |> figlet |> toConsole
9
+
10
+ // using vscode F# highlight ext can also execute inline python
11
+ let python ( p : string )= p
12
+
13
+ let create_py_venv () =
14
+ cli {
15
+ Shell Shells.BASH
16
+ Command ( " python3 -m venv .venv" )
17
+ }
18
+ |> Command.execute
19
+ |> Output.printText
20
+
21
+ let install_pip_run_package () =
22
+ " install pip-run" |> C |> toConsole
23
+ cli {
24
+ Shell Shells.BASH
25
+ Command ( " pipx install pip-run" )
26
+ }
27
+ |> Command.execute
28
+ |> Output.printText
29
+
30
+
31
+ let isGlobal =
32
+ match fsi.CommandLineArgs |> Seq.toList with
33
+ |_::_:: " --global" ::[] -> true
34
+ |_:: " --global" ::_ -> true
35
+ |_ -> false
36
+
37
+ let install_pipreqs_package () =
38
+ " install pipreqs" |> C |> toConsole
39
+ cli {
40
+ Shell Shells.BASH
41
+ Command ( " pipx install pipreqs" )
42
+ }
43
+ |> Command.execute
44
+ |> Output.printText
45
+
46
+ // FAILS...
47
+ let gen_pip_requirements_file () =
48
+ " execute pipreqs to generate requirements.txt" |> C |> toConsole
49
+ cli {
50
+ Shell Shells.BASH
51
+ Command ( " python3 -m pipreqs ." )
52
+ }
53
+ |> Command.execute
54
+ |> Output.throwIfErrored
55
+ |> Output.printText
56
+
57
+ let create_requirements () =
58
+ cli {
59
+ Shell Shells.BASH
60
+ Command ( " touch requirements.txt" )
61
+ }
62
+ |> Command.execute
63
+ |> Output.printText
64
+
65
+ let gen_pip_requirements_file_local () =
66
+ " execute local pipreqs to generate requirements.txt" |> C |> toConsole
67
+ cli {
68
+ Shell Shells.BASH
69
+ Command ( " pipreqs ." )
70
+ }
71
+ |> Command.execute
72
+ |> Output.throwIfErrored
73
+ |> Output.printText
74
+
75
+ let install_pip_requirements () =
76
+ " pip install requirements.txt" |> C |> toConsole
77
+ cli {
78
+ Shell Shells.BASH
79
+ Command ( " python3 -m pip install -r requirements.txt " )
80
+ }
81
+ |> Command.execute
82
+ |> Output.throwIfErrored
83
+ |> Output.printText
84
+
85
+ let activate_py_venv () =
86
+ cli {
87
+ Shell Shells.BASH
88
+ Command ( " source .venv/bin/activate" )
89
+ }
90
+ |> Command.execute
91
+ |> Output.throwIfErrored
92
+ |> Output.printText
93
+
94
+ let which_python () =
95
+ cli {
96
+ Shell Shells.BASH
97
+ Command ( " which python" )
98
+ }
99
+ |> Command.execute
100
+ |> Output.printText
101
+
102
+
103
+ /// pip install flask
104
+ let pip_install_flask () =
105
+ cli {
106
+ Shell Shells.BASH
107
+ Command ( " python3 -m pip install flask" )
108
+ }
109
+ |> Command.execute
110
+ |> Output.printText
111
+
112
+ /// pip install extra dependencies if needed, specify them as extra arg ',' separated
113
+ let pip_install_extras () =
114
+ let dependencies =
115
+ match fsi.CommandLineArgs |> Seq.toList with
116
+ |_::_:: trd::[] -> trd.Split( " ," )
117
+ |_ -> [||]
118
+
119
+ for dep in dependencies do
120
+ cli {
121
+ Shell Shells.BASH
122
+ Command ( $" python3 -m pip install {dep}" )
123
+ }
124
+ |> Command.execute
125
+ |> Output.printText
126
+
127
+ let fable_compile_flask_app () =
128
+ " FABLE" |> figlet |> toConsole
129
+
130
+ let flaskScriptName =
131
+ printfn $" args: {fsi.CommandLineArgs}"
132
+ match fsi.CommandLineArgs |> Seq.toList with
133
+ |_:: snd::[] -> snd
134
+ |_ -> " app.fsx"
135
+
136
+ cli {
137
+ Shell Shells.BASH
138
+ Command ( $" dotnet tool restore && dotnet fable {flaskScriptName} --lang Python --noCache" )
139
+ }
140
+ |> Command.execute
141
+ |> Output.throwIfErrored
142
+ |> Output.printText
143
+
144
+ let remove_old_app () =
145
+ " rm app.py" |> C |> toConsole
146
+ cli {
147
+ Shell Shells.BASH
148
+ Command " rm app.py"
149
+ }
150
+ |> Command.execute
151
+ |> Output.throwIfErrored
152
+ |> Output.printText
153
+
154
+ let rename_and_cleanup () =
155
+ " rename latest script to app.py" |> C |> toConsole
156
+ cli {
157
+ Shell Shells.BASH
158
+ Command " mv *.py app.py"
159
+ }
160
+ |> Command.execute
161
+ |> Output.throwIfErrored
162
+ |> Output.printText
163
+
164
+ let deactivate_py_venv () =
165
+ cli {
166
+ Shell Shells.BASH
167
+ Command ( " python3 -m deactivate" )
168
+ }
169
+ |> Command.execute
170
+ |> Output.printText
171
+
172
+ let run_flask_app () =
173
+ try
174
+ " starting app to listen on http://127.0.0.1:5000" |> P |> toConsole
175
+ cli {
176
+ Shell Shells.BASH
177
+ Command ( " python3 -m flask run" )
178
+ }
179
+ |> Command.execute
180
+ |> Output.throwIfErrored
181
+ |> ignore
182
+ with ex ->
183
+ printfn $" script exited with code {ex.Message}"
184
+ ()
185
+
186
+ let one_shot_run_flask_app () =
187
+ try
188
+ " starting ONE-SHOT app to listen on http://127.0.0.1:5000" |> P |> toConsole
189
+ cli {
190
+ Shell Shells.BASH
191
+ Command ( " pip-run flask -r requirements.txt -- -m flask run" )
192
+ }
193
+ |> Command.execute
194
+ |> Output.throwIfErrored
195
+ |> ignore
196
+ with ex ->
197
+ $" script exited with code {ex.Message}" |> P |> toConsole
198
+ ()
199
+
200
+
201
+ let isVenv =
202
+ match fsi.CommandLineArgs |> Seq.toList with
203
+ |_::_:: " --venv" ::[] -> true
204
+ |_:: " --venv" ::_ -> true
205
+ |_ -> false
206
+
207
+ // execution
208
+
209
+ if isVenv then
210
+ create_ py_ venv()
211
+ |> activate_ py_ venv
212
+ |> which_ python
213
+ printfn " remember to run deactivate at the end of the script, or which python"
214
+
215
+ //install_pipreqs_package()
216
+ //|> if isGlobal then gen_pip_requirements_file else gen_pip_requirements_file_local
217
+ ()
218
+ |> create_ requirements
219
+ |> if isGlobal then id else install_ pip_ run_ package
220
+ |> fable_ compile_ flask_ app
221
+ |> remove_ old_ app
222
+ |> rename_ and_ cleanup
223
+ |> if isGlobal then run_ flask_ app else one_ shot_ run_ flask_ app
224
+
225
+ // need a strategy to not break on CTRL-C for this...
226
+ // maybe use `fg` ?
227
+ // |> deactivate_py_venv
228
+ // |> which_python
0 commit comments