Explore all Validation open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Validation

validator.js

13.6.0

yup

Fix object.partial(), `required` changes

jquery-validation

1.19.3 / 2021-01-09

validator

Release 10.10.1

pydantic

v1.9.0 (2021-12-31)

Popular Libraries in Validation

validator.js

by validatorjs doticonjavascriptdoticon

star image 18549 doticonMIT

String validation

joi

by sideway doticonjavascriptdoticon

star image 18174 doticonNOASSERTION

The most powerful data validation library for JS

yup

by jquense doticontypescriptdoticon

star image 16677 doticonMIT

Dead simple Object schema validation

jquery-validation

by jquery-validation doticonjavascriptdoticon

star image 10075 doticonMIT

jQuery Validation Plugin library sources

validator

by go-playground doticongodoticon

star image 9801 doticonMIT

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

pydantic

by samuelcolvin doticonpythondoticon

star image 9557 doticonMIT

Data parsing and validation using Python type hints

Parsley.js

by guillaumepotier doticonjavascriptdoticon

star image 9019 doticonMIT

Validate your forms, frontend, without writing a single line of javascript

vee-validate

by logaretm doticontypescriptdoticon

star image 8995 doticonMIT

✅ Form Validation for Vue.js

class-validator

by typestack doticontypescriptdoticon

star image 7578 doticonMIT

Decorator-based property validation for classes.

Trending New libraries in Validation

zod

by colinhacks doticontypescriptdoticon

star image 6987 doticonMIT

TypeScript-first schema validation with static type inference

deep-email-validator

by mfbx9da4 doticontypescriptdoticon

star image 581 doticonMIT

Validates regex, typos, disposable, dns and smtp

resolvers

by react-hook-form doticontypescriptdoticon

star image 552 doticonMIT

📋 Validation resolvers: Zod, Yup, Joi, Superstruct, Vest, class-validator, io-ts, typanion, and nope.

kubeconform

by yannh doticongodoticon

star image 466 doticonNOASSERTION

A FAST Kubernetes manifests validator, with support for Custom Resources!

typecheck.macro

by vedantroy doticontypescriptdoticon

star image 369 doticonMIT

A easy (and fast) typescript validation library/macro. Automatically generate validation functions for Typescript types.

bueno

by philipnilsson doticontypescriptdoticon

star image 357 doticon

Composable validators for forms, API:s in TypeScript

computed-types

by neuledge doticontypescriptdoticon

star image 292 doticonMIT

🦩 Joi like validations for TypeScript

react-native-iaphub

by iaphub doticonjavascriptdoticon

star image 274 doticonMIT

The easiest way to implement IAP (In-app purchase) in your React Native app.

iodine

by mattkingshott doticonjavascriptdoticon

star image 238 doticonMIT

A micro JavaScript validation library.

Top Authors in Validation

1

validate-io

23 Libraries

star icon63

2

personnummer

8 Libraries

star icon101

3

laravel-validation-rules

6 Libraries

star icon198

4

data-cleaning

5 Libraries

star icon378

5

microsoft

5 Libraries

star icon120

6

hugomd

4 Libraries

star icon27

7

miguelmota

4 Libraries

star icon54

8

fnando

4 Libraries

star icon524

9

Lemoncode

4 Libraries

star icon198

10

spatie

4 Libraries

star icon1338

1

23 Libraries

star icon63

2

8 Libraries

star icon101

4

5 Libraries

star icon378

5

5 Libraries

star icon120

6

4 Libraries

star icon27

7

4 Libraries

star icon54

8

4 Libraries

star icon524

9

4 Libraries

star icon198

10

4 Libraries

star icon1338

Trending Kits in Validation

JavaScript validation libraries play a vital role in web development. They ensure the accuracy and integrity of user-submitted data. These libraries provide functions to validate form elements and implement client-side validation. They support both native HTML5 form validation elements and custom rules. This enables developers to create robust, user-friendly forms. Let's explore the key aspects and benefits of using JavaScript validation libraries:  

Validation Types and Elements:  

JavaScript validation libraries cover diverse needs, validating text fields, number inputs, and more. They handle email formats, postal codes, URLs, and complex field matching. These libraries ensure data conformity across browsers and platforms using built-in validation methods.   

Error Handling and Customization:  

Effective validation libraries provide immediate feedback to users. This is done by displaying error messages when form inputs are invalid. They provide customizable error messages that match the application's language and branding. Custom error messages can be set for specific validation constraints. Thus, it improves the user experience and guides correct input.  

Validation Rules and Constraints:  

JavaScript validation libraries support various validation rules out of the box. This includes required fields, length constraints, and common validations like validating email addresses. These libraries offer flexible methods to define custom validation rules meeting project requirements. Developers can define validation constraints such as allowed values and minimum & maximum lengths.  

Integration and Event Handling:  

Validation libraries integrate with HTML forms. They handle form submissions and submitting events. They provide methods to access and validate form values, individual fields, or inputs. Event handlers, such as change events, enable real-time validation and error messaging. It empowers users to correct input errors.  

Lightweight and Cross-Framework Support:  

Many JavaScript validation libraries are lightweight and dependency-free. It makes them suitable for various frameworks and projects. They can be integrated into existing forms, irrespective of the underlying technology stack. Some libraries, like the jQuery Validation Plugin, provide cross-framework compatibility for consistent validation.  

 

By leveraging these validation libraries, developers can ensure that:  

  • **data entered into forms is accurate,  
  • conforms to specified rules, and  
  • aligns with the desired format.  


This improves user experience & minimizes the chances of processing wrong or inappropriate information. JavaScript validation libraries play a vital role in the following:  

  • **enhancing data quality,  
  • reducing server-side validation overhead, and  
  • providing immediate feedback to users during form interactions.  


In conclusion, JavaScript validation libraries enhance form validation and maintain data integrity. By choosing the right library and utilizing its features, developers can:  

  • **streamline the validation process,  
  • improve user satisfaction, and  
  • optimize the performance of web applications.

validator.js  

  • It helps validate form inputs, ensuring they meet specific criteria. 
  • These include required fields, length constraints, and email format.  
  • It supports custom validation rules.  
  • This will help define complex validation logic tailored to your application's requirements. 

yup  

  • It helps validate data schema, especially for forms, APIs, or data transformation.   
  • It supports declarative validation rules with a simple and intuitive syntax. 

joi  

  • It helps validate and sanitize data input in Node.js applications.  
  • This includes request payload, query parameters, and route parameters.  
  • It supports various validation rules and powerful schema validation capabilities. It ensures the integrity and safety of incoming data. 

jquery-validation  

  • It helps validate form inputs in jQuery-based applications. 
  • It provides a comprehensive set of validation options.  
  • It supports various validation rules, error messaging, and form submission handling.  

Parsley.js  

  • It helps validate form inputs in HTML.  
  • This is a simple and flexible way to define validation rules and error messaging.  
  • It supports real-time validation for instant feedback to users as they fill out the form.  

express-validator  

  • It helps validate and sanitize user input in Express.js applications.  
  • It ensures the integrity and security of incoming data.  
  • It supports middleware functions to validate request inputs.  
  • It makes integrating with existing Express routing easy. 

vuelidate 

  • Validates form inputs in Vue.js applications.  
  • Thus, offering a simple and expressive way to define validation rules.  
  • It supports declarative validation rules and provides real-time error feedback to users. 

react-form-v1 

  • Handle form validation in React applications using custom React hooks for performant solutions.  
  • Supports validation rules, error messaging, and form submission handling while minimizing unnecessary re-renders.  

FAQ  

1. What different validation elements are available in a javascript validation library?  

JavaScript validation libraries provide a range of validation elements to validate form inputs. These include text, numbers, select boxes, checkboxes, radio buttons, and date pickers. By targeting specific elements, developers can apply validation rules and constraints. It ensures the correctness of user-submitted data.  

 

2. What is the jQuery Validation Plugin, and how can it be used to validate forms?  

The jQuery Validation Plugin is a used JavaScript library. It simplifies form validation in web development. It integrates with jQuery, providing methods and rules to validate form inputs. Developers can specify validation rules using HTML attributes or define them. The plugin handles form submission events and performs validation. It displays error messages for invalid inputs. It offers extensive customization options. It helps tailor the validation behavior according to specific project requirements.  

 

3. How is client-side validation achieved using javascript?  

Client-side validation in JavaScript is achieved. We can use the browser's native validation capabilities or leverage JavaScript libraries. Native HTML5, form validation elements, enable basic client-side validation. JavaScript libraries enhance client-side validation by providing extra features. It includes custom validation rules, error message customization, and real-time use event handlers.  

 

4. Can a custom validator be created using a javascript library?  

Yes, most JavaScript validation libraries allow developers to create custom validators. These libraries provide APIs or extension mechanisms to define custom validation functions. Developers can implement custom logic to validate form inputs based on unique requirements. It includes validating against external services, checking data patterns, or implementing validation rules. Custom validators empower developers to enforce specialized validation constraints. It is beyond the standard validation functions provided by the library.  

 

5. What kind of validation functions are supported by most javascript libraries?  

JavaScript libraries support a wide range of validation functions. Common validation functions include checking for empty or required fields and validating email addresses. It ensures numeric or alphabetic input, validating URLs or comparing values between fields. It helps pattern check using regular expressions and validating data against predefined lists. Libraries create custom validation functions tailored to specific project needs.  

 

6. How do you display error messages for invalid form fields with a js library?  

JavaScript libraries offer mechanisms to display error messages for invalid form fields. When a form is submitted, or an input field loses focus, the library performs validation. It helps checks the validity of each field. An invalid field can display an error message near the field or in a designated error container. The error message provides specific feedback to the user on the validation error. JavaScript libraries provide methods to handle error message rendering. It offers flexibility in styling and positioning the error messages. It ensures optimal user experience. 

Tkinter validation refers to a mechanism provided by the Tkinter library in Python. This validates user input in GUI (Graphical User Interface) applications. It allows developers to enforce certain constraints or rules on the data. The data that users enter into entry fields or other input widgets. The validation process ensures that the input data meets specific criteria. That is done before it is accepted and processed further.   

 

The primary purpose of tkinter validation is to enhance the user experience. Done by preventing invalid or incorrect data from being entered into the application. By defining validation rules, developers can ensure that only valid data is accepted. This helps reduce the chances of errors or inconsistencies in the application's functionality. Tkinter validation can be applied to various types of user input. User inputs are text, numbers, dates, or any other data format. That is used for an application's requirements. Some common scenarios where validation is useful include:   

  • Data format validation   
  • Range or boundary validation   
  • Data type validation   
  • Custom validation   

 

Tkinter is a popular Python library for creating graphical user interfaces (GUIs). It provides a range of widgets and functionality for creating interactive applications. It does not offer specific validation mechanisms for input fields or protection. This won't be done against complex security vulnerabilities like cross-site scripting (XSS). But you can install various types of validation in Tkinter applications. It can be done using programming techniques. Let's discuss some common approaches:   

Simple Input Validation:   

  • Data Type Validation: You can confirm the type of input entered in a widget. It can be done by ensuring that a numerical field is found. It only accepts numbers or a text field containing no digits.   
  • Range or Length Validation: Confirm if the input falls within a specified range.   
  • Pattern Matching: Use regular expressions to check if the input matches a pattern. That is like email address or phone number validation.    

Field-Level Validation:   

  • Mandatory Field Validation: Ensure that required fields are not blank before proceeding.   
  • Format Validation: Confirm the format of input fields. It can be verifying that an input field is in a specific date or time format.    

Cross-Field Validation:   

  • Dependency Validation: Check if the values entered in one field depend on the other.   
  • Comparison Validation: Compare values between many fields. It can ensure that the value in one field is greater than in another.    

Business Rule Validation:   

  • Custom Validation Logic: Install custom validation logic based on specific business rules.   

Tkinter applications:   

  • Sanitization: Ensure that user-generated input is sanitized to prevent the execution of codes.   
  • Input Filtering: Install filtering techniques to strip or reject harmful input.   

Validation is a critical aspect of application development. That ensures the integrity and correctness of user input. It involves verifying that the data entered by users meets certain criteria. Validation can be implemented using various techniques. That may range from simple checks in the user interface to more within the codebase.   

Different ways of implementing validation:   

1. User Interface Validation:   

  • Mandatory Field Checks   
  • Length and Format Checks   
  • Range and Boundary Checks   
  • Dependency Checks   
  • Pattern Matching    

2. Client-Side Validation:   

  • JavaScript Validation   
  • Browser-Based Validation   

3. Server-Side Validation:   

  • Input Sanitization    
  • Data Type and Format Checks   
  • Business Rule Validation   
  • Database Constraints   

4. Codebase Validation:   

  • Model-Level Validation   
  • Custom Validation Logic   

5. Automated Testing:   

  • Unit Tests    
  • Integration Tests   
  • End-to-End Tests   

Implementing validation in an application is crucial to ensure data integrity. It is also crucial to maintain the quality of the system.   

Tips for implementing validation:   

  • Define clear validation rules - Identify the specific data requirements and constraints.   
  • Leverage built-in validation mechanisms - Most programming languages and frameworks provide validation mechanisms.   
  • Install custom validation rules - Besides validation, you need to define custom rules. That is tailored to your application's unique requirements.   
  • Confirm data on both client-side - Implementing validation on the client side. That provides a comprehensive validation approach.   
  • Server-side - Implementing validation at server-side. That provides a comprehensive validation approach.   
  • Install validation as early as possible - Confirm user input as early as possible.   
  • Provide clear error messages - When validation fails, provide error messages. That is used to guide users in correcting their input.   
  • Consider using external validation services - For complex validation, consider leveraging external services.   
  • Test and iterate - Testing is crucial to ensure your validation rules work as intended.   
  • Stay consistent and maintain validation rules - As your application evolves. It will ensure that validation rules are applied throughout the system.   

 

In conclusion, validation is of paramount importance in any application. It plays a major role in ensuring the data's accuracy and safety. By validating the input data, developers can install safeguards. Checks that help prevent errors, vulnerabilities, and potential risks. Validation serves as a vital safeguard in application development. That ensures that the data being processed is accurate and safe. By implementing validation techniques, developers can prevent data corruption. It improves security and enhances the user experience. Also, it maintains data integrity. Investing time and effort into robust validation practices contributes to reliability. It benefits both users and organizations alike.    


Here is an example of how to perform validation on user input in Tkinter Python.



Fig: Previw of the Code.



Fig2: Preview of the Output when the code is run in IDE.

Code


In this solution, we are performing validation on user input using Tkinter.

Instructions

Follow the steps carefully to get the output easily.

  1. Install Jupyter Notebook on your computer.
  2. Open the terminal and install the required libraries with the following commands.
  3. Install tkinter - pip install tk
  4. Copy the snippet using the 'copy' button and paste it into that file
  5. Run the file using run button.


I hope you found this useful. I have added the link to dependent libraries, and version information in the following sections.


I found this code snippet by searching for "How to perform validation on user input in Tkinter Python" in kandi. You can try any such use case!

Dependent Libraries

You can also search for any dependent libraries on kandi like "tkinter"


Environment Tested


I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in Python3.9.6.
  2. The solution is tested on Tkinter 0.1.0 version.


Using this solution, we are able to perform validation on user input using Tkinter.


This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us to perform validation on user input using Tkinter.

Support


  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.

FAQ:   

1. What is Tkinter, and how does it work?   

Tkinter is a standard Python library for creating graphical user interfaces (GUIs). It provides tools and widgets that allow developers to build desktop apps. This is done with buttons, menus, text boxes, and other interactive components. The basic workflow of using Tkinter involves the following steps:   

  • Import the tkinter module   
  • Create a root window   
  • Design the GUI   
  • Organize the widgets   
  • Handle events   
  • Run the main event loop   

 

2. How can I ensure that widget content is valid before processing it?   

You can install validation mechanisms to ensure widget content is valid before processing. That is used to check the content against predefined criteria or rules. Here are some steps you can follow to confirm widget content:   

  • Define validation rules   
  • Use client-side validation   
  • Install server-side validation   
  • Confirm data types   
  • Confirm required fields   
  • Check length and character limits   
  • Use regular expressions or patterns   
  • Sanitize input   
  • Handle validation errors   
  • Test   

 

3. Is the text entry widget the best option for input validation in Tkinter?    

The Text entry widget in Tkinter is designed for multiline text input. Other widgets are better suited for this. It helps perform input validation using a text widget. If you need to confirm user input in a single line, the Entry widget is a more appropriate choice. It allows you to restrict the input to a specific format or set the greatest length for the input.   

 

The best option for input validation depends on the requirements of your app. The Text widget can be used for validation if needed.   

 

4. Are other libraries available to assist with input validation on Tkinter?    

In tkinter, there are several libraries available that can assist with input validation. Here are a few popular ones:   

  • tkinter-validation: Provides a set of validation functions   
  • Python validity offers a powerful way to perform input validation on Tkinter.   
  • Tkinter-validators: Provides a collection of validation functions designed for Tkinter entry widgets.   

 

5. How do I set widget options to confirm user inputs in a text entry field?    

To set widget options to confirm user inputs, you should define validation rules. The exact process depends on the platform or framework you use to develop your app. Here's a general approach:   

  • Identify the widget: Determine the text entry field widget you use in your app.   
  • Understand available options: Review the documentation or resources for the widget. It helps understand the available options for validation.   
  • Define validation rules: Determine the specific validation rules to apply to the field.   
  • Set widget options: Use the provided options to configure the validation rules.   
  • Install error handling: Determine how you want to handle validation errors. This should be done when the user input does not meet the specified rules.   
  • Test and iterate: Test your implementation by entering different inputs and validating.  

Trending Discussions on Validation

How to set up a custom AJV keyword?

Google OAuth 2.0 failing with Error 400: invalid_request for some client_id, but works well for others in the same project

The unauthenticated git protocol on port 9418 is no longer supported

FastAPI - GET request results in typeerror (value is not a valid dict)

Vue 3 and Vuetify 3 Alpha: ValidationError: progress plugin invalid options

FastAPI - Pydantic - Value Error Raises Internal Server Error

How to check two argument in java is either one of them null or both

Is there anyway to clear a textarea without using an onclick function?

logistic regression and GridSearchCV using python sklearn

Single File ASP.NET Core 5 web app does not load static resources

QUESTION

How to set up a custom AJV keyword?

Asked 2022-Apr-01 at 15:42

I'm using ajv and want to add a custom validator inspecting a given object. This custom validator should either return true or fail with the correct error message. Based on

I started with the following sample code

1const Ajv = require("ajv");
2const ajv = new Ajv({ allErrors: true });
3
4ajv.addKeyword({
5  keyword: "eachPropIsTrue",
6  type: "object",
7  schemaType: "boolean",
8  code(context) {
9    const { data } = context;
10    const eachPropIsTrue = Object.values(data).every(value => !!value);
11
12    context.fail(!eachPropIsTrue);
13  },
14})
15
16const schema = {
17  type: "object",
18  eachPropIsTrue: true,
19  properties: {
20    a: { type: "boolean" }
21  },
22  required: [ "a" ],
23  additionalProperties: false
24};
25
26const validate = ajv.compile(schema);
27console.log(validate({ a: true })); // true
28console.log(validate({ a: false })); // true but should be false!
29console.log(validate({ })); // false
30

Unfortunately I'm facing two problems:

  • The second validation does not fail although I would expect it to fail
  • Where do I have to pass in information about the constructed error message?

When inspecting the data variable inside the code function I can see that it does not contain the current object. So what is the correct way to set up a custom keyword?

I'm looking for a sample function like

1const Ajv = require("ajv");
2const ajv = new Ajv({ allErrors: true });
3
4ajv.addKeyword({
5  keyword: "eachPropIsTrue",
6  type: "object",
7  schemaType: "boolean",
8  code(context) {
9    const { data } = context;
10    const eachPropIsTrue = Object.values(data).every(value => !!value);
11
12    context.fail(!eachPropIsTrue);
13  },
14})
15
16const schema = {
17  type: "object",
18  eachPropIsTrue: true,
19  properties: {
20    a: { type: "boolean" }
21  },
22  required: [ "a" ],
23  additionalProperties: false
24};
25
26const validate = ajv.compile(schema);
27console.log(validate({ a: true })); // true
28console.log(validate({ a: false })); // true but should be false!
29console.log(validate({ })); // false
30validate(theObject): boolean | string => {
31  const anyPropIsFalse = Object.values(data).some(value => value === false);
32
33  if(anyPropIsFalse) {
34    return 'Any prop is false!';
35  }
36
37  return true;
38};
39

or maybe a function returning void but using context.fail('Any prop is false!');


Sidenote:

My real world usecase scenario is an object with two properties where one is an object with an id and the other property is an array holding objects with ids. I have to make sure that all ids from both properties are completely unique.

ANSWER

Answered 2022-Apr-01 at 15:42

This makes the example to work as intended.

1const Ajv = require("ajv");
2const ajv = new Ajv({ allErrors: true });
3
4ajv.addKeyword({
5  keyword: "eachPropIsTrue",
6  type: "object",
7  schemaType: "boolean",
8  code(context) {
9    const { data } = context;
10    const eachPropIsTrue = Object.values(data).every(value => !!value);
11
12    context.fail(!eachPropIsTrue);
13  },
14})
15
16const schema = {
17  type: "object",
18  eachPropIsTrue: true,
19  properties: {
20    a: { type: "boolean" }
21  },
22  required: [ "a" ],
23  additionalProperties: false
24};
25
26const validate = ajv.compile(schema);
27console.log(validate({ a: true })); // true
28console.log(validate({ a: false })); // true but should be false!
29console.log(validate({ })); // false
30validate(theObject): boolean | string => {
31  const anyPropIsFalse = Object.values(data).some(value => value === false);
32
33  if(anyPropIsFalse) {
34    return 'Any prop is false!';
35  }
36
37  return true;
38};
39ajv.addKeyword({
40  keyword:    "eachPropIsTrue",
41  type:       "object",
42  schemaType: "boolean",
43  compile:    () => data => Object.values(data).every(value => !!value)
44});
45

Anyway I strongly suggest to better check ajv user-defined keywords documentation.

Source https://stackoverflow.com/questions/71673483

QUESTION

Google OAuth 2.0 failing with Error 400: invalid_request for some client_id, but works well for others in the same project

Asked 2022-Mar-30 at 14:21

We have some apps (or maybe we should call them a handful of scripts) that use Google APIs to facilitate some administrative tasks. Recently, after making another client_id in the same project, I started getting an error message similar to the one described in localhost redirect_uri does not work for Google Oauth2 (results in 400: invalid_request error). I.e.,

Error 400: invalid_request

You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure.

You can let the app developer know that this app doesn't comply with one or more Google validation rules.

Request details:

The content in this section has been provided by the app developer. This content has not been reviewed or verified by Google.

If you’re the app developer, make sure that these request details comply with Google policies.

redirect_uri: urn:ietf:wg:oauth:2.0:oob

How do I get through this error? It is important to note that:

  • The OAuth consent screen for this project is marked as "Internal". Therefore any mentions of Google review of the project, or publishing status are irrelevant
  • I do have "Trust internal, domain-owned apps" enabled for the domain
  • Another client id in the same project works and there are no obvious differences between the client IDs - they are both "Desktop" type which only gives me a Client ID and Client secret that are different
  • This is a command line script, so I use the "copy/paste" verification method as documented here hence the urn:ietf:wg:oauth:2.0:oob redirect URI (copy/paste is the only friendly way to run this on a headless machine which has no browser).
  • I was able to reproduce the same problem in a dev domain. I have three client ids. The oldest one is from January 2021, another one from December 2021, and one I created today - March 2022. Of those, only the December 2021 works and lets me choose which account to authenticate with before it either accepts it or rejects it with "Error 403: org_internal" (this is expected). The other two give me an "Error 400: invalid_request" and do not even let me choose the "internal" account. Here are the URLs generated by my app (I use the ruby google client APIs) and the only difference between them is the client_id - January 2021, December 2021, March 2022.

Here is the part of the code around the authorization flow, and the URLs for the different client IDs are what was produced on the $stderr.puts url line. It is pretty much the same thing as documented in the official example here (version as of this writing).

1
2OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
3
4def user_credentials_for(scope, user_id = 'default')
5    token_store = Google::Auth::Stores::FileTokenStore.new(:file => token_store_path)
6    authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
7    credentials = authorizer.get_credentials(user_id)
8    if credentials.nil?
9        url = authorizer.get_authorization_url(base_url: OOB_URI)
10        $stderr.puts ""
11        $stderr.puts "-----------------------------------------------"
12        $stderr.puts "Requesting authorization for '#{user_id}'"
13        $stderr.puts "Open the following URL in your browser and authorize the application."
14        $stderr.puts url
15        code = $stdin.readline.chomp
16        $stderr.puts "-----------------------------------------------"
17        credentials = authorizer.get_and_store_credentials_from_code(
18            user_id: user_id, code: code, base_url: OOB_URI)
19    end
20    credentials
21end
22                                                                                                                                          
23

ANSWER

Answered 2022-Mar-02 at 07:56

steps.oauth.v2.invalid_request 400 This error name is used for multiple different kinds of errors, typically for missing or incorrect parameters sent in the request. If is set to false, use fault variables (described below) to retrieve details about the error, such as the fault name and cause.

  • GenerateAccessToken GenerateAuthorizationCode
  • GenerateAccessTokenImplicitGrant
  • RefreshAccessToken

Google Oauth Policy

Source https://stackoverflow.com/questions/71318804

QUESTION

The unauthenticated git protocol on port 9418 is no longer supported

Asked 2022-Mar-27 at 13:23

I have been using github actions for quite sometime but today my deployments started failing. Below is the error from github action logs

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7

Upon investigation, it appears that below section in my yml file is causing the issue.

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9

I have looked into this change log but can't seem to comprehend the issue.

Additional Details: Server: EC2 Instance Github actions steps:

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9  steps:
10  - name: Checkout
11    uses: actions/checkout@v2
12
13  - id: vars
14    run: |
15      if [ '${{ github.ref }}' == 'refs/heads/master' ]; then echo "::set-output name=environment::prod_stackstream" ; echo "::set-output name=api-url::api" ; elif [ '${{ github.ref }}' == 'refs/heads/staging' ]; then echo "::set-output name=environment::staging_stackstream"  ; echo "::set-output name=api-url::stagingapi" ; else echo "::set-output name=environment::dev_stackstream" ; echo "::set-output name=api-url::devapi" ; fi
16
17  - uses: pCYSl5EDgo/cat@master
18    id: slack
19    with:
20      path: .github/workflows/slack.txt
21
22  - name: Slack Start Notification
23    uses: 8398a7/action-slack@v3
24    env:
25      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
26      ENVIRONMENT: '`${{ steps.vars.outputs.environment }}`'
27      COLOR: good
28      STATUS: '`Started`'
29    with:
30      status: custom
31      fields: workflow,job,commit,repo,ref,author,took
32      custom_payload: |
33        ${{ steps.slack.outputs.text }}
34
35  - name: Installing modules
36    env:
37      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
38    run: yarn install
39
40  - name: Create Frontend Build
41    env:
42      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
43    run: yarn build
44
45  - name: Deploy to Frontend Server DEV
46    if: ${{ contains(github.ref, 'dev') }}
47    uses: easingthemes/ssh-deploy@v2.1.5
48    env:
49      SSH_PRIVATE_KEY: ${{ secrets.DEV_KEY }}
50      ARGS: '-rltgoDzvO --delete'
51      SOURCE: 'deploy/'
52      REMOTE_HOST: ${{ secrets.DEV_HOST }}
53      REMOTE_USER: plyfolio-dev
54      TARGET: '/home/plyfolio-dev/${{ steps.vars.outputs.environment }}/fe/deploy'
55

package.json file

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9  steps:
10  - name: Checkout
11    uses: actions/checkout@v2
12
13  - id: vars
14    run: |
15      if [ '${{ github.ref }}' == 'refs/heads/master' ]; then echo "::set-output name=environment::prod_stackstream" ; echo "::set-output name=api-url::api" ; elif [ '${{ github.ref }}' == 'refs/heads/staging' ]; then echo "::set-output name=environment::staging_stackstream"  ; echo "::set-output name=api-url::stagingapi" ; else echo "::set-output name=environment::dev_stackstream" ; echo "::set-output name=api-url::devapi" ; fi
16
17  - uses: pCYSl5EDgo/cat@master
18    id: slack
19    with:
20      path: .github/workflows/slack.txt
21
22  - name: Slack Start Notification
23    uses: 8398a7/action-slack@v3
24    env:
25      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
26      ENVIRONMENT: '`${{ steps.vars.outputs.environment }}`'
27      COLOR: good
28      STATUS: '`Started`'
29    with:
30      status: custom
31      fields: workflow,job,commit,repo,ref,author,took
32      custom_payload: |
33        ${{ steps.slack.outputs.text }}
34
35  - name: Installing modules
36    env:
37      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
38    run: yarn install
39
40  - name: Create Frontend Build
41    env:
42      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
43    run: yarn build
44
45  - name: Deploy to Frontend Server DEV
46    if: ${{ contains(github.ref, 'dev') }}
47    uses: easingthemes/ssh-deploy@v2.1.5
48    env:
49      SSH_PRIVATE_KEY: ${{ secrets.DEV_KEY }}
50      ARGS: '-rltgoDzvO --delete'
51      SOURCE: 'deploy/'
52      REMOTE_HOST: ${{ secrets.DEV_HOST }}
53      REMOTE_USER: plyfolio-dev
54      TARGET: '/home/plyfolio-dev/${{ steps.vars.outputs.environment }}/fe/deploy'
55   {
56  "name": "stackstream-fe",
57  "version": "1.0.0",
58  "authors": [
59    "fayyaznofal@gmail.com"
60  ],
61  "private": true,
62  "dependencies": {
63    "@fortawesome/fontawesome-svg-core": "^1.2.34",
64    "@fortawesome/free-solid-svg-icons": "^5.15.2",
65    "@fortawesome/react-fontawesome": "^0.1.14",
66    "@fullcalendar/bootstrap": "^5.5.0",
67    "@fullcalendar/core": "^5.5.0",
68    "@fullcalendar/daygrid": "^5.5.0",
69    "@fullcalendar/interaction": "^5.5.0",
70    "@fullcalendar/react": "^5.5.0",
71    "@lourenci/react-kanban": "^2.1.0",
72    "@redux-saga/simple-saga-monitor": "^1.1.2",
73    "@testing-library/jest-dom": "^5.11.9",
74    "@testing-library/react": "^11.2.3",
75    "@testing-library/user-event": "^12.6.0",
76    "@toast-ui/react-chart": "^1.0.2",
77    "@types/jest": "^26.0.14",
78    "@types/node": "^14.10.3",
79    "@types/react": "^16.9.49",
80    "@types/react-dom": "^16.9.8",
81    "@vtaits/react-color-picker": "^0.1.1",
82    "apexcharts": "^3.23.1",
83    "availity-reactstrap-validation": "^2.7.0",
84    "axios": "^0.21.1",
85    "axios-mock-adapter": "^1.19.0",
86    "axios-progress-bar": "^1.2.0",
87    "bootstrap": "^5.0.0-beta2",
88    "chart.js": "^2.9.4",
89    "chartist": "^0.11.4",
90    "classnames": "^2.2.6",
91    "components": "^0.1.0",
92    "dotenv": "^8.2.0",
93    "draft-js": "^0.11.7",
94    "echarts": "^4.9.0",
95    "echarts-for-react": "^2.0.16",
96    "firebase": "^8.2.3",
97    "google-maps-react": "^2.0.6",
98    "history": "^4.10.1",
99    "i": "^0.3.6",
100    "i18next": "^19.8.4",
101    "i18next-browser-languagedetector": "^6.0.1",
102    "jsonwebtoken": "^8.5.1",
103    "leaflet": "^1.7.1",
104    "lodash": "^4.17.21",
105    "lodash.clonedeep": "^4.5.0",
106    "lodash.get": "^4.4.2",
107    "metismenujs": "^1.2.1",
108    "mkdirp": "^1.0.4",
109    "moment": "2.29.1",
110    "moment-timezone": "^0.5.32",
111    "nouislider-react": "^3.3.9",
112    "npm": "^7.6.3",
113    "prop-types": "^15.7.2",
114    "query-string": "^6.14.0",
115    "react": "^16.13.1",
116    "react-apexcharts": "^1.3.7",
117    "react-auth-code-input": "^1.0.0",
118    "react-avatar": "^3.10.0",
119    "react-bootstrap": "^1.5.0",
120    "react-bootstrap-editable": "^0.8.2",
121    "react-bootstrap-sweetalert": "^5.2.0",
122    "react-bootstrap-table-next": "^4.0.3",
123    "react-bootstrap-table2-editor": "^1.4.0",
124    "react-bootstrap-table2-paginator": "^2.1.2",
125    "react-bootstrap-table2-toolkit": "^2.1.3",
126    "react-chartist": "^0.14.3",
127    "react-chartjs-2": "^2.11.1",
128    "react-color": "^2.19.3",
129    "react-confirm-alert": "^2.7.0",
130    "react-content-loader": "^6.0.1",
131    "react-countdown": "^2.3.1",
132    "react-countup": "^4.3.3",
133    "react-cropper": "^2.1.4",
134    "react-data-table-component": "^6.11.8",
135    "react-date-picker": "^8.0.6",
136    "react-datepicker": "^3.4.1",
137    "react-dom": "^16.13.1",
138    "react-draft-wysiwyg": "^1.14.5",
139    "react-drag-listview": "^0.1.8",
140    "react-drawer": "^1.3.4",
141    "react-dropzone": "^11.2.4",
142    "react-dual-listbox": "^2.0.0",
143    "react-facebook-login": "^4.1.1",
144    "react-flatpickr": "^3.10.6",
145    "react-google-login": "^5.2.2",
146    "react-hook-form": "^7.15.2",
147    "react-i18next": "^11.8.5",
148    "react-icons": "^4.2.0",
149    "react-image-lightbox": "^5.1.1",
150    "react-input-mask": "^2.0.4",
151    "react-jvectormap": "^0.0.16",
152    "react-leaflet": "^3.0.5",
153    "react-meta-tags": "^1.0.1",
154    "react-modal-video": "^1.2.6",
155    "react-notifications": "^1.7.2",
156    "react-number-format": "^4.7.3",
157    "react-perfect-scrollbar": "^1.5.8",
158    "react-rangeslider": "^2.2.0",
159    "react-rating": "^2.0.5",
160    "react-rating-tooltip": "^1.1.6",
161    "react-redux": "^7.2.1",
162    "react-responsive-carousel": "^3.2.11",
163    "react-router-dom": "^5.2.0",
164    "react-script": "^2.0.5",
165    "react-scripts": "3.4.3",
166    "react-select": "^4.3.1",
167    "react-sparklines": "^1.7.0",
168    "react-star-ratings": "^2.3.0",
169    "react-super-responsive-table": "^5.2.0",
170    "react-switch": "^6.0.0",
171    "react-table": "^7.6.3",
172    "react-toastify": "^7.0.3",
173    "react-toastr": "^3.0.0",
174    "react-twitter-auth": "0.0.13",
175    "reactstrap": "^8.8.1",
176    "recharts": "^2.0.8",
177    "redux": "^4.0.5",
178    "redux-saga": "^1.1.3",
179    "reselect": "^4.0.0",
180    "sass": "^1.37.5",
181    "simplebar-react": "^2.3.0",
182    "styled": "^1.0.0",
183    "styled-components": "^5.2.1",
184    "toastr": "^2.1.4",
185    "typescript": "^4.0.2",
186    "universal-cookie": "^4.0.4"
187  },
188  "devDependencies": {
189    "@typescript-eslint/eslint-plugin": "^2.27.0",
190    "@typescript-eslint/parser": "^2.27.0",
191    "@typescript-eslint/typescript-estree": "^4.15.2",
192    "eslint-config-prettier": "^6.10.1",
193    "eslint-plugin-prettier": "^3.1.2",
194    "husky": "^4.2.5",
195    "lint-staged": "^10.1.3",
196    "prettier": "^1.19.1",
197    "react-test-renderer": "^16.13.1",
198    "redux-devtools-extension": "^2.13.8",
199    "redux-mock-store": "^1.5.4"
200  },
201  "scripts": {
202    "start": "react-scripts start",
203    "build": "react-scripts build && mv build ./deploy/build",
204    "build-local": "react-scripts build",
205    "test": "react-scripts test",
206    "eject": "react-scripts eject"
207  },
208  "eslintConfig": {
209    "extends": "react-app"
210  },
211  "husky": {
212    "hooks": {
213      "pre-commit": "lint-staged"
214    }
215  },
216  "lint-staged": {
217    "*.{js,ts,tsx}": [
218      "eslint --fix"
219    ]
220  },
221  "browserslist": {
222    "production": [
223      ">0.2%",
224      "not dead",
225      "not op_mini all"
226    ],
227    "development": [
228      "last 1 chrome version",
229      "last 1 firefox version",
230      "last 1 safari version"
231    ]
232  }
233}
234

ANSWER

Answered 2022-Mar-16 at 07:01

First, this error message is indeed expected on Jan. 11th, 2022.
See "Improving Git protocol security on GitHub".

January 11, 2022 Final brownout.

This is the full brownout period where we’ll temporarily stop accepting the deprecated key and signature types, ciphers, and MACs, and the unencrypted Git protocol.
This will help clients discover any lingering use of older keys or old URLs.

Second, check your package.json dependencies for any git:// URL, as in this example, fixed in this PR.

As noted by Jörg W Mittag:

There was a 4-month warning.
The entire Internet has been moving away from unauthenticated, unencrypted protocols for a decade, it's not like this is a huge surprise.

Personally, I consider it less an "issue" and more "detecting unmaintained dependencies".

Plus, this is still only the brownout period, so the protocol will only be disabled for a short period of time, allowing developers to discover the problem.

The permanent shutdown is not until March 15th.


For GitHub Actions:

As in actions/checkout issue 14, you can add as a first step:

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9  steps:
10  - name: Checkout
11    uses: actions/checkout@v2
12
13  - id: vars
14    run: |
15      if [ '${{ github.ref }}' == 'refs/heads/master' ]; then echo "::set-output name=environment::prod_stackstream" ; echo "::set-output name=api-url::api" ; elif [ '${{ github.ref }}' == 'refs/heads/staging' ]; then echo "::set-output name=environment::staging_stackstream"  ; echo "::set-output name=api-url::stagingapi" ; else echo "::set-output name=environment::dev_stackstream" ; echo "::set-output name=api-url::devapi" ; fi
16
17  - uses: pCYSl5EDgo/cat@master
18    id: slack
19    with:
20      path: .github/workflows/slack.txt
21
22  - name: Slack Start Notification
23    uses: 8398a7/action-slack@v3
24    env:
25      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
26      ENVIRONMENT: '`${{ steps.vars.outputs.environment }}`'
27      COLOR: good
28      STATUS: '`Started`'
29    with:
30      status: custom
31      fields: workflow,job,commit,repo,ref,author,took
32      custom_payload: |
33        ${{ steps.slack.outputs.text }}
34
35  - name: Installing modules
36    env:
37      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
38    run: yarn install
39
40  - name: Create Frontend Build
41    env:
42      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
43    run: yarn build
44
45  - name: Deploy to Frontend Server DEV
46    if: ${{ contains(github.ref, 'dev') }}
47    uses: easingthemes/ssh-deploy@v2.1.5
48    env:
49      SSH_PRIVATE_KEY: ${{ secrets.DEV_KEY }}
50      ARGS: '-rltgoDzvO --delete'
51      SOURCE: 'deploy/'
52      REMOTE_HOST: ${{ secrets.DEV_HOST }}
53      REMOTE_USER: plyfolio-dev
54      TARGET: '/home/plyfolio-dev/${{ steps.vars.outputs.environment }}/fe/deploy'
55   {
56  "name": "stackstream-fe",
57  "version": "1.0.0",
58  "authors": [
59    "fayyaznofal@gmail.com"
60  ],
61  "private": true,
62  "dependencies": {
63    "@fortawesome/fontawesome-svg-core": "^1.2.34",
64    "@fortawesome/free-solid-svg-icons": "^5.15.2",
65    "@fortawesome/react-fontawesome": "^0.1.14",
66    "@fullcalendar/bootstrap": "^5.5.0",
67    "@fullcalendar/core": "^5.5.0",
68    "@fullcalendar/daygrid": "^5.5.0",
69    "@fullcalendar/interaction": "^5.5.0",
70    "@fullcalendar/react": "^5.5.0",
71    "@lourenci/react-kanban": "^2.1.0",
72    "@redux-saga/simple-saga-monitor": "^1.1.2",
73    "@testing-library/jest-dom": "^5.11.9",
74    "@testing-library/react": "^11.2.3",
75    "@testing-library/user-event": "^12.6.0",
76    "@toast-ui/react-chart": "^1.0.2",
77    "@types/jest": "^26.0.14",
78    "@types/node": "^14.10.3",
79    "@types/react": "^16.9.49",
80    "@types/react-dom": "^16.9.8",
81    "@vtaits/react-color-picker": "^0.1.1",
82    "apexcharts": "^3.23.1",
83    "availity-reactstrap-validation": "^2.7.0",
84    "axios": "^0.21.1",
85    "axios-mock-adapter": "^1.19.0",
86    "axios-progress-bar": "^1.2.0",
87    "bootstrap": "^5.0.0-beta2",
88    "chart.js": "^2.9.4",
89    "chartist": "^0.11.4",
90    "classnames": "^2.2.6",
91    "components": "^0.1.0",
92    "dotenv": "^8.2.0",
93    "draft-js": "^0.11.7",
94    "echarts": "^4.9.0",
95    "echarts-for-react": "^2.0.16",
96    "firebase": "^8.2.3",
97    "google-maps-react": "^2.0.6",
98    "history": "^4.10.1",
99    "i": "^0.3.6",
100    "i18next": "^19.8.4",
101    "i18next-browser-languagedetector": "^6.0.1",
102    "jsonwebtoken": "^8.5.1",
103    "leaflet": "^1.7.1",
104    "lodash": "^4.17.21",
105    "lodash.clonedeep": "^4.5.0",
106    "lodash.get": "^4.4.2",
107    "metismenujs": "^1.2.1",
108    "mkdirp": "^1.0.4",
109    "moment": "2.29.1",
110    "moment-timezone": "^0.5.32",
111    "nouislider-react": "^3.3.9",
112    "npm": "^7.6.3",
113    "prop-types": "^15.7.2",
114    "query-string": "^6.14.0",
115    "react": "^16.13.1",
116    "react-apexcharts": "^1.3.7",
117    "react-auth-code-input": "^1.0.0",
118    "react-avatar": "^3.10.0",
119    "react-bootstrap": "^1.5.0",
120    "react-bootstrap-editable": "^0.8.2",
121    "react-bootstrap-sweetalert": "^5.2.0",
122    "react-bootstrap-table-next": "^4.0.3",
123    "react-bootstrap-table2-editor": "^1.4.0",
124    "react-bootstrap-table2-paginator": "^2.1.2",
125    "react-bootstrap-table2-toolkit": "^2.1.3",
126    "react-chartist": "^0.14.3",
127    "react-chartjs-2": "^2.11.1",
128    "react-color": "^2.19.3",
129    "react-confirm-alert": "^2.7.0",
130    "react-content-loader": "^6.0.1",
131    "react-countdown": "^2.3.1",
132    "react-countup": "^4.3.3",
133    "react-cropper": "^2.1.4",
134    "react-data-table-component": "^6.11.8",
135    "react-date-picker": "^8.0.6",
136    "react-datepicker": "^3.4.1",
137    "react-dom": "^16.13.1",
138    "react-draft-wysiwyg": "^1.14.5",
139    "react-drag-listview": "^0.1.8",
140    "react-drawer": "^1.3.4",
141    "react-dropzone": "^11.2.4",
142    "react-dual-listbox": "^2.0.0",
143    "react-facebook-login": "^4.1.1",
144    "react-flatpickr": "^3.10.6",
145    "react-google-login": "^5.2.2",
146    "react-hook-form": "^7.15.2",
147    "react-i18next": "^11.8.5",
148    "react-icons": "^4.2.0",
149    "react-image-lightbox": "^5.1.1",
150    "react-input-mask": "^2.0.4",
151    "react-jvectormap": "^0.0.16",
152    "react-leaflet": "^3.0.5",
153    "react-meta-tags": "^1.0.1",
154    "react-modal-video": "^1.2.6",
155    "react-notifications": "^1.7.2",
156    "react-number-format": "^4.7.3",
157    "react-perfect-scrollbar": "^1.5.8",
158    "react-rangeslider": "^2.2.0",
159    "react-rating": "^2.0.5",
160    "react-rating-tooltip": "^1.1.6",
161    "react-redux": "^7.2.1",
162    "react-responsive-carousel": "^3.2.11",
163    "react-router-dom": "^5.2.0",
164    "react-script": "^2.0.5",
165    "react-scripts": "3.4.3",
166    "react-select": "^4.3.1",
167    "react-sparklines": "^1.7.0",
168    "react-star-ratings": "^2.3.0",
169    "react-super-responsive-table": "^5.2.0",
170    "react-switch": "^6.0.0",
171    "react-table": "^7.6.3",
172    "react-toastify": "^7.0.3",
173    "react-toastr": "^3.0.0",
174    "react-twitter-auth": "0.0.13",
175    "reactstrap": "^8.8.1",
176    "recharts": "^2.0.8",
177    "redux": "^4.0.5",
178    "redux-saga": "^1.1.3",
179    "reselect": "^4.0.0",
180    "sass": "^1.37.5",
181    "simplebar-react": "^2.3.0",
182    "styled": "^1.0.0",
183    "styled-components": "^5.2.1",
184    "toastr": "^2.1.4",
185    "typescript": "^4.0.2",
186    "universal-cookie": "^4.0.4"
187  },
188  "devDependencies": {
189    "@typescript-eslint/eslint-plugin": "^2.27.0",
190    "@typescript-eslint/parser": "^2.27.0",
191    "@typescript-eslint/typescript-estree": "^4.15.2",
192    "eslint-config-prettier": "^6.10.1",
193    "eslint-plugin-prettier": "^3.1.2",
194    "husky": "^4.2.5",
195    "lint-staged": "^10.1.3",
196    "prettier": "^1.19.1",
197    "react-test-renderer": "^16.13.1",
198    "redux-devtools-extension": "^2.13.8",
199    "redux-mock-store": "^1.5.4"
200  },
201  "scripts": {
202    "start": "react-scripts start",
203    "build": "react-scripts build && mv build ./deploy/build",
204    "build-local": "react-scripts build",
205    "test": "react-scripts test",
206    "eject": "react-scripts eject"
207  },
208  "eslintConfig": {
209    "extends": "react-app"
210  },
211  "husky": {
212    "hooks": {
213      "pre-commit": "lint-staged"
214    }
215  },
216  "lint-staged": {
217    "*.{js,ts,tsx}": [
218      "eslint --fix"
219    ]
220  },
221  "browserslist": {
222    "production": [
223      ">0.2%",
224      "not dead",
225      "not op_mini all"
226    ],
227    "development": [
228      "last 1 chrome version",
229      "last 1 firefox version",
230      "last 1 safari version"
231    ]
232  }
233}
234    - name: Fix up git URLs
235      run: echo -e '[url "https://github.com/"]\n  insteadOf = "git://github.com/"' >> ~/.gitconfig
236

That will change any git://github.com/ into https://github.com/.

For local projects

For all your repositories, you can set:

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9  steps:
10  - name: Checkout
11    uses: actions/checkout@v2
12
13  - id: vars
14    run: |
15      if [ '${{ github.ref }}' == 'refs/heads/master' ]; then echo "::set-output name=environment::prod_stackstream" ; echo "::set-output name=api-url::api" ; elif [ '${{ github.ref }}' == 'refs/heads/staging' ]; then echo "::set-output name=environment::staging_stackstream"  ; echo "::set-output name=api-url::stagingapi" ; else echo "::set-output name=environment::dev_stackstream" ; echo "::set-output name=api-url::devapi" ; fi
16
17  - uses: pCYSl5EDgo/cat@master
18    id: slack
19    with:
20      path: .github/workflows/slack.txt
21
22  - name: Slack Start Notification
23    uses: 8398a7/action-slack@v3
24    env:
25      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
26      ENVIRONMENT: '`${{ steps.vars.outputs.environment }}`'
27      COLOR: good
28      STATUS: '`Started`'
29    with:
30      status: custom
31      fields: workflow,job,commit,repo,ref,author,took
32      custom_payload: |
33        ${{ steps.slack.outputs.text }}
34
35  - name: Installing modules
36    env:
37      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
38    run: yarn install
39
40  - name: Create Frontend Build
41    env:
42      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
43    run: yarn build
44
45  - name: Deploy to Frontend Server DEV
46    if: ${{ contains(github.ref, 'dev') }}
47    uses: easingthemes/ssh-deploy@v2.1.5
48    env:
49      SSH_PRIVATE_KEY: ${{ secrets.DEV_KEY }}
50      ARGS: '-rltgoDzvO --delete'
51      SOURCE: 'deploy/'
52      REMOTE_HOST: ${{ secrets.DEV_HOST }}
53      REMOTE_USER: plyfolio-dev
54      TARGET: '/home/plyfolio-dev/${{ steps.vars.outputs.environment }}/fe/deploy'
55   {
56  "name": "stackstream-fe",
57  "version": "1.0.0",
58  "authors": [
59    "fayyaznofal@gmail.com"
60  ],
61  "private": true,
62  "dependencies": {
63    "@fortawesome/fontawesome-svg-core": "^1.2.34",
64    "@fortawesome/free-solid-svg-icons": "^5.15.2",
65    "@fortawesome/react-fontawesome": "^0.1.14",
66    "@fullcalendar/bootstrap": "^5.5.0",
67    "@fullcalendar/core": "^5.5.0",
68    "@fullcalendar/daygrid": "^5.5.0",
69    "@fullcalendar/interaction": "^5.5.0",
70    "@fullcalendar/react": "^5.5.0",
71    "@lourenci/react-kanban": "^2.1.0",
72    "@redux-saga/simple-saga-monitor": "^1.1.2",
73    "@testing-library/jest-dom": "^5.11.9",
74    "@testing-library/react": "^11.2.3",
75    "@testing-library/user-event": "^12.6.0",
76    "@toast-ui/react-chart": "^1.0.2",
77    "@types/jest": "^26.0.14",
78    "@types/node": "^14.10.3",
79    "@types/react": "^16.9.49",
80    "@types/react-dom": "^16.9.8",
81    "@vtaits/react-color-picker": "^0.1.1",
82    "apexcharts": "^3.23.1",
83    "availity-reactstrap-validation": "^2.7.0",
84    "axios": "^0.21.1",
85    "axios-mock-adapter": "^1.19.0",
86    "axios-progress-bar": "^1.2.0",
87    "bootstrap": "^5.0.0-beta2",
88    "chart.js": "^2.9.4",
89    "chartist": "^0.11.4",
90    "classnames": "^2.2.6",
91    "components": "^0.1.0",
92    "dotenv": "^8.2.0",
93    "draft-js": "^0.11.7",
94    "echarts": "^4.9.0",
95    "echarts-for-react": "^2.0.16",
96    "firebase": "^8.2.3",
97    "google-maps-react": "^2.0.6",
98    "history": "^4.10.1",
99    "i": "^0.3.6",
100    "i18next": "^19.8.4",
101    "i18next-browser-languagedetector": "^6.0.1",
102    "jsonwebtoken": "^8.5.1",
103    "leaflet": "^1.7.1",
104    "lodash": "^4.17.21",
105    "lodash.clonedeep": "^4.5.0",
106    "lodash.get": "^4.4.2",
107    "metismenujs": "^1.2.1",
108    "mkdirp": "^1.0.4",
109    "moment": "2.29.1",
110    "moment-timezone": "^0.5.32",
111    "nouislider-react": "^3.3.9",
112    "npm": "^7.6.3",
113    "prop-types": "^15.7.2",
114    "query-string": "^6.14.0",
115    "react": "^16.13.1",
116    "react-apexcharts": "^1.3.7",
117    "react-auth-code-input": "^1.0.0",
118    "react-avatar": "^3.10.0",
119    "react-bootstrap": "^1.5.0",
120    "react-bootstrap-editable": "^0.8.2",
121    "react-bootstrap-sweetalert": "^5.2.0",
122    "react-bootstrap-table-next": "^4.0.3",
123    "react-bootstrap-table2-editor": "^1.4.0",
124    "react-bootstrap-table2-paginator": "^2.1.2",
125    "react-bootstrap-table2-toolkit": "^2.1.3",
126    "react-chartist": "^0.14.3",
127    "react-chartjs-2": "^2.11.1",
128    "react-color": "^2.19.3",
129    "react-confirm-alert": "^2.7.0",
130    "react-content-loader": "^6.0.1",
131    "react-countdown": "^2.3.1",
132    "react-countup": "^4.3.3",
133    "react-cropper": "^2.1.4",
134    "react-data-table-component": "^6.11.8",
135    "react-date-picker": "^8.0.6",
136    "react-datepicker": "^3.4.1",
137    "react-dom": "^16.13.1",
138    "react-draft-wysiwyg": "^1.14.5",
139    "react-drag-listview": "^0.1.8",
140    "react-drawer": "^1.3.4",
141    "react-dropzone": "^11.2.4",
142    "react-dual-listbox": "^2.0.0",
143    "react-facebook-login": "^4.1.1",
144    "react-flatpickr": "^3.10.6",
145    "react-google-login": "^5.2.2",
146    "react-hook-form": "^7.15.2",
147    "react-i18next": "^11.8.5",
148    "react-icons": "^4.2.0",
149    "react-image-lightbox": "^5.1.1",
150    "react-input-mask": "^2.0.4",
151    "react-jvectormap": "^0.0.16",
152    "react-leaflet": "^3.0.5",
153    "react-meta-tags": "^1.0.1",
154    "react-modal-video": "^1.2.6",
155    "react-notifications": "^1.7.2",
156    "react-number-format": "^4.7.3",
157    "react-perfect-scrollbar": "^1.5.8",
158    "react-rangeslider": "^2.2.0",
159    "react-rating": "^2.0.5",
160    "react-rating-tooltip": "^1.1.6",
161    "react-redux": "^7.2.1",
162    "react-responsive-carousel": "^3.2.11",
163    "react-router-dom": "^5.2.0",
164    "react-script": "^2.0.5",
165    "react-scripts": "3.4.3",
166    "react-select": "^4.3.1",
167    "react-sparklines": "^1.7.0",
168    "react-star-ratings": "^2.3.0",
169    "react-super-responsive-table": "^5.2.0",
170    "react-switch": "^6.0.0",
171    "react-table": "^7.6.3",
172    "react-toastify": "^7.0.3",
173    "react-toastr": "^3.0.0",
174    "react-twitter-auth": "0.0.13",
175    "reactstrap": "^8.8.1",
176    "recharts": "^2.0.8",
177    "redux": "^4.0.5",
178    "redux-saga": "^1.1.3",
179    "reselect": "^4.0.0",
180    "sass": "^1.37.5",
181    "simplebar-react": "^2.3.0",
182    "styled": "^1.0.0",
183    "styled-components": "^5.2.1",
184    "toastr": "^2.1.4",
185    "typescript": "^4.0.2",
186    "universal-cookie": "^4.0.4"
187  },
188  "devDependencies": {
189    "@typescript-eslint/eslint-plugin": "^2.27.0",
190    "@typescript-eslint/parser": "^2.27.0",
191    "@typescript-eslint/typescript-estree": "^4.15.2",
192    "eslint-config-prettier": "^6.10.1",
193    "eslint-plugin-prettier": "^3.1.2",
194    "husky": "^4.2.5",
195    "lint-staged": "^10.1.3",
196    "prettier": "^1.19.1",
197    "react-test-renderer": "^16.13.1",
198    "redux-devtools-extension": "^2.13.8",
199    "redux-mock-store": "^1.5.4"
200  },
201  "scripts": {
202    "start": "react-scripts start",
203    "build": "react-scripts build && mv build ./deploy/build",
204    "build-local": "react-scripts build",
205    "test": "react-scripts test",
206    "eject": "react-scripts eject"
207  },
208  "eslintConfig": {
209    "extends": "react-app"
210  },
211  "husky": {
212    "hooks": {
213      "pre-commit": "lint-staged"
214    }
215  },
216  "lint-staged": {
217    "*.{js,ts,tsx}": [
218      "eslint --fix"
219    ]
220  },
221  "browserslist": {
222    "production": [
223      ">0.2%",
224      "not dead",
225      "not op_mini all"
226    ],
227    "development": [
228      "last 1 chrome version",
229      "last 1 firefox version",
230      "last 1 safari version"
231    ]
232  }
233}
234    - name: Fix up git URLs
235      run: echo -e '[url "https://github.com/"]\n  insteadOf = "git://github.com/"' >> ~/.gitconfig
236git config --global url."https://github.com/".insteadOf git://github.com/
237

You can also use SSH, but GitHub Security reminds us that, as of March 15th, 2022, GitHub stopped accepting DSA keys. RSA keys uploaded after Nov 2, 2021 will work only with SHA-2 signatures.
The deprecated MACs, ciphers, and unencrypted Git protocol are permanently disabled.

So this (with the right key) would work:

1Command: git
2Arguments: ls-remote --tags --heads git://github.com/adobe-webplatform/eve.git
3Directory: /home/runner/work/stackstream-fe/stackstream-fe
4Output:
5fatal: remote error: 
6  The unauthenticated git protocol on port 9418 is no longer supported.
7    - name: Installing modules
8      run: yarn install
9  steps:
10  - name: Checkout
11    uses: actions/checkout@v2
12
13  - id: vars
14    run: |
15      if [ '${{ github.ref }}' == 'refs/heads/master' ]; then echo "::set-output name=environment::prod_stackstream" ; echo "::set-output name=api-url::api" ; elif [ '${{ github.ref }}' == 'refs/heads/staging' ]; then echo "::set-output name=environment::staging_stackstream"  ; echo "::set-output name=api-url::stagingapi" ; else echo "::set-output name=environment::dev_stackstream" ; echo "::set-output name=api-url::devapi" ; fi
16
17  - uses: pCYSl5EDgo/cat@master
18    id: slack
19    with:
20      path: .github/workflows/slack.txt
21
22  - name: Slack Start Notification
23    uses: 8398a7/action-slack@v3
24    env:
25      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
26      ENVIRONMENT: '`${{ steps.vars.outputs.environment }}`'
27      COLOR: good
28      STATUS: '`Started`'
29    with:
30      status: custom
31      fields: workflow,job,commit,repo,ref,author,took
32      custom_payload: |
33        ${{ steps.slack.outputs.text }}
34
35  - name: Installing modules
36    env:
37      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
38    run: yarn install
39
40  - name: Create Frontend Build
41    env:
42      REACT_APP_API_URL: 'https://${{ steps.vars.outputs.api-url }}mergestack.com/api/v1'
43    run: yarn build
44
45  - name: Deploy to Frontend Server DEV
46    if: ${{ contains(github.ref, 'dev') }}
47    uses: easingthemes/ssh-deploy@v2.1.5
48    env:
49      SSH_PRIVATE_KEY: ${{ secrets.DEV_KEY }}
50      ARGS: '-rltgoDzvO --delete'
51      SOURCE: 'deploy/'
52      REMOTE_HOST: ${{ secrets.DEV_HOST }}
53      REMOTE_USER: plyfolio-dev
54      TARGET: '/home/plyfolio-dev/${{ steps.vars.outputs.environment }}/fe/deploy'
55   {
56  "name": "stackstream-fe",
57  "version": "1.0.0",
58  "authors": [
59    "fayyaznofal@gmail.com"
60  ],
61  "private": true,
62  "dependencies": {
63    "@fortawesome/fontawesome-svg-core": "^1.2.34",
64    "@fortawesome/free-solid-svg-icons": "^5.15.2",
65    "@fortawesome/react-fontawesome": "^0.1.14",
66    "@fullcalendar/bootstrap": "^5.5.0",
67    "@fullcalendar/core": "^5.5.0",
68    "@fullcalendar/daygrid": "^5.5.0",
69    "@fullcalendar/interaction": "^5.5.0",
70    "@fullcalendar/react": "^5.5.0",
71    "@lourenci/react-kanban": "^2.1.0",
72    "@redux-saga/simple-saga-monitor": "^1.1.2",
73    "@testing-library/jest-dom": "^5.11.9",
74    "@testing-library/react": "^11.2.3",
75    "@testing-library/user-event": "^12.6.0",
76    "@toast-ui/react-chart": "^1.0.2",
77    "@types/jest": "^26.0.14",
78    "@types/node": "^14.10.3",
79    "@types/react": "^16.9.49",
80    "@types/react-dom": "^16.9.8",
81    "@vtaits/react-color-picker": "^0.1.1",
82    "apexcharts": "^3.23.1",
83    "availity-reactstrap-validation": "^2.7.0",
84    "axios": "^0.21.1",
85    "axios-mock-adapter": "^1.19.0",
86    "axios-progress-bar": "^1.2.0",
87    "bootstrap": "^5.0.0-beta2",
88    "chart.js": "^2.9.4",
89    "chartist": "^0.11.4",
90    "classnames": "^2.2.6",
91    "components": "^0.1.0",
92    "dotenv": "^8.2.0",
93    "draft-js": "^0.11.7",
94    "echarts": "^4.9.0",
95    "echarts-for-react": "^2.0.16",
96    "firebase": "^8.2.3",
97    "google-maps-react": "^2.0.6",
98    "history": "^4.10.1",
99    "i": "^0.3.6",
100    "i18next": "^19.8.4",
101    "i18next-browser-languagedetector": "^6.0.1",
102    "jsonwebtoken": "^8.5.1",
103    "leaflet": "^1.7.1",
104    "lodash": "^4.17.21",
105    "lodash.clonedeep": "^4.5.0",
106    "lodash.get": "^4.4.2",
107    "metismenujs": "^1.2.1",
108    "mkdirp": "^1.0.4",
109    "moment": "2.29.1",
110    "moment-timezone": "^0.5.32",
111    "nouislider-react": "^3.3.9",
112    "npm": "^7.6.3",
113    "prop-types": "^15.7.2",
114    "query-string": "^6.14.0",
115    "react": "^16.13.1",
116    "react-apexcharts": "^1.3.7",
117    "react-auth-code-input": "^1.0.0",
118    "react-avatar": "^3.10.0",
119    "react-bootstrap": "^1.5.0",
120    "react-bootstrap-editable": "^0.8.2",
121    "react-bootstrap-sweetalert": "^5.2.0",
122    "react-bootstrap-table-next": "^4.0.3",
123    "react-bootstrap-table2-editor": "^1.4.0",
124    "react-bootstrap-table2-paginator": "^2.1.2",
125    "react-bootstrap-table2-toolkit": "^2.1.3",
126    "react-chartist": "^0.14.3",
127    "react-chartjs-2": "^2.11.1",
128    "react-color": "^2.19.3",
129    "react-confirm-alert": "^2.7.0",
130    "react-content-loader": "^6.0.1",
131    "react-countdown": "^2.3.1",
132    "react-countup": "^4.3.3",
133    "react-cropper": "^2.1.4",
134    "react-data-table-component": "^6.11.8",
135    "react-date-picker": "^8.0.6",
136    "react-datepicker": "^3.4.1",
137    "react-dom": "^16.13.1",
138    "react-draft-wysiwyg": "^1.14.5",
139    "react-drag-listview": "^0.1.8",
140    "react-drawer": "^1.3.4",
141    "react-dropzone": "^11.2.4",
142    "react-dual-listbox": "^2.0.0",
143    "react-facebook-login": "^4.1.1",
144    "react-flatpickr": "^3.10.6",
145    "react-google-login": "^5.2.2",
146    "react-hook-form": "^7.15.2",
147    "react-i18next": "^11.8.5",
148    "react-icons": "^4.2.0",
149    "react-image-lightbox": "^5.1.1",
150    "react-input-mask": "^2.0.4",
151    "react-jvectormap": "^0.0.16",
152    "react-leaflet": "^3.0.5",
153    "react-meta-tags": "^1.0.1",
154    "react-modal-video": "^1.2.6",
155    "react-notifications": "^1.7.2",
156    "react-number-format": "^4.7.3",
157    "react-perfect-scrollbar": "^1.5.8",
158    "react-rangeslider": "^2.2.0",
159    "react-rating": "^2.0.5",
160    "react-rating-tooltip": "^1.1.6",
161    "react-redux": "^7.2.1",
162    "react-responsive-carousel": "^3.2.11",
163    "react-router-dom": "^5.2.0",
164    "react-script": "^2.0.5",
165    "react-scripts": "3.4.3",
166    "react-select": "^4.3.1",
167    "react-sparklines": "^1.7.0",
168    "react-star-ratings": "^2.3.0",
169    "react-super-responsive-table": "^5.2.0",
170    "react-switch": "^6.0.0",
171    "react-table": "^7.6.3",
172    "react-toastify": "^7.0.3",
173    "react-toastr": "^3.0.0",
174    "react-twitter-auth": "0.0.13",
175    "reactstrap": "^8.8.1",
176    "recharts": "^2.0.8",
177    "redux": "^4.0.5",
178    "redux-saga": "^1.1.3",
179    "reselect": "^4.0.0",
180    "sass": "^1.37.5",
181    "simplebar-react": "^2.3.0",
182    "styled": "^1.0.0",
183    "styled-components": "^5.2.1",
184    "toastr": "^2.1.4",
185    "typescript": "^4.0.2",
186    "universal-cookie": "^4.0.4"
187  },
188  "devDependencies": {
189    "@typescript-eslint/eslint-plugin": "^2.27.0",
190    "@typescript-eslint/parser": "^2.27.0",
191    "@typescript-eslint/typescript-estree": "^4.15.2",
192    "eslint-config-prettier": "^6.10.1",
193    "eslint-plugin-prettier": "^3.1.2",
194    "husky": "^4.2.5",
195    "lint-staged": "^10.1.3",
196    "prettier": "^1.19.1",
197    "react-test-renderer": "^16.13.1",
198    "redux-devtools-extension": "^2.13.8",
199    "redux-mock-store": "^1.5.4"
200  },
201  "scripts": {
202    "start": "react-scripts start",
203    "build": "react-scripts build && mv build ./deploy/build",
204    "build-local": "react-scripts build",
205    "test": "react-scripts test",
206    "eject": "react-scripts eject"
207  },
208  "eslintConfig": {
209    "extends": "react-app"
210  },
211  "husky": {
212    "hooks": {
213      "pre-commit": "lint-staged"
214    }
215  },
216  "lint-staged": {
217    "*.{js,ts,tsx}": [
218      "eslint --fix"
219    ]
220  },
221  "browserslist": {
222    "production": [
223      ">0.2%",
224      "not dead",
225      "not op_mini all"
226    ],
227    "development": [
228      "last 1 chrome version",
229      "last 1 firefox version",
230      "last 1 safari version"
231    ]
232  }
233}
234    - name: Fix up git URLs
235      run: echo -e '[url "https://github.com/"]\n  insteadOf = "git://github.com/"' >> ~/.gitconfig
236git config --global url."https://github.com/".insteadOf git://github.com/
237git config --global url."git@github.com:".insteadOf git://github.com/
238

That will change any git://github.com/ (unencrypted Git protocol) into git@github.com: (SSH URL).

Source https://stackoverflow.com/questions/70663523

QUESTION

FastAPI - GET request results in typeerror (value is not a valid dict)

Asked 2022-Mar-23 at 22:19

this is my database schema.

enter image description here

I defined my Schema like this:

from pydantic import BaseModel

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6

This is my model:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13

In a crud.py I define a get_attributes method.

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15

This is my GET endpoint:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20

The connection to the database seems to work, but a problem is the datatype:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35

Why does FASTApi expect a dictionary here? I don´t really understand it, since I am not able to even print the response. How can I fix this?

ANSWER

Answered 2022-Mar-23 at 22:19

SQLAlchemy does not return a dictionary, which is what pydantic expects by default. You can configure your model to also support loading from standard orm parameters (i.e. attributes on the object instead of dictionary lookups):

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35class Userattribute(BaseModel):
36    name: str
37    value: str
38    user_id: str
39    id: str
40
41    class Config:
42        orm_mode = True
43

You can also attach a debugger right before the call to return to see what's being returned.

Since this answer has become slightly popular, I'd like to also mention that you can make orm_mode = True the default for your schema classes by having a common parent class that inherits from BaseModel:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35class Userattribute(BaseModel):
36    name: str
37    value: str
38    user_id: str
39    id: str
40
41    class Config:
42        orm_mode = True
43class OurBaseModel(BaseModel):
44    class Config:
45        orm_mode = True
46
47
48class Userattribute(OurBaseModel):
49    name: str
50    value: str
51    user_id: str
52    id: str
53

This is useful if you want to support orm_mode for most of your classes (and for those where you don't, inherit from the regular BaseModel).

Source https://stackoverflow.com/questions/69504352

QUESTION

Vue 3 and Vuetify 3 Alpha: ValidationError: progress plugin invalid options

Asked 2022-Feb-14 at 09:55

After creating a Vue 3 project, adding Vuetify 3 Alpha, when I run "npm run serve", this is the error I get. I tried without adding Vuetify 3 Alpha and the Vue 3 project starts fine, it's just after adding the Vuetify that the error appears.

1INFO  Starting development server...
2ERROR  ValidationError: Progress Plugin Invalid Options
3
4    options should NOT have additional properties
5    options should NOT have additional properties
6    options should NOT have additional properties
7    options should pass "instanceof" keyword validation
8    options should match exactly one schema in oneOf
9
10ValidationError: Progress Plugin Invalid Options
11
12options should NOT have additional properties
13options should NOT have additional properties
14options should NOT have additional properties
15options should pass "instanceof" keyword validation
16options should match exactly one schema in oneOf
17
18at validateOptions (c:\wamp64\www\vuetify-3\node_modules\webpack\node_modules\schema-utils\src\validateOptions.js:32:11)
19at new ProgressPlugin (c:\wamp64\www\vuetify-3\node_modules\webpack\lib\ProgressPlugin.js:62:3)
20at new Progress (c:\wamp64\www\vuetify-3\node_modules\progress-webpack-plugin\index.js:25:21)
21at new progressPlugin (c:\wamp64\www\vuetify-3\node_modules\progress-webpack-plugin\index.js:127:10)
22at c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Plugin.js:14:18
23at Object.toConfig (c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Plugin.js:78:22)
24at c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Config.js:129:63
25at Array.map (<anonymous>)
26at module.exports.toConfig (c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Config.js:129:40)
27at Service.resolveWebpackConfig (c:\wamp64\www\vuetify-3\node_modules\@vue\cli-service\lib\Service.js:261:34)
28

ANSWER

Answered 2021-Nov-15 at 03:41

I had the same error after running vue add vuetify

Run npm update and re-create the project again.

Also make sure you are on the latest versions of the following.

1INFO  Starting development server...
2ERROR  ValidationError: Progress Plugin Invalid Options
3
4    options should NOT have additional properties
5    options should NOT have additional properties
6    options should NOT have additional properties
7    options should pass "instanceof" keyword validation
8    options should match exactly one schema in oneOf
9
10ValidationError: Progress Plugin Invalid Options
11
12options should NOT have additional properties
13options should NOT have additional properties
14options should NOT have additional properties
15options should pass "instanceof" keyword validation
16options should match exactly one schema in oneOf
17
18at validateOptions (c:\wamp64\www\vuetify-3\node_modules\webpack\node_modules\schema-utils\src\validateOptions.js:32:11)
19at new ProgressPlugin (c:\wamp64\www\vuetify-3\node_modules\webpack\lib\ProgressPlugin.js:62:3)
20at new Progress (c:\wamp64\www\vuetify-3\node_modules\progress-webpack-plugin\index.js:25:21)
21at new progressPlugin (c:\wamp64\www\vuetify-3\node_modules\progress-webpack-plugin\index.js:127:10)
22at c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Plugin.js:14:18
23at Object.toConfig (c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Plugin.js:78:22)
24at c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Config.js:129:63
25at Array.map (<anonymous>)
26at module.exports.toConfig (c:\wamp64\www\vuetify-3\node_modules\webpack-chain\src\Config.js:129:40)
27at Service.resolveWebpackConfig (c:\wamp64\www\vuetify-3\node_modules\@vue\cli-service\lib\Service.js:261:34)
28node -v && vue -V && npm -v
29
  • v17.0.1
  • @vue/cli 5.0.0-rc.0
  • 8.1.0

Source https://stackoverflow.com/questions/69931346

QUESTION

FastAPI - Pydantic - Value Error Raises Internal Server Error

Asked 2022-Jan-14 at 12:44

I am using FastAPI with Pydantic.

My problem - I need to raise ValueError using Pydantic

1from fastapi import FastAPI
2from pydantic import BaseModel, validator
3from fastapi import Depends, HTTPException
4
5app = FastAPI()
6
7class RankInput(BaseModel):
8
9    rank: int
10
11    @validator('rank')
12    def check_if_value_in_range(cls, v):
13        """
14        check if input rank is within range
15        """
16        if not 0 < v < 1000001:
17
18            raise ValueError("Rank Value Must be within range (0,1000000)")
19            #raise HTTPException(status_code=400, detail="Rank Value Error") - this works But I am looking for a solution using ValueError
20        return v
21
22def get_info_by_rank(rank):
23    return rank
24
25@app.get('/rank/{rank}')
26async def get_rank(value: RankInput = Depends()):
27    result = get_info_by_rank(value.rank)
28    return result
29

this piece of code gives Internal Server Error when a ValueError is raised

1from fastapi import FastAPI
2from pydantic import BaseModel, validator
3from fastapi import Depends, HTTPException
4
5app = FastAPI()
6
7class RankInput(BaseModel):
8
9    rank: int
10
11    @validator('rank')
12    def check_if_value_in_range(cls, v):
13        """
14        check if input rank is within range
15        """
16        if not 0 < v < 1000001:
17
18            raise ValueError("Rank Value Must be within range (0,1000000)")
19            #raise HTTPException(status_code=400, detail="Rank Value Error") - this works But I am looking for a solution using ValueError
20        return v
21
22def get_info_by_rank(rank):
23    return rank
24
25@app.get('/rank/{rank}')
26async def get_rank(value: RankInput = Depends()):
27    result = get_info_by_rank(value.rank)
28    return result
29INFO:     127.0.0.1:59427 - "GET /info/?rank=-1 HTTP/1.1" 500 Internal Server Error
30ERROR:    Exception in ASGI application
31Traceback (most recent call last):
32  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
33    result = await app(self.scope, self.receive, self.send)
34  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
35    return await self.app(scope, receive, send)
36  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
37    await super().__call__(scope, receive, send)
38  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
39    await self.middleware_stack(scope, receive, send)
40  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
41    raise exc from None
42  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
43    await self.app(scope, receive, _send)
44  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
45    raise exc from None
46  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
47    await self.app(scope, receive, sender)
48  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
49    await route.handle(scope, receive, send)
50  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
51    await self.app(scope, receive, send)
52  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
53    response = await func(request)
54  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
55    dependency_overrides_provider=dependency_overrides_provider,
56  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
57    solved = await run_in_threadpool(call, **sub_values)
58  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
59    return await loop.run_in_executor(None, func, *args)
60  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
61    result = self.fn(*self.args, **self.kwargs)
62  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
63pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
64rank
65  ValueError() takes no keyword arguments (type=type_error)
66ERROR:uvicorn.error:Exception in ASGI application
67Traceback (most recent call last):
68  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
69    result = await app(self.scope, self.receive, self.send)
70  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
71    return await self.app(scope, receive, send)
72  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
73    await super().__call__(scope, receive, send)
74  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
75    await self.middleware_stack(scope, receive, send)
76  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
77    raise exc from None
78  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
79    await self.app(scope, receive, _send)
80  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
81    raise exc from None
82  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
83    await self.app(scope, receive, sender)
84  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
85    await route.handle(scope, receive, send)
86  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
87    await self.app(scope, receive, send)
88  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
89    response = await func(request)
90  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
91    dependency_overrides_provider=dependency_overrides_provider,
92  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
93    solved = await run_in_threadpool(call, **sub_values)
94  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
95    return await loop.run_in_executor(None, func, *args)
96  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
97    result = self.fn(*self.args, **self.kwargs)
98  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
99pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
100rank
101  ValueError() takes no keyword arguments (type=type_error)
102

I also checked https://github.com/tiangolo/fastapi/issues/2180.

But I was not able to figure out a solution.

What I need to do is Raise ValueError with a Custom Status Code.

Note - I know I can get the Job Done by raising HTTPException.

But I am looking for a solution using ValueError

Could you tell me where I am going wrong?

Have Also Posted this Issue on Github - https://github.com/tiangolo/fastapi/issues/3761

ANSWER

Answered 2021-Aug-25 at 04:48

If you're not raising an HTTPException then normally any other uncaught exception will generate a 500 response (an Internal Server Error). If your intent is to respond with some other custom error message and HTTP status when raising a particular exception - say, ValueError - then you can use add a global exception handler to your app:

1from fastapi import FastAPI
2from pydantic import BaseModel, validator
3from fastapi import Depends, HTTPException
4
5app = FastAPI()
6
7class RankInput(BaseModel):
8
9    rank: int
10
11    @validator('rank')
12    def check_if_value_in_range(cls, v):
13        """
14        check if input rank is within range
15        """
16        if not 0 < v < 1000001:
17
18            raise ValueError("Rank Value Must be within range (0,1000000)")
19            #raise HTTPException(status_code=400, detail="Rank Value Error") - this works But I am looking for a solution using ValueError
20        return v
21
22def get_info_by_rank(rank):
23    return rank
24
25@app.get('/rank/{rank}')
26async def get_rank(value: RankInput = Depends()):
27    result = get_info_by_rank(value.rank)
28    return result
29INFO:     127.0.0.1:59427 - "GET /info/?rank=-1 HTTP/1.1" 500 Internal Server Error
30ERROR:    Exception in ASGI application
31Traceback (most recent call last):
32  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
33    result = await app(self.scope, self.receive, self.send)
34  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
35    return await self.app(scope, receive, send)
36  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
37    await super().__call__(scope, receive, send)
38  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
39    await self.middleware_stack(scope, receive, send)
40  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
41    raise exc from None
42  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
43    await self.app(scope, receive, _send)
44  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
45    raise exc from None
46  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
47    await self.app(scope, receive, sender)
48  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
49    await route.handle(scope, receive, send)
50  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
51    await self.app(scope, receive, send)
52  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
53    response = await func(request)
54  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
55    dependency_overrides_provider=dependency_overrides_provider,
56  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
57    solved = await run_in_threadpool(call, **sub_values)
58  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
59    return await loop.run_in_executor(None, func, *args)
60  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
61    result = self.fn(*self.args, **self.kwargs)
62  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
63pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
64rank
65  ValueError() takes no keyword arguments (type=type_error)
66ERROR:uvicorn.error:Exception in ASGI application
67Traceback (most recent call last):
68  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
69    result = await app(self.scope, self.receive, self.send)
70  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
71    return await self.app(scope, receive, send)
72  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
73    await super().__call__(scope, receive, send)
74  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
75    await self.middleware_stack(scope, receive, send)
76  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
77    raise exc from None
78  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
79    await self.app(scope, receive, _send)
80  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
81    raise exc from None
82  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
83    await self.app(scope, receive, sender)
84  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
85    await route.handle(scope, receive, send)
86  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
87    await self.app(scope, receive, send)
88  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
89    response = await func(request)
90  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
91    dependency_overrides_provider=dependency_overrides_provider,
92  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
93    solved = await run_in_threadpool(call, **sub_values)
94  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
95    return await loop.run_in_executor(None, func, *args)
96  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
97    result = self.fn(*self.args, **self.kwargs)
98  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
99pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
100rank
101  ValueError() takes no keyword arguments (type=type_error)
102from fastapi import FastAPI, Request
103from fastapi.responses import JSONResponse
104
105
106@app.exception_handler(ValueError)
107async def value_error_exception_handler(request: Request, exc: ValueError):
108    return JSONResponse(
109        status_code=400,
110        content={"message": str(exc)},
111    )
112

This will give a 400 response (or you can change the status code to whatever you like) like this:

1from fastapi import FastAPI
2from pydantic import BaseModel, validator
3from fastapi import Depends, HTTPException
4
5app = FastAPI()
6
7class RankInput(BaseModel):
8
9    rank: int
10
11    @validator('rank')
12    def check_if_value_in_range(cls, v):
13        """
14        check if input rank is within range
15        """
16        if not 0 < v < 1000001:
17
18            raise ValueError("Rank Value Must be within range (0,1000000)")
19            #raise HTTPException(status_code=400, detail="Rank Value Error") - this works But I am looking for a solution using ValueError
20        return v
21
22def get_info_by_rank(rank):
23    return rank
24
25@app.get('/rank/{rank}')
26async def get_rank(value: RankInput = Depends()):
27    result = get_info_by_rank(value.rank)
28    return result
29INFO:     127.0.0.1:59427 - "GET /info/?rank=-1 HTTP/1.1" 500 Internal Server Error
30ERROR:    Exception in ASGI application
31Traceback (most recent call last):
32  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
33    result = await app(self.scope, self.receive, self.send)
34  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
35    return await self.app(scope, receive, send)
36  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
37    await super().__call__(scope, receive, send)
38  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
39    await self.middleware_stack(scope, receive, send)
40  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
41    raise exc from None
42  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
43    await self.app(scope, receive, _send)
44  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
45    raise exc from None
46  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
47    await self.app(scope, receive, sender)
48  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
49    await route.handle(scope, receive, send)
50  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
51    await self.app(scope, receive, send)
52  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
53    response = await func(request)
54  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
55    dependency_overrides_provider=dependency_overrides_provider,
56  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
57    solved = await run_in_threadpool(call, **sub_values)
58  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
59    return await loop.run_in_executor(None, func, *args)
60  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
61    result = self.fn(*self.args, **self.kwargs)
62  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
63pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
64rank
65  ValueError() takes no keyword arguments (type=type_error)
66ERROR:uvicorn.error:Exception in ASGI application
67Traceback (most recent call last):
68  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
69    result = await app(self.scope, self.receive, self.send)
70  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
71    return await self.app(scope, receive, send)
72  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py", line 199, in __call__
73    await super().__call__(scope, receive, send)
74  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py", line 111, in __call__
75    await self.middleware_stack(scope, receive, send)
76  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 181, in __call__
77    raise exc from None
78  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py", line 159, in __call__
79    await self.app(scope, receive, _send)
80  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 82, in __call__
81    raise exc from None
82  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py", line 71, in __call__
83    await self.app(scope, receive, sender)
84  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 566, in __call__
85    await route.handle(scope, receive, send)
86  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 227, in handle
87    await self.app(scope, receive, send)
88  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
89    response = await func(request)
90  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py", line 195, in app
91    dependency_overrides_provider=dependency_overrides_provider,
92  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py", line 550, in solve_dependencies
93    solved = await run_in_threadpool(call, **sub_values)
94  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
95    return await loop.run_in_executor(None, func, *args)
96  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py", line 57, in run
97    result = self.fn(*self.args, **self.kwargs)
98  File "pydantic/main.py", line 400, in pydantic.main.BaseModel.__init__
99pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
100rank
101  ValueError() takes no keyword arguments (type=type_error)
102from fastapi import FastAPI, Request
103from fastapi.responses import JSONResponse
104
105
106@app.exception_handler(ValueError)
107async def value_error_exception_handler(request: Request, exc: ValueError):
108    return JSONResponse(
109        status_code=400,
110        content={"message": str(exc)},
111    )
112{
113    "message": "Value Must be within range (0,1000000)"
114}
115

Source https://stackoverflow.com/questions/68914523

QUESTION

How to check two argument in java is either one of them null or both

Asked 2021-Dec-28 at 16:37

I want to validate below condition but something is going wrong with my if condition and returning invalid results. My validations are: Either productId or productAltID can have value or both can be null If both productId and productAltID are null, then productSellDate and productReturnDate must have value.

If productSellDate and productReturnDate are null, then productId or productAltID should have value.

Please find my code below, I'm getting incorrect result not sure what I'm messing up here:

1public class validate {
2
3    public static void main(String[] args) {
4
5        String productId = null;
6        String productAltID = "adfafadsf";
7        Date productSellDate = null;
8        Date productReturnDate = new Date();
9
10        if (productId != null || productAltID != null || productSellDate !=null && productReturnDate != null) {
11            System.out.println("validation success");
12        } else {
13            System.out.println("validation failed");
14        }
15
16    }
17}
18

// Valid Scenarios

1public class validate {
2
3    public static void main(String[] args) {
4
5        String productId = null;
6        String productAltID = "adfafadsf";
7        Date productSellDate = null;
8        Date productReturnDate = new Date();
9
10        if (productId != null || productAltID != null || productSellDate !=null && productReturnDate != null) {
11            System.out.println("validation success");
12        } else {
13            System.out.println("validation failed");
14        }
15
16    }
17}
18Combination 1: Valid Scenario
19        String productId = null;
20        String productAltID = null;
21        Date productSellDate = new Date();
22        Date productReturnDate = new Date();
23
24
25Combination 2: Valid Scenario
26        String productId = null;
27        String productAltID = "3432fefsf";
28        Date productSellDate = new Date();
29        Date productReturnDate = new Date();
30
31Combination 3: Valid Scenario
32        String productId = "sdf3234234324";
33        String productAltID = "3432fefsf";
34        Date productSellDate = null;
35        Date productReturnDate = null;
36
37Combination 4: Valid Scenario
38        String productId = null;
39        String productAltID = "3432fefsf";
40        Date productSellDate = null;
41        Date productReturnDate = null;
42

ANSWER

Answered 2021-Nov-03 at 21:18

As noted, the operator && binds tighter than || so the logic wasn't doing what you wished it was. See Operators page of the Oracle tutorial.

A little Literate Programming goes a long way to avoiding this sort of thing.

1public class validate {
2
3    public static void main(String[] args) {
4
5        String productId = null;
6        String productAltID = "adfafadsf";
7        Date productSellDate = null;
8        Date productReturnDate = new Date();
9
10        if (productId != null || productAltID != null || productSellDate !=null && productReturnDate != null) {
11            System.out.println("validation success");
12        } else {
13            System.out.println("validation failed");
14        }
15
16    }
17}
18Combination 1: Valid Scenario
19        String productId = null;
20        String productAltID = null;
21        Date productSellDate = new Date();
22        Date productReturnDate = new Date();
23
24
25Combination 2: Valid Scenario
26        String productId = null;
27        String productAltID = "3432fefsf";
28        Date productSellDate = new Date();
29        Date productReturnDate = new Date();
30
31Combination 3: Valid Scenario
32        String productId = "sdf3234234324";
33        String productAltID = "3432fefsf";
34        Date productSellDate = null;
35        Date productReturnDate = null;
36
37Combination 4: Valid Scenario
38        String productId = null;
39        String productAltID = "3432fefsf";
40        Date productSellDate = null;
41        Date productReturnDate = null;
42boolean hasAnId = productId != null || productAltID != null;
43boolean hasDates = productSellDate != null && productReturnDate != null;
44if ( hasAnId || hasDates ) ...
45

Source https://stackoverflow.com/questions/69831227

QUESTION

Is there anyway to clear a textarea without using an onclick function?

Asked 2021-Dec-16 at 06:29

I want to achieve an automatic clear and display another value corresponds to new inputted data

This is my html code for input data and text area

1function getInputValue(){
2   
3    var integer = document.getElementById("integer").value;
4    var text = document.getElementById("answer");
5    
6    for (var i = 1; i <= integer; i++) {
7        if (i % 15 == 0){
8          text.append( i, "= ","fizzbuzz\n")
9        }
10        else if (i % 3 == 0){
11            text.append( i, "= ","fizz \n")
12        }
13        else if (i % 5 == 0){
14            text.append( i, "= ","buzz \n")
15        }
16        else{
17          text.append(i, "= \n")
18        }
19        
20    }
21    
22}
1function getInputValue(){
2   
3    var integer = document.getElementById("integer").value;
4    var text = document.getElementById("answer");
5    
6    for (var i = 1; i <= integer; i++) {
7        if (i % 15 == 0){
8          text.append( i, "= ","fizzbuzz\n")
9        }
10        else if (i % 3 == 0){
11            text.append( i, "= ","fizz \n")
12        }
13        else if (i % 5 == 0){
14            text.append( i, "= ","buzz \n")
15        }
16        else{
17          text.append(i, "= \n")
18        }
19        
20    }
21    
22}<div class="form-floating mb-3">
23                            
24  <input class="form-control" id="integer" onChange="getInputValue()" type="text" placeholder="Input an Integer for N" data-sb-validations="required" />
25  <label for="integer">Input an Integer for N</label>
26</div>
27
28<div class="col-lg-6 form-floating answer mb-3">
29     <textarea disabled class="form-control" id="answer" type="text" placeholder="Answer" style="height: 20rem"></textarea>
30 </div>

If you will try to run the snippet you can see that if you will change the value in the input tag the textarea will just stack all the outputs

ANSWER

Answered 2021-Dec-16 at 05:39

As far as I can see here, there isn't a way to clear a textarea without using an onclick function.

Source https://stackoverflow.com/questions/70374263

QUESTION

logistic regression and GridSearchCV using python sklearn

Asked 2021-Dec-10 at 14:14

I am trying code from this page. I ran up to the part LR (tf-idf) and got the similar results

After that I decided to try GridSearchCV. My questions below:

1)

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16

Then I calculated f1 score manually. why it is not matching?

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23
  1. If I try scoring='precision' why does it give below error? I am not clear mainly because I have relatively balanced dataset (55-45%) and f1 which requires precision is getting calculated without any problems

#lets try gridsearchcv #https://www.kaggle.com/enespolat/grid-search-with-logistic-regression

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23from sklearn.model_selection import GridSearchCV
24
25grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
26logreg=LogisticRegression(solver = 'liblinear')
27logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='precision')
28logreg_cv.fit(X_train_vectors_tfidf, y_train)
29
30print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
31print("best score :",logreg_cv.best_score_)
32
33
34
35/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
36  _warn_prf(average, modifier, msg_start, len(result))
37/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
38  _warn_prf(average, modifier, msg_start, len(result))
39/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
40  _warn_prf(average, modifier, msg_start, len(result))
41/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
42  _warn_prf(average, modifier, msg_start, len(result))
43/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
44  _warn_prf(average, modifier, msg_start, len(result))
45/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
46  _warn_prf(average, modifier, msg_start, len(result))
47tuned hpyerparameters :(best parameters)  {'C': 0.1, 'penalty': 'l2'}
48best score : 0.9474200393672962
49
  1. is there any easier way to get predictions on the train data back? we already have the logreg_cv object. I used below method to get the predictions back. Is there a better way to do the same?

logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]

############################

############update 1

  1. Please answer question 1 from above. In the comment for the question it says The best score in GridSearchCV is calculated by taking the average score from cross validation for the best estimators. That is, it is calculated from data that is held out during fitting. From what I can tell, you are calculating predicted values from the training data and calculating an F1 score on that. Since the model was trained on that data, that is why the F1 score is so much larger compared to the results in the grid search

is that the reason I get below results #tuned hpyerparameters :(best parameters) {'C': 10.0, 'penalty': 'l2'} #best score : 0.7390325593588823

but when i do manually i get f1_score(y_train, final_prediction) 0.9839388145315489

2)

I tried to tune using f1_micro as suggested in the answer below. No error message. I am still not clear why f1_micro is not failing when precision fails

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23from sklearn.model_selection import GridSearchCV
24
25grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
26logreg=LogisticRegression(solver = 'liblinear')
27logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='precision')
28logreg_cv.fit(X_train_vectors_tfidf, y_train)
29
30print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
31print("best score :",logreg_cv.best_score_)
32
33
34
35/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
36  _warn_prf(average, modifier, msg_start, len(result))
37/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
38  _warn_prf(average, modifier, msg_start, len(result))
39/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
40  _warn_prf(average, modifier, msg_start, len(result))
41/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
42  _warn_prf(average, modifier, msg_start, len(result))
43/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
44  _warn_prf(average, modifier, msg_start, len(result))
45/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
46  _warn_prf(average, modifier, msg_start, len(result))
47tuned hpyerparameters :(best parameters)  {'C': 0.1, 'penalty': 'l2'}
48best score : 0.9474200393672962
49from sklearn.model_selection import GridSearchCV
50
51grid={"C":np.logspace(-3,3,7), "penalty":["l2"], "solver":['liblinear','newton-cg'], 'class_weight':[{ 0:0.95, 1:0.05 }, { 0:0.55, 1:0.45 }, { 0:0.45, 1:0.55 },{ 0:0.05, 1:0.95 }]}# l1 lasso l2 ridge
52#logreg=LogisticRegression(solver = 'liblinear')
53logreg=LogisticRegression()
54logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1_micro')
55logreg_cv.fit(X_train_vectors_tfidf, y_train)
56
57tuned hpyerparameters :(best parameters)  {'C': 10.0, 'class_weight': {0: 0.45, 1: 0.55}, 'penalty': 'l2', 'solver': 'newton-cg'}
58best score : 0.7894909688013136
59

ANSWER

Answered 2021-Dec-09 at 23:12

You end up with the error with precision because some of your penalization is too strong for this model, if you check the results, you get 0 for f1 score when C = 0.001 and C = 0.01

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23from sklearn.model_selection import GridSearchCV
24
25grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
26logreg=LogisticRegression(solver = 'liblinear')
27logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='precision')
28logreg_cv.fit(X_train_vectors_tfidf, y_train)
29
30print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
31print("best score :",logreg_cv.best_score_)
32
33
34
35/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
36  _warn_prf(average, modifier, msg_start, len(result))
37/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
38  _warn_prf(average, modifier, msg_start, len(result))
39/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
40  _warn_prf(average, modifier, msg_start, len(result))
41/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
42  _warn_prf(average, modifier, msg_start, len(result))
43/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
44  _warn_prf(average, modifier, msg_start, len(result))
45/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
46  _warn_prf(average, modifier, msg_start, len(result))
47tuned hpyerparameters :(best parameters)  {'C': 0.1, 'penalty': 'l2'}
48best score : 0.9474200393672962
49from sklearn.model_selection import GridSearchCV
50
51grid={"C":np.logspace(-3,3,7), "penalty":["l2"], "solver":['liblinear','newton-cg'], 'class_weight':[{ 0:0.95, 1:0.05 }, { 0:0.55, 1:0.45 }, { 0:0.45, 1:0.55 },{ 0:0.05, 1:0.95 }]}# l1 lasso l2 ridge
52#logreg=LogisticRegression(solver = 'liblinear')
53logreg=LogisticRegression()
54logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1_micro')
55logreg_cv.fit(X_train_vectors_tfidf, y_train)
56
57tuned hpyerparameters :(best parameters)  {'C': 10.0, 'class_weight': {0: 0.45, 1: 0.55}, 'penalty': 'l2', 'solver': 'newton-cg'}
58best score : 0.7894909688013136
59res = pd.DataFrame(logreg_cv.cv_results_)
60res.iloc[:,res.columns.str.contains("split[0-9]_test_score|params",regex=True)]
61 
62                           params  split0_test_score  split1_test_score  split2_test_score
630   {'C': 0.001, 'penalty': 'l2'}           0.000000           0.000000           0.000000
641    {'C': 0.01, 'penalty': 'l2'}           0.000000           0.000000           0.000000
652     {'C': 0.1, 'penalty': 'l2'}           0.973568           0.952607           0.952174
663     {'C': 1.0, 'penalty': 'l2'}           0.863934           0.851064           0.836449
674    {'C': 10.0, 'penalty': 'l2'}           0.811634           0.769547           0.787838
685   {'C': 100.0, 'penalty': 'l2'}           0.789826           0.762162           0.773438
696  {'C': 1000.0, 'penalty': 'l2'}           0.781003           0.750000           0.763871
70

You can check this:

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23from sklearn.model_selection import GridSearchCV
24
25grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
26logreg=LogisticRegression(solver = 'liblinear')
27logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='precision')
28logreg_cv.fit(X_train_vectors_tfidf, y_train)
29
30print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
31print("best score :",logreg_cv.best_score_)
32
33
34
35/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
36  _warn_prf(average, modifier, msg_start, len(result))
37/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
38  _warn_prf(average, modifier, msg_start, len(result))
39/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
40  _warn_prf(average, modifier, msg_start, len(result))
41/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
42  _warn_prf(average, modifier, msg_start, len(result))
43/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
44  _warn_prf(average, modifier, msg_start, len(result))
45/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
46  _warn_prf(average, modifier, msg_start, len(result))
47tuned hpyerparameters :(best parameters)  {'C': 0.1, 'penalty': 'l2'}
48best score : 0.9474200393672962
49from sklearn.model_selection import GridSearchCV
50
51grid={"C":np.logspace(-3,3,7), "penalty":["l2"], "solver":['liblinear','newton-cg'], 'class_weight':[{ 0:0.95, 1:0.05 }, { 0:0.55, 1:0.45 }, { 0:0.45, 1:0.55 },{ 0:0.05, 1:0.95 }]}# l1 lasso l2 ridge
52#logreg=LogisticRegression(solver = 'liblinear')
53logreg=LogisticRegression()
54logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1_micro')
55logreg_cv.fit(X_train_vectors_tfidf, y_train)
56
57tuned hpyerparameters :(best parameters)  {'C': 10.0, 'class_weight': {0: 0.45, 1: 0.55}, 'penalty': 'l2', 'solver': 'newton-cg'}
58best score : 0.7894909688013136
59res = pd.DataFrame(logreg_cv.cv_results_)
60res.iloc[:,res.columns.str.contains("split[0-9]_test_score|params",regex=True)]
61 
62                           params  split0_test_score  split1_test_score  split2_test_score
630   {'C': 0.001, 'penalty': 'l2'}           0.000000           0.000000           0.000000
641    {'C': 0.01, 'penalty': 'l2'}           0.000000           0.000000           0.000000
652     {'C': 0.1, 'penalty': 'l2'}           0.973568           0.952607           0.952174
663     {'C': 1.0, 'penalty': 'l2'}           0.863934           0.851064           0.836449
674    {'C': 10.0, 'penalty': 'l2'}           0.811634           0.769547           0.787838
685   {'C': 100.0, 'penalty': 'l2'}           0.789826           0.762162           0.773438
696  {'C': 1000.0, 'penalty': 'l2'}           0.781003           0.750000           0.763871
70lr = LogisticRegression(C=0.01).fit(X_train_vectors_tfidf,y_train)
71np.unique(lr.predict(X_train_vectors_tfidf))
72array([0])
73

And that the probabilities predicted drift towards the intercept:

1#lets try gridsearchcv
2#https://www.kaggle.com/enespolat/grid-search-with-logistic-regression
3
4from sklearn.model_selection import GridSearchCV
5
6grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
7logreg=LogisticRegression(solver = 'liblinear')
8logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1')
9logreg_cv.fit(X_train_vectors_tfidf, y_train)
10
11print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
12print("best score :",logreg_cv.best_score_)
13
14#tuned hpyerparameters :(best parameters)  {'C': 10.0, 'penalty': 'l2'}
15#best score : 0.7390325593588823
16logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]
17final_prediction=np.where(logreg_cv.predict_proba(X_train_vectors_tfidf)[:,1]>=0.5,1,0)
18#https://www.statology.org/f1-score-in-python/
19from sklearn.metrics import f1_score
20#calculate F1 score
21f1_score(y_train, final_prediction)
220.9839388145315489
23from sklearn.model_selection import GridSearchCV
24
25grid={"C":np.logspace(-3,3,7), "penalty":["l2"]}# l1 lasso l2 ridge
26logreg=LogisticRegression(solver = 'liblinear')
27logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='precision')
28logreg_cv.fit(X_train_vectors_tfidf, y_train)
29
30print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
31print("best score :",logreg_cv.best_score_)
32
33
34
35/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
36  _warn_prf(average, modifier, msg_start, len(result))
37/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
38  _warn_prf(average, modifier, msg_start, len(result))
39/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
40  _warn_prf(average, modifier, msg_start, len(result))
41/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
42  _warn_prf(average, modifier, msg_start, len(result))
43/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
44  _warn_prf(average, modifier, msg_start, len(result))
45/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py:1308: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples. Use `zero_division` parameter to control this behavior.
46  _warn_prf(average, modifier, msg_start, len(result))
47tuned hpyerparameters :(best parameters)  {'C': 0.1, 'penalty': 'l2'}
48best score : 0.9474200393672962
49from sklearn.model_selection import GridSearchCV
50
51grid={"C":np.logspace(-3,3,7), "penalty":["l2"], "solver":['liblinear','newton-cg'], 'class_weight':[{ 0:0.95, 1:0.05 }, { 0:0.55, 1:0.45 }, { 0:0.45, 1:0.55 },{ 0:0.05, 1:0.95 }]}# l1 lasso l2 ridge
52#logreg=LogisticRegression(solver = 'liblinear')
53logreg=LogisticRegression()
54logreg_cv=GridSearchCV(logreg,grid,cv=3,scoring='f1_micro')
55logreg_cv.fit(X_train_vectors_tfidf, y_train)
56
57tuned hpyerparameters :(best parameters)  {'C': 10.0, 'class_weight': {0: 0.45, 1: 0.55}, 'penalty': 'l2', 'solver': 'newton-cg'}
58best score : 0.7894909688013136
59res = pd.DataFrame(logreg_cv.cv_results_)
60res.iloc[:,res.columns.str.contains("split[0-9]_test_score|params",regex=True)]
61 
62                           params  split0_test_score  split1_test_score  split2_test_score
630   {'C': 0.001, 'penalty': 'l2'}           0.000000           0.000000           0.000000
641    {'C': 0.01, 'penalty': 'l2'}           0.000000           0.000000           0.000000
652     {'C': 0.1, 'penalty': 'l2'}           0.973568           0.952607           0.952174
663     {'C': 1.0, 'penalty': 'l2'}           0.863934           0.851064           0.836449
674    {'C': 10.0, 'penalty': 'l2'}           0.811634           0.769547           0.787838
685   {'C': 100.0, 'penalty': 'l2'}           0.789826           0.762162           0.773438
696  {'C': 1000.0, 'penalty': 'l2'}           0.781003           0.750000           0.763871
70lr = LogisticRegression(C=0.01).fit(X_train_vectors_tfidf,y_train)
71np.unique(lr.predict(X_train_vectors_tfidf))
72array([0])
73# expected probability
74np.exp(lr.intercept_)/(1+np.exp(lr.intercept_))
75array([0.41764462])
76
77lr.predict_proba(X_train_vectors_tfidf)
78 
79array([[0.58732636, 0.41267364],
80       [0.57074279, 0.42925721],
81       [0.57219143, 0.42780857],
82       ...,
83       [0.57215605, 0.42784395],
84       [0.56988186, 0.43011814],
85       [0.58966184, 0.41033816]])
86

For the question on "get predictions on the train data back", i think that's the only way. The model is refitted on the whole training set using the best parameters, but the predictions or predicted probabilities are not stored. If you are looking for the values obtained during train / test, you can check cross_val_predict

Source https://stackoverflow.com/questions/70264157

QUESTION

Single File ASP.NET Core 5 web app does not load static resources

Asked 2021-Dec-01 at 17:09

I'm trying to create a single file asp.net core 5 web app. Goal is to have a single .exe file, run the Kestrel server by executing this exe file and load the page in the browser.

I created an ASP.NET Core 5 template app in VS 2019. Then using cli I run this command:

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3

This generates an exe file, which when I copy elsewhere, runs without a problem. But when I browse the page, none of the static files are loaded:

enter image description here

What would be the proper way of generating a single file asp.net core app, so it loads static content ?

EDIT

As requested, putting here the screenshot of the output after the publish

enter image description here

EDIT 2

To get a reproducible project:

Visual Studio 2019 -> New Solution -> ASP.NET Core Web App with the configuration below

enter image description here

EDIT 3

Thanks to the answer by @JHBonarius, I changed the Program.cs to set ContentRoot to a temp folder where wwwroot content is getting extracted.

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29

While I appreciate that this seems like a hack (I couldn't find any official documentation for this path), I wanted to have some working code.

With these changes page now loads static resources, not all of them though.

This is the content of wwwroot folder in the solution explorer

enter image description here

And this is the content of extracted wwwroot folder on the temp path

enter image description here enter image description here

As can be seen js/css folders are missing altogether as well as jquery-validation & jquery-validation-unobtrusive folders.

Any clue what's going on ?

I created a github repo with latest changes.

ANSWER

Answered 2021-Nov-29 at 21:46

I tried to reproduce your problem on new asp net core empty project and it works fine.

Perhaps Startup is missing some configuration.

Here's what I did.

csproj

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34

Startup.cs

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50

wwwroot

Similar to your scenario, there is also bootstrap.

wwwroot content

index.html

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50<!DOCTYPE html>
51<html>
52<head>
53    <meta charset="utf-8" />
54    <title>TEST</title>
55    <link href="css/bootstrap.min.css" rel="stylesheet">
56</head>
57<body>
58    <script src="js/bootstrap.min.js"></script>
59</body>
60</html>
61

-- Update 29/11/2021

My expectation was that .exe will be a self-contained bundle and upon execution it would extract static files and be able to resolve references to it. That doesn't seem to be the case.

Well, then you'd have to mark everything in wwwroot as embedded resource

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50<!DOCTYPE html>
51<html>
52<head>
53    <meta charset="utf-8" />
54    <title>TEST</title>
55    <link href="css/bootstrap.min.css" rel="stylesheet">
56</head>
57<body>
58    <script src="js/bootstrap.min.js"></script>
59</body>
60</html>
61<PropertyGroup>
62    <TargetFramework>net5.0</TargetFramework>
63   <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
64</PropertyGroup>
65
66<ItemGroup>
67    <Compile Remove="wwwroot\**" />
68    <Content Remove="wwwroot\**" />
69     <EmbeddedResource Include="wwwroot\**\*">
70      <Link>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</Link>
71      <LogicalName>wwwroot\%(RecursiveDir)%(Filename)%(Extension) 
72      </LogicalName>
73    </EmbeddedResource>
74    <None Remove="wwwroot\**" />
75</ItemGroup>
76

And perform file extraction in Configure(IApplicationBuilder, IWebHostEnvironment) method

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50<!DOCTYPE html>
51<html>
52<head>
53    <meta charset="utf-8" />
54    <title>TEST</title>
55    <link href="css/bootstrap.min.css" rel="stylesheet">
56</head>
57<body>
58    <script src="js/bootstrap.min.js"></script>
59</body>
60</html>
61<PropertyGroup>
62    <TargetFramework>net5.0</TargetFramework>
63   <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
64</PropertyGroup>
65
66<ItemGroup>
67    <Compile Remove="wwwroot\**" />
68    <Content Remove="wwwroot\**" />
69     <EmbeddedResource Include="wwwroot\**\*">
70      <Link>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</Link>
71      <LogicalName>wwwroot\%(RecursiveDir)%(Filename)%(Extension) 
72      </LogicalName>
73    </EmbeddedResource>
74    <None Remove="wwwroot\**" />
75</ItemGroup>
76var basePath = AppDomain.CurrentDomain.BaseDirectory;
77var wwwrootPath = Path.Combine(basePath, "wwwroot");
78
79if (!Directory.Exists(wwwrootPath))
80{
81    var assembly = typeof(Startup).Assembly;
82    Directory.CreateDirectory(wwwrootPath);
83
84    var resourcePaths = assembly.GetManifestResourceNames()
85        .Where(rnn => rnn.Contains("wwwroot"))
86        .ToList();
87
88    foreach (var resourcePath in resourcePaths)
89    {
90        var fileName = resourcePath;
91        var filePath = Path.Combine(basePath, fileName);
92        var fileInfo = new System.IO.FileInfo(filePath);
93        fileInfo.Directory.Create();
94        using var stream = File.Create(filePath);
95        using var resourceStream = assembly.GetManifestResourceStream(resourcePath);
96
97        resourceStream.CopyTo(stream);
98    }
99};
100

If you don't like this approach you might consider EmbeddedFileProvider

Startup.cs:Configure

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50<!DOCTYPE html>
51<html>
52<head>
53    <meta charset="utf-8" />
54    <title>TEST</title>
55    <link href="css/bootstrap.min.css" rel="stylesheet">
56</head>
57<body>
58    <script src="js/bootstrap.min.js"></script>
59</body>
60</html>
61<PropertyGroup>
62    <TargetFramework>net5.0</TargetFramework>
63   <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
64</PropertyGroup>
65
66<ItemGroup>
67    <Compile Remove="wwwroot\**" />
68    <Content Remove="wwwroot\**" />
69     <EmbeddedResource Include="wwwroot\**\*">
70      <Link>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</Link>
71      <LogicalName>wwwroot\%(RecursiveDir)%(Filename)%(Extension) 
72      </LogicalName>
73    </EmbeddedResource>
74    <None Remove="wwwroot\**" />
75</ItemGroup>
76var basePath = AppDomain.CurrentDomain.BaseDirectory;
77var wwwrootPath = Path.Combine(basePath, "wwwroot");
78
79if (!Directory.Exists(wwwrootPath))
80{
81    var assembly = typeof(Startup).Assembly;
82    Directory.CreateDirectory(wwwrootPath);
83
84    var resourcePaths = assembly.GetManifestResourceNames()
85        .Where(rnn => rnn.Contains("wwwroot"))
86        .ToList();
87
88    foreach (var resourcePath in resourcePaths)
89    {
90        var fileName = resourcePath;
91        var filePath = Path.Combine(basePath, fileName);
92        var fileInfo = new System.IO.FileInfo(filePath);
93        fileInfo.Directory.Create();
94        using var stream = File.Create(filePath);
95        using var resourceStream = assembly.GetManifestResourceStream(resourcePath);
96
97        resourceStream.CopyTo(stream);
98    }
99};
100app.UseStaticFiles(new StaticFileOptions
101{
102    FileProvider = new EmbeddedFileProvider(
103        assembly: typeof(Startup).Assembly, 
104        baseNamespace: "TestApp.wwwroot"),
105});
106
107app.UseEndpoints(endpoints =>
108{
109    endpoints.MapGet("/", async http => { http.Response.Redirect("/index.html"); });
110});
111

csproj

1dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true \n 
2/p:IncludeNativeLibrariesForSelfExtract=true --self-contained true
3public class Program
4    {
5        public static void Main(string[] args)
6        {
7            var path = Path.Combine(Path.GetTempPath(), ".net", typeof(Program).Assembly.GetName().Name);
8
9            var directory = 
10                Directory
11                    .GetDirectories(path)
12                    .Select(path => new DirectoryInfo(path))
13                    .OrderByDescending(di => di.LastWriteTime)
14                    .First();
15
16            CreateHostBuilder(args)
17                .UseContentRoot(directory.FullName)
18                .Build()
19                .Run();
20        }
21
22        public static IHostBuilder CreateHostBuilder(string[] args) =>
23            Host.CreateDefaultBuilder(args)            
24                .ConfigureWebHostDefaults(webBuilder =>
25                {
26                    webBuilder.UseStartup<Startup>();
27                });
28    }
29<Project Sdk="Microsoft.NET.Sdk.Web">
30  <PropertyGroup>
31    <TargetFramework>net5.0</TargetFramework>
32  </PropertyGroup>
33</Project>
34public class Startup
35    {
36        public void ConfigureServices(IServiceCollection services)
37        {
38        }
39
40        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
41        {
42            app.UseDefaultFiles();
43            app.UseStaticFiles();
44
45            app.UseRouting();
46
47            app.UseEndpoints(endpoints => {});
48        }
49    }
50<!DOCTYPE html>
51<html>
52<head>
53    <meta charset="utf-8" />
54    <title>TEST</title>
55    <link href="css/bootstrap.min.css" rel="stylesheet">
56</head>
57<body>
58    <script src="js/bootstrap.min.js"></script>
59</body>
60</html>
61<PropertyGroup>
62    <TargetFramework>net5.0</TargetFramework>
63   <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
64</PropertyGroup>
65
66<ItemGroup>
67    <Compile Remove="wwwroot\**" />
68    <Content Remove="wwwroot\**" />
69     <EmbeddedResource Include="wwwroot\**\*">
70      <Link>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</Link>
71      <LogicalName>wwwroot\%(RecursiveDir)%(Filename)%(Extension) 
72      </LogicalName>
73    </EmbeddedResource>
74    <None Remove="wwwroot\**" />
75</ItemGroup>
76var basePath = AppDomain.CurrentDomain.BaseDirectory;
77var wwwrootPath = Path.Combine(basePath, "wwwroot");
78
79if (!Directory.Exists(wwwrootPath))
80{
81    var assembly = typeof(Startup).Assembly;
82    Directory.CreateDirectory(wwwrootPath);
83
84    var resourcePaths = assembly.GetManifestResourceNames()
85        .Where(rnn => rnn.Contains("wwwroot"))
86        .ToList();
87
88    foreach (var resourcePath in resourcePaths)
89    {
90        var fileName = resourcePath;
91        var filePath = Path.Combine(basePath, fileName);
92        var fileInfo = new System.IO.FileInfo(filePath);
93        fileInfo.Directory.Create();
94        using var stream = File.Create(filePath);
95        using var resourceStream = assembly.GetManifestResourceStream(resourcePath);
96
97        resourceStream.CopyTo(stream);
98    }
99};
100app.UseStaticFiles(new StaticFileOptions
101{
102    FileProvider = new EmbeddedFileProvider(
103        assembly: typeof(Startup).Assembly, 
104        baseNamespace: "TestApp.wwwroot"),
105});
106
107app.UseEndpoints(endpoints =>
108{
109    endpoints.MapGet("/", async http => { http.Response.Redirect("/index.html"); });
110});
111<EmbeddedResource Include="wwwroot\**\*" />
112

Source https://stackoverflow.com/questions/69915710

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Validation

Tutorials and Learning Resources are not available at this moment for Validation

Share this Page

share link

Get latest updates on Validation