Browse Source

example: migrate `python-xhr` to ESM (#4010)

Antoine du Hamel 2 years ago
parent
commit
66f49db856

+ 1 - 0
.eslintrc.js

@@ -198,6 +198,7 @@ module.exports = {
         'examples/multiple-instances/*.js',
         'examples/multiple-instances/*.js',
         'examples/node-xhr/*.js',
         'examples/node-xhr/*.js',
         'examples/php-xhr/*.js',
         'examples/php-xhr/*.js',
+        'examples/python-xhr/*.js',
         'examples/transloadit-markdown-bin/*.js',
         'examples/transloadit-markdown-bin/*.js',
         'examples/xhr-bundle/*.js',
         'examples/xhr-bundle/*.js',
         'private/dev/*.js',
         'private/dev/*.js',

+ 1 - 2
examples/python-xhr/.gitignore

@@ -1,2 +1 @@
-uppy.min.css
-uploads
+uploads/

+ 7 - 8
examples/python-xhr/readme.md → examples/python-xhr/README.md

@@ -6,22 +6,21 @@ This example uses a Python Flask server and `@uppy/xhr-upload` to upload files t
 
 
 To run this example, make sure you've correctly installed the **repository root**:
 To run this example, make sure you've correctly installed the **repository root**:
 
 
-```bash
-npm install
-npm run build
+```sh
+corepack yarn install
+corepack yarn build
 ```
 ```
 
 
 That will also install the npm dependencies for this example.
 That will also install the npm dependencies for this example.
 
 
 Additionally, this example uses python dependencies. Move into this directory, and install them using pip:
 Additionally, this example uses python dependencies. Move into this directory, and install them using pip:
 
 
-```bash
-cd ./examples/python-xhr
-pip install -r requirements.txt
+```sh
+corepack yarn workspace @uppy-example/python-xhr installPythonDeps
 ```
 ```
 
 
 Then, again in the **repository root**, start this example by doing:
 Then, again in the **repository root**, start this example by doing:
 
 
-```bash
-npm run example python-xhr
+```sh
+corepack yarn workspace @uppy-example/python-xhr start
 ```
 ```

+ 2 - 2
examples/python-xhr/index.html

@@ -4,9 +4,9 @@
     <meta charset="utf-8">
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title>Python + Uppy Example</title>
     <title>Python + Uppy Example</title>
-    <link href="uppy.min.css" rel="stylesheet">
   </head>
   </head>
   <body>
   <body>
-    <script src="bundle.js"></script>
+    <noscript>This app requires JavaScript.</noscript>
+    <script src="./main.js" type="module"></script>
   </body>
   </body>
 </html>
 </html>

+ 8 - 4
examples/python-xhr/main.js

@@ -1,7 +1,11 @@
-const Uppy = require('@uppy/core')
-const Webcam = require('@uppy/webcam')
-const Dashboard = require('@uppy/dashboard')
-const XHRUpload = require('@uppy/xhr-upload')
+import Uppy from '@uppy/core'
+import Webcam from '@uppy/webcam'
+import Dashboard from '@uppy/dashboard'
+import XHRUpload from '@uppy/xhr-upload'
+
+import '@uppy/core/dist/style.css'
+import '@uppy/webcam/dist/style.css'
+import '@uppy/dashboard/dist/style.css'
 
 
 const uppy = new Uppy({
 const uppy = new Uppy({
   debug: true,
   debug: true,

+ 10 - 12
examples/python-xhr/package.json

@@ -1,24 +1,22 @@
 {
 {
   "name": "@uppy-example/python-xhr",
   "name": "@uppy-example/python-xhr",
   "version": "0.0.0",
   "version": "0.0.0",
+  "type": "module",
   "dependencies": {
   "dependencies": {
-    "@babel/core": "^7.4.4",
     "@uppy/core": "workspace:*",
     "@uppy/core": "workspace:*",
     "@uppy/dashboard": "workspace:*",
     "@uppy/dashboard": "workspace:*",
     "@uppy/webcam": "workspace:*",
     "@uppy/webcam": "workspace:*",
-    "@uppy/xhr-upload": "workspace:*",
-    "babelify": "^10.0.0",
-    "budo": "^11.3.2",
-    "cookie-parser": "^1.4.6",
-    "cors": "^2.8.4",
-    "formidable": "^1.2.1",
-    "npm-run-all": "^4.1.3"
+    "@uppy/xhr-upload": "workspace:*"
+  },
+  "devDependencies": {
+    "npm-run-all": "^4.1.3",
+    "vite": "^3.0.0"
   },
   },
   "private": true,
   "private": true,
   "scripts": {
   "scripts": {
-    "copy": "cp ../../packages/uppy/dist/uppy.min.css .",
-    "start": "npm-run-all --serial copy --parallel start:*",
-    "start:client": "budo main.js:bundle.js -- -t babelify",
-    "start:server": "mkdir -p uploads && python server.py"
+    "installPythonDeps": "python3 -m pip install -r requirements.txt",
+    "start": "npm-run-all --parallel start:server start:client",
+    "start:client": "vite",
+    "start:server": "mkdir -p uploads && python3 server.py"
   }
   }
 }
 }

+ 3 - 2
examples/python-xhr/server.py

@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
 import os
 import os
 from flask import Flask, request, jsonify
 from flask import Flask, request, jsonify
 from werkzeug.utils import secure_filename
 from werkzeug.utils import secure_filename
@@ -7,7 +8,7 @@ UPLOAD_FOLDER = 'uploads'
 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
 
 
 app = Flask(__name__)
 app = Flask(__name__)
-app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
+app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__), UPLOAD_FOLDER)
 CORS(app)
 CORS(app)
 
 
 def allowed_file(filename):
 def allowed_file(filename):
@@ -18,7 +19,7 @@ def allowed_file(filename):
 def upload_file():
 def upload_file():
     if request.method == 'POST':
     if request.method == 'POST':
         # check if the post request has the file part
         # check if the post request has the file part
-        print request.files
+        print (request.files)
         if len(request.files) == 0:
         if len(request.files) == 0:
           return jsonify(
           return jsonify(
               error="No file n request"
               error="No file n request"

+ 2 - 14
yarn.lock

@@ -8175,17 +8175,12 @@ __metadata:
   version: 0.0.0-use.local
   version: 0.0.0-use.local
   resolution: "@uppy-example/python-xhr@workspace:examples/python-xhr"
   resolution: "@uppy-example/python-xhr@workspace:examples/python-xhr"
   dependencies:
   dependencies:
-    "@babel/core": ^7.4.4
     "@uppy/core": "workspace:*"
     "@uppy/core": "workspace:*"
     "@uppy/dashboard": "workspace:*"
     "@uppy/dashboard": "workspace:*"
     "@uppy/webcam": "workspace:*"
     "@uppy/webcam": "workspace:*"
     "@uppy/xhr-upload": "workspace:*"
     "@uppy/xhr-upload": "workspace:*"
-    babelify: ^10.0.0
-    budo: ^11.3.2
-    cookie-parser: ^1.4.6
-    cors: ^2.8.4
-    formidable: ^1.2.1
     npm-run-all: ^4.1.3
     npm-run-all: ^4.1.3
+    vite: ^3.0.0
   languageName: unknown
   languageName: unknown
   linkType: soft
   linkType: soft
 
 
@@ -13695,7 +13690,7 @@ __metadata:
   languageName: node
   languageName: node
   linkType: hard
   linkType: hard
 
 
-"cors@npm:^2.8.4, cors@npm:^2.8.5, cors@npm:latest, cors@npm:~2.8.5":
+"cors@npm:^2.8.5, cors@npm:latest, cors@npm:~2.8.5":
   version: 2.8.5
   version: 2.8.5
   resolution: "cors@npm:2.8.5"
   resolution: "cors@npm:2.8.5"
   dependencies:
   dependencies:
@@ -18479,13 +18474,6 @@ __metadata:
   languageName: node
   languageName: node
   linkType: hard
   linkType: hard
 
 
-"formidable@npm:^1.2.1":
-  version: 1.2.6
-  resolution: "formidable@npm:1.2.6"
-  checksum: 2b68ed07ba88302b9c63f8eda94f19a460cef6017bfda48348f09f41d2a36660c9353137991618e0e4c3db115b41e4b8f6fa63bc973b7a7c91dec66acdd02a56
-  languageName: node
-  linkType: hard
-
 "formidable@npm:^2.0.1":
 "formidable@npm:^2.0.1":
   version: 2.0.1
   version: 2.0.1
   resolution: "formidable@npm:2.0.1"
   resolution: "formidable@npm:2.0.1"